public class CProgram
{
List<Declaration> decpart = new List<Declaration>();
List<Statement> body = new List<Statement>();
public CProgram(List<Declaration> d, List<Statement> b)
{
decpart = d;
body = b;
}
public String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
foreach(var item in decpart)
result = result + item.toStringIndented(indent + " ") + ",\n";
foreach(var item in body)
result = result + item.toStringIndented(indent + " ") + ",\n";
Console.WriteLine(result + indent + ")");
return result + indent + ")";
}
}
public CProgram program()
{
match(Token.TokenType.Int);
match(Token.TokenType.Main);
match(Token.TokenType.LeftParen);
match(Token.TokenType.RightParen);
match(Token.TokenType.LeftBrace);
List<Declaration> decls = declarations();
List<Statement> stmts = statements();
match(Token.TokenType.RightBrace);
CProgram p = new CProgram(decls, stmts);
if (error_flag == false)
Console.WriteLine("No Syntax Error. Comgratulation!");
return p;
}
public CProgram(List<Declaration> d, List<Statement> b)
{
decpart = d;
body = b;
toString();
}
public override String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
foreach(var item in decpart)
result = result + item.toStringIndented(indent + " ") + ",\n";
foreach(var item in body)
result = result + item.toStringIndented(indent + " ") + ",\n";
Console.WriteLine(result + indent + ")");
return result + indent + ")";
}
using System;
using System.Diagnostics;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AbstractSyntax
{
public class CProgram
{
List<Declaration> decpart = new List<Declaration>();
List<Statement> body = new List<Statement>();
public CProgram(List<Declaration> d, List<Statement> b)
{
decpart = d;
body = b;
ToString();
}
public override String ToString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
foreach(var item in decpart)
result = result + item.toStringIndented(indent + " ") + ",\n";
foreach(var item in body)
result = result + item.toStringIndented(indent + " ") + ",\n";
Console.WriteLine(result + indent + ")");
return result + indent + ")";
}
}
}
Declaration.cspublic class Declaration
{
public List<Declaration> decl = null;
public Declaration()
{
}
public Declaration(List<Declaration> Declarations)
{
this.decl = Declarations;
ToString();
}
public override String ToString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
StringBuilder sb = new StringBuilder();
sb.Append(indent);
sb.Append("Declarations(\n");
int i = 0;
foreach (Declaration s in decl)
{
sb.Append(this.toStringIndented(indent + " "));
i++;
if (i == decl.Count)
sb.Append("\n");
else
sb.Append(",\n");
}
sb.Append(indent);
sb.Append(")");
return sb.ToString();
}
}
public String toStringIndented(String indent)
{
StringBuilder sb = new StringBuilder();
sb.Append(indent);
sb.Append("Declarations(\n");
int i = 0;
if(decl != null)
{
foreach (Declaration s in decl)
{
sb.Append(this.toStringIndented(indent + " "));
i++;
if (i == decl.Count)
sb.Append("\n");
else
sb.Append(",\n");
}
}
sb.Append(indent);
sb.Append(")");
return sb.ToString();
}
public CProgram program()
{
match(Token.TokenType.Int);
match(Token.TokenType.Main);
match(Token.TokenType.LeftParen);
match(Token.TokenType.RightParen);
match(Token.TokenType.LeftBrace);
List<Declaration> decls = declarations();
List<Statement> stmts = statements();
match(Token.TokenType.RightBrace);
CProgram p = new CProgram(decls, stmts);
if (error_flag == false)
Console.WriteLine("No Syntax Error. Comgratulation!");
return p;
}
private List<Declaration> declarations()
{
List<Declaration> ds = new List<Declaration>();
List<Declaration> dsl = new List<Declaration>();
while (current_token.getType() == Token.TokenType.Int ||
current_token.getType() == Token.TokenType.Float ||
current_token.getType() == Token.TokenType.Char ||
current_token.getType() == Token.TokenType.Bool)
{
dsl = declaration();
ds.AddRange(dsl);
}
return ds;
}
private List<Declaration> declaration()
{
List<Declaration> result = new List<Declaration>();
Type t = type();
Variable v = new Variable(match(Token.TokenType.Identifier));
if (current_token.getType() == Token.TokenType.LeftBracket)
{
match(Token.TokenType.LeftBracket);
int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
match(Token.TokenType.RightBracket);
ArrayDecl ad = new ArrayDecl(v, t, i);
result.Add(ad);
}
else
{
VariableDecl vd = new VariableDecl(v, t);
result.Add(vd);
}
while (current_token.getType() == Token.TokenType.Comma)
{
match(Token.TokenType.Comma);
Variable v1 = new Variable(match(Token.TokenType.Identifier));
if (current_token.getType() == Token.TokenType.LeftBracket)
{
match(Token.TokenType.LeftBracket);
int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
match(Token.TokenType.RightBracket);
ArrayDecl ad = new ArrayDecl(v1, t, i);
result.Add(ad);
}
else
{
VariableDecl vd2 = new VariableDecl(v1, t);
result.Add(vd2);
}
}
match(Token.TokenType.Semicolon);
return result;
}
private Type type()
{
Type t = null;
switch (current_token.getType())
{
case Token.TokenType.Int:
case Token.TokenType.Char:
case Token.TokenType.Float:
case Token.TokenType.Bool:
t = new Type(current_token.toString());
current_token = mylexer.next();
return t;
default:
Console.WriteLine("Syntax error: line " + mylexer.getCurrentLine() + " expecting: int, char, bool, or float" + "; saw: " + current_token.getType() + "value is " + current_token.toString());
current_token = mylexer.next();
error_flag = true;
break;
}
return t;
}
public class Declaration
{
public List<Declaration> decl = null;
public Declaration()
{
}
public Declaration(List<Declaration> Declarations)
{
this.decl = Declarations;
}
public String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
StringBuilder sb = new StringBuilder();
sb.Append(indent);
sb.Append("Declarations(\n");
int i = 0;
if (decl != null)
{
foreach (Declaration s in decl)
{
sb.Append(this.toStringIndented(indent + " "));
i++;
if (i == decl.Count)
sb.Append("\n");
else
sb.Append(",\n");
}
}
sb.Append(indent);
sb.Append(")");
return sb.ToString();
}
}
public CProgram program()
{
Console.WriteLine("The program main has the following statements:");
match(Token.TokenType.Int);
match(Token.TokenType.Main);
match(Token.TokenType.LeftParen);
match(Token.TokenType.RightParen);
match(Token.TokenType.LeftBrace);
List<Declaration> decls = declarations();
List<Statement> stmts = statements();
match(Token.TokenType.RightBrace);
CProgram p = new CProgram(decls, stmts);
if (error_flag == false)
Console.WriteLine("No Syntax Error. Comgratulation!");
return p;
}
private List<Declaration> declarations()
{
List<Declaration> ds = new List<Declaration>();
List<Declaration> dsl = new List<Declaration>();
while (current_token.getType() == Token.TokenType.Int ||
current_token.getType() == Token.TokenType.Float ||
current_token.getType() == Token.TokenType.Char ||
current_token.getType() == Token.TokenType.Bool)
{
dsl = declaration();
ds.AddRange(dsl);
}
return ds;
}
private List<Declaration> declaration()
{
List<Declaration> result = new List<Declaration>();
Type t = type();
Variable v = new Variable(match(Token.TokenType.Identifier));
if (current_token.getType() == Token.TokenType.LeftBracket)
{
match(Token.TokenType.LeftBracket);
int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
match(Token.TokenType.RightBracket);
ArrayDecl ad = new ArrayDecl(v, t, i);
result.Add(ad);
}
else
{
VariableDecl vd = new VariableDecl(v, t);
result.Add(vd);
}
while (current_token.getType() == Token.TokenType.Comma)
{
match(Token.TokenType.Comma);
Variable v1 = new Variable(match(Token.TokenType.Identifier));
if (current_token.getType() == Token.TokenType.LeftBracket)
{
match(Token.TokenType.LeftBracket);
int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
match(Token.TokenType.RightBracket);
ArrayDecl ad = new ArrayDecl(v1, t, i);
result.Add(ad);
}
else
{
VariableDecl vd2 = new VariableDecl(v1, t);
result.Add(vd2);
}
}
match(Token.TokenType.Semicolon);
return result;
}
private Type type()
{
Type t = null;
switch (current_token.getType())
{
case Token.TokenType.Int:
case Token.TokenType.Char:
case Token.TokenType.Float:
case Token.TokenType.Bool:
t = new Type(current_token.toString());
current_token = mylexer.next();
return t;
default:
Console.WriteLine("Syntax error: line " + mylexer.getCurrentLine() + " expecting: int, char, bool, or float" + "; saw: " + current_token.getType() + "value is " + current_token.toString());
current_token = mylexer.next();
error_flag = true;
break;
}
return t;
}
public class CProgram
{
List<Declaration> decpart = new List<Declaration>();
List<Statement> body = new List<Statement>();
public CProgram(List<Declaration> d, List<Statement> b)
{
decpart = d;
body = b;
ToString();
}
public override String ToString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
foreach (var item in decpart)
result = result + item.toStringIndented(indent + " ") + ",\n";
foreach (var item in body)
result = result + item.toStringIndented(indent + " ") + ",\n";
Console.WriteLine(result + indent + ")");
return result + indent + ")";
}
}
public class Declaration
{
public List<Declaration> decl = null;
public Declaration()
{
}
public Declaration(List<Declaration> Declarations)
{
this.decl = Declarations;
}
public String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
StringBuilder sb = new StringBuilder();
sb.Append(indent);
sb.Append("Declarations(\n");
int i = 0;
if (decl != null)
{
foreach (Declaration s in decl)
{
sb.Append(this.toStringIndented(indent + " "));
i++;
if (i == decl.Count)
sb.Append("\n");
else
sb.Append(",\n");
}
}
sb.Append(indent);
sb.Append(")");
return sb.ToString();
}
}
public class VariableDecl : Declaration
{
Variable v = null;
Type t = null;
public VariableDecl(List<Declaration> Declarations)
: base(Declarations)
{
}
public VariableDecl(Variable v1, Type t1)
: base()
{
v = v1;
t = t1;
toString();
}
public new String toString()
{
return toStringIndented("");
}
public new String toStringIndented(String indent)
{
Console.WriteLine("Variable Declaration: " + v.toString() + " " + t.toString());
return indent + "Variable Declaration: " + v.toString() + " " + t.toString();
}
}
public class ArrayDecl : Declaration
{
Variable v = null;
Type t = null;
int size = 0;
public ArrayDecl(List<Declaration> Declarations)
: base(Declarations)
{
}
public ArrayDecl(Variable v1, Type t1, int size1)
: base()
{
v = v1;
t = t1;
size = size1;
toString();
}
public new String toString()
{
return toStringIndented("");
}
public new String toStringIndented(String indent)
{
Console.WriteLine("Variable Declaration: " + v.toString() + " " + t.toString() + " size is " + size);
return indent + "Variable Declaration: " + v.toString() + " " + t.toString() + " size is " + size;
}
}
public class CProgram
{
private List<Declaration> decpart = new List<Declaration>();
private List<Statement> body = new List<Statement>();
public CProgram(List<Declaration> d, List<Statement> b)
{
decpart = d;
body = b;
ToString();
}
public override String ToString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
foreach (Declaration item in decpart)
Console.WriteLine(item);
//result = result + item.toStringIndented(indent + " ") + ",\n";
foreach(var item in body)
result = result + item.toStringIndented(indent + " ") + ",\n";
//Console.WriteLine(result + indent + ")");
return result + indent + ")";
}
public class CProgram
{
private List<Declaration> decpart = new List<Declaration>();
private List<Statement> body = new List<Statement>();
public CProgram(List<Declaration> d, List<Statement> b)
{
decpart = d;
body = b;
ToString();
}
public override String ToString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
//Console.WriteLine(result);
foreach (Declaration item in decpart)
{
result = result + item.toStringIndented(indent + " ") + ",\n";
//Console.WriteLine(item);
}
foreach(var item in body)
result = result + item.toStringIndented(indent + " ") + ",\n";
//Console.WriteLine(result + indent + ")");
return result + indent + ")";
}
}
public class Declaration
{
public List<Declaration> decl = null;
public Declaration()
{
}
public Declaration(List<Declaration> Declarations)
{
this.decl = Declarations;
}
public String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
StringBuilder sb = new StringBuilder();
sb.Append(indent);
sb.Append("Declarations(\n");
int i = 0;
if (decl != null)
{
//Console.WriteLine("decl is not NULL!");
foreach (Declaration s in decl)
{
sb.Append(s.toStringIndented(indent + " "));
i++;
if (i == decl.Count)
sb.Append("\n");
else
sb.Append(",\n");
}
}
sb.Append(indent);
sb.Append(")");
return sb.ToString();
}
}
public String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
StringBuilder sb = new StringBuilder();
sb.Append(indent);
sb.Append("Declarations(\n");
int i = 0;
if (decl != null)
{
Console.WriteLine("decl is not NULL!");
foreach (Declaration s in decl)
{
sb.Append(s.toStringIndented(indent + " "));
i++;
if (i == decl.Count)
sb.Append("\n");
else
sb.Append(",\n");
}
}
sb.Append(indent);
sb.Append(")");
Console.WriteLine(sb.ToString());
return sb.ToString();
}
if (decl != null)
{
public override String ToString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
foreach (Declaration item in decpart)
result = result + item.ToString() + ",\n"; <---this line
foreach(var item in body)
result = result + item.toStringIndented(indent + " ") + ",\n";
Console.WriteLine(result + indent + ")");
return result + indent + ")";
public class CProgram
{
private List<Declaration> decpart = new List<Declaration>();
private List<Statement> body = new List<Statement>();
public CProgram(List<Declaration> d, List<Statement> b)
{
decpart = d;
body = b;
ToString();
}
public override String ToString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
Declaration temp = new Declaration(decpart);
result = result + temp;
foreach (var item in body) <--ignore
result = result + item.toStringIndented(indent + " ") + ",\n"; <--ignore
Console.WriteLine(result + indent + ")");
return result + indent + ")";
}
}
public class Declaration
{
public List<Declaration> decl = null;
public Declaration()
{
}
public Declaration(List<Declaration> Declarations)
{
this.decl = Declarations;
toString();
}
public String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
StringBuilder sb = new StringBuilder();
sb.Append(indent);
sb.Append("Declarations(\n");
int i = 0;
if (decl != null)
{
//Console.WriteLine("decl is not NULL!");
foreach (Declaration s in decl)
{
sb.Append(s.ToString() + "\n");
i++;
if (i == decl.Count)
sb.Append("\n");
else
sb.Append(",\n");
}
}
sb.Append(indent);
sb.Append(")");
Console.WriteLine(sb.ToString());
return sb.ToString();
}
}
public class Declaration
{
public List<Declaration> decl = null;
public Declaration()
{
}
public Declaration(List<Declaration> Declarations)
{
this.decl = Declarations;
toString();
}
public String toString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
StringBuilder sb = new StringBuilder();
sb.Append(indent);
sb.Append("Declarations(\n");
int i = 0;
if (decl != null)
{
//Console.WriteLine("decl is not NULL!");
foreach (Declaration s in decl)
{
sb.Append(s.ToString());
i++;
if (i == decl.Count)
sb.Append("\n");
else
sb.Append(",\n");
}
}
sb.Append(indent);
sb.Append(")");
//Console.WriteLine(sb.ToString());
return sb.ToString();
}
}
public class CProgram
{
private List<Declaration> decpart = new List<Declaration>();
private List<Statement> body = new List<Statement>();
public CProgram(List<Declaration> d, List<Statement> b)
{
decpart = d;
body = b;
ToString();
}
public override String ToString()
{
return toStringIndented("");
}
public String toStringIndented(String indent)
{
string result = indent + "Program(\n";
Declaration temp = new Declaration(decpart);
result = result + temp.toStringIndented("");
foreach (var item in body)
result = result + item.toStringIndented(indent + " ") + ",\n";
Console.WriteLine(result + indent + ")");
return result + indent + ")";
}
}
public CProgram program()
{
Console.WriteLine("The program main has the following statements:");
match(Token.TokenType.Int);
match(Token.TokenType.Main);
match(Token.TokenType.LeftParen);
match(Token.TokenType.RightParen);
match(Token.TokenType.LeftBrace);
List<Declaration> decls = declarations();
List<Statement> stmts = statements();
match(Token.TokenType.RightBrace);
foreach (Declaration s in decls)
Console.WriteLine(s.ToString());
CProgram p = new CProgram(decls, stmts);
if (error_flag == false)
Console.WriteLine("No Syntax Error. Comgratulation!");
return p;
}
private List<Declaration> declarations()
{
List<Declaration> ds = new List<Declaration>();
List<Declaration> dsl = new List<Declaration>();
while (current_token.getType() == Token.TokenType.Int ||
current_token.getType() == Token.TokenType.Float ||
current_token.getType() == Token.TokenType.Char ||
current_token.getType() == Token.TokenType.Bool)
{
dsl = declaration();
ds.AddRange(dsl);
}
return ds;
}
private List<Declaration> declaration()
{
List<Declaration> result = new List<Declaration>();
Type t = type();
Variable v = new Variable(match(Token.TokenType.Identifier));
if (current_token.getType() == Token.TokenType.LeftBracket)
{
match(Token.TokenType.LeftBracket);
int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
match(Token.TokenType.RightBracket);
ArrayDecl ad = new ArrayDecl(v, t, i);
result.Add(ad);
}
else
{
VariableDecl vd = new VariableDecl(v, t);
result.Add(vd);
}
while (current_token.getType() == Token.TokenType.Comma)
{
match(Token.TokenType.Comma);
Variable v1 = new Variable(match(Token.TokenType.Identifier));
if (current_token.getType() == Token.TokenType.LeftBracket)
{
match(Token.TokenType.LeftBracket);
int i = Convert.ToInt32(match(Token.TokenType.IntLiteral));
match(Token.TokenType.RightBracket);
ArrayDecl ad = new ArrayDecl(v1, t, i);
result.Add(ad);
}
else
{
VariableDecl vd2 = new VariableDecl(v1, t);
result.Add(vd2);
}
}
match(Token.TokenType.Semicolon);
return result;
}
public class VariableDecl : Declaration
{
Variable v = null;
Type t = null;
public VariableDecl(List<Declaration> Declarations)
: base(Declarations)
{
}
public VariableDecl(Variable v1, Type t1)
: base()
{
v = v1;
t = t1;
toString();
}
public new String toString()
{
return toStringIndented("");
}
public new String toStringIndented(String indent)
{
Console.WriteLine("Variable Declaration: " + v.toString() + " " + t.toString());
return indent + "Variable Declaration: " + v.toString() + " " + t.toString();
}
}
List<Person> persons = new List<Person>();
Person p = new Person("Gautham")
persons .Add(p);
Console.WriteLine(persons[0].PersonName); // this will print gautham
List<Person> persons = new List<Person>();
Person p = new Person("Gautham")
persons .Add(p);
foreach(Person p in persons )
{
Console.WriteLine(p.ToString()); // this will print gautham if you override ToString as shown below in class otherwise this will print class name.
Console.WriteLine(p.PersonName); // this will print gautham
}
public class Person
{
public string PersonName{get;set;}
public Person ()
{
// of this ctr is called person name wont get passed
}
public Person (string personName)
{
PersonName = personName;
}
public override ToString() // remeber these are case sensitive toString is not ToString
{
return this.PersonName ;
}
}