finished lab6

This commit is contained in:
RustyRaptor committed 2021-03-31 23:36:53 -06:00
1 parent 4e3eb0d308
commit 887235d6a8
8 files changed
+1419 -1053

No files matched your search

+138 -23
View File
@@ -32,7 +32,9 @@ ASTnode *ASTCreateNode(enum AST_Tree_Element_Type mytype)
void PT(int howmany)
{
// MISSING
for (int i = 0; i < howmany; i++) {
printf(" ");
}
}
void AST_Print_Type(enum AST_Decaf_Types t)
@@ -42,7 +44,7 @@ void AST_Print_Type(enum AST_Decaf_Types t)
printf(" INT ");
break;
case A_Decaf_BOOL:
printf(" BOOL ");
printf(" BOOLEAN ");
break;
case A_Decaf_STRING:
printf(" STRING ");
@@ -65,30 +67,44 @@ void ASTprint(int level, ASTnode *p)
else {
PT(level); /*indent */
switch (p->type) {
case A_VAR_RVALUE:
printf("Variable %s \n", p->name);
if (p->S1 != NULL) {
PT(level + 2);
printf("[\n");
ASTprint(level+2, p->S1);
PT(level + 2);
printf("]\n");
}
break;
case A_PROGRAM:
printf("PROGRAM \n");
// printf("PROGRAM \n");
ASTprint(level + 1, p->S1);
ASTprint(level + 1, p->S2);
break;
case A_EXTERN:
printf("EXTERN FUNC %s ", p->name);
printf(" ( ");
printf("EXTERN FUNC %s\n", p->name);
// printf("\n");
ASTprint(level + 1, p->S1);
printf(" ) ");
AST_Print_Type(p->A_Declared_Type);
printf("\n");
// printf("\n");
printf("END EXTERN with Type:\n ");
AST_Print_Type(p->A_Declared_Type);
break;
case A_PACKAGE:
printf("PACKAGE %s", p->name);
printf("{\n");
printf("Package : %s\n", p->name);
PT(level + 2);
// printf("{\n");
ASTprint(level + 1, p->S1);
ASTprint(level + 1, p->S2);
printf("}\n");
PT(level + 2);
printf("\n");
break;
case A_ExternType:
printf("ExternType ");
printf("EXTERN Type ");
AST_Print_Type(p->A_Declared_Type);
printf("\n");
break;
case A_VARDEC:
@@ -96,31 +112,34 @@ void ASTprint(int level, ASTnode *p)
printf(" %s", p->name);
if (p->S1 != NULL) {
printf("[ ");
printf("[");
printf("%d", p->S1->value);
printf(" ] ");
printf("] ");
}
printf(" with type ");
AST_Print_Type(p->A_Declared_Type);
printf(" ");
if (p->S1 == NULL) {
printf("= %d", p->value);
if (p->S2 != NULL) {
printf("= %d", p->S2->value);
}
printf("\n");
// ASTprint(level, p->S1);
break;
case A_METHODDEC:
printf("Method '%s' with type ", p->name);
printf("METHOD FUNCTION '%s' with type ", p->name);
AST_Print_Type(p->A_Declared_Type);
/* print out the parameter list */
if (p->S1 == NULL) {
printf("\n");
PT(level + 2);
printf(" (NONE) ");
} else {
printf("\n");
PT(level + 2);
printf("( ");
printf("(\n");
ASTprint(level + 2, p->S1);
PT(level + 2);
printf(" ) ");
printf(")");
}
printf("\n");
ASTprint(level + 2, p->S2); // print out the block
@@ -130,7 +149,7 @@ void ASTprint(int level, ASTnode *p)
printf("Method Variable");
printf(" %s", p->name);
AST_Print_Type(p->A_Declared_Type);
// printf("\n");
printf("\n");
break;
case A_PARAM:
@@ -156,10 +175,57 @@ void ASTprint(int level, ASTnode *p)
case A_MINUS:
printf(" - ");
break;
case A_DIVIDE:
printf(" / ");
break;
case A_EQ:
printf(" == ");
break;
case A_LEQ:
printf(" <= ");
break;
case A_GEQ:
printf(" >= ");
break;
case A_GT:
printf(" > ");
break;
case A_LT:
printf(" < ");
break;
case A_LEFTSHIFT:
printf(" << ");
break;
case A_RIGHTSHIFT:
printf(" >> ");
break;
case A_MOD:
printf(" %% ");
break;
case A_AND:
printf(" && ");
break;
case A_OR:
printf(" || ");
break;
case A_NEQ:
printf(" != ");
break;
case A_TIMES:
printf(" * ");
break;
case A_NOT:
printf(" !");
break;
case A_UMINUS:
printf(" Unary Minus");
break;
default:
printf("Unknown EXPR Operator");
}
printf("\n");
ASTprint(level + 1, p->S1);
if (p->operator!= A_NOT)
if (p->operator!= A_NOT && p->operator != A_UMINUS)
ASTprint(level + 1, p->S2);
break;
case A_BLOCK:
@@ -172,10 +238,59 @@ void ASTprint(int level, ASTnode *p)
ASTprint(level + 1, p->S1);
ASTprint(level + 2, p->S2);
break;
case A_NUMBER:
printf("NUMBER with value %d\n", p->value);
case A_CONSTANT_INT:
printf("CONSTANT INTEGER %d\n", p->value);
break;
case A_CONSTANT_STRING:
printf("CONSTANT STRING %s\n", p->name);
break;
case A_CONSTANT_BOOL:
printf("CONSTANT BOOLEAN ");
if (p->value == 1) {
printf("true\n");
} else {
printf("false\n");
}
break;
case A_BREAK:
printf("BREAK STATEMENT \n");
break;
case A_RETURN:
printf("RETURN STATEMENT \n");
ASTprint(level + 1, p->S1);
break;
case A_ASSIGN:
printf("ASSIGNMENT STATEMENT\n");
ASTprint(level + 1, p->S1);
ASTprint(level + 1, p->S2);
break;
case A_IF:
printf("IF STATEMENT \n");
ASTprint(level + 1, p->S1);
ASTprint(level + 1, p->S2);
break;
case A_IFBODY:
// printf("IF BODY \n");
ASTprint(level + 1, p->S1);
ASTprint(level + 1, p->S2);
break;
case A_ELSE:
printf("ELSE \n");
ASTprint(level + 1, p->S1);
break;
case A_METHODCALL:
printf(" METHOD CALL name: %s\n", p->name);
PT(level);
printf("(\n");
printf("METHOD ARG \n");
ASTprint(level + 1, p->S1);
PT(level);
printf(")\n");
break;
default:
printf("unknown type in ASTprint\n");
}
+23 -1
View File
@@ -32,7 +32,16 @@ enum AST_Tree_Element_Type {
A_METHODID,
A_CONSTANT_INT,
A_CONSTANT_BOOL,
A_CONSTANT_STRING
A_CONSTANT_STRING,
A_BREAK,
A_VAR_RVALUE,
A_RETURN,
A_ASSIGN,
A_IF,
A_IFBODY,
A_ELSE,
A_METHODCALL,
A_CONTINUE
// missing
@@ -41,7 +50,20 @@ enum AST_Tree_Element_Type {
enum AST_Operators {
A_PLUS,
A_MINUS,
A_UMINUS,
A_TIMES,
A_DIVIDE,
A_MOD,
A_AND,
A_OR,
A_LEFTSHIFT,
A_RIGHTSHIFT,
A_LT,
A_GT,
A_EQ,
A_LEQ,
A_GEQ,
A_NEQ,
A_NOT
// missing
};
+2 -9
View File
@@ -78,15 +78,13 @@ CHANGES:
"bool" {return(T_BOOLTYPE);}
"break" {return(T_BREAK);}
"continue" {return(T_CONTINUE);}
\. {return(T_DOT);}
"else" {return(T_ELSE);}
"==" {return(T_EQ);}
"extern" {return(T_EXTERN);}
"false" {return(T_FALSE);}
"for" {return(T_FOR);}
"func" {return(T_FUNC);}
">=" {return(T_GEQ);}
">" {return(T_GT);}
"if" {if (mydebug) fprintf(stderr,"T_IF found\n");return(T_IF);}
@@ -99,7 +97,6 @@ CHANGES:
"<<" {return(T_LEFTSHIFT);}
"<=" {return(T_LEQ);}
"!=" {return(T_NEQ);}
"null" {return(T_NULL);}
"||" {return(T_OR);}
"package" {return(T_PACKAGE);}
"return" {return(T_RETURN);}
@@ -110,10 +107,6 @@ CHANGES:
"void" {if (mydebug) fprintf(stderr,"T_VOID found\n");return(T_VOID);}
"while" {return(T_WHILE);}
\'.\' {
yylval.value=yytext[1];
return(T_CHARCONSTANT);
}
\".*\" {
yylval.string=strdup(yytext);
@@ -145,7 +138,7 @@ CHANGES:
if (mydebug) fprintf(stderr,"Whitespace found\n");
}
[\-+*/%();{},\[\]!<] {
[\-+*/%();{},\[\]!<>] {
if (mydebug) fprintf(stderr,"return a token %c\n",*yytext);
return (*yytext);
}
+281 -210
View File
@@ -37,6 +37,7 @@ from LEX and how the operators are associated */
char* string;
struct ASTnodetype *astnode;
enum AST_Decaf_Types asttype;
enum AST_Operators operator;
}
// Added a token for each of the lex tokens
@@ -44,17 +45,17 @@ from LEX and how the operators are associated */
%token T_ASSIGN
%token T_BOOLTYPE
%token T_BREAK
%token T_CHARCONSTANT
/* %token T_CHARCONSTANT */
%token T_CONTINUE
%token T_DOT
/* %token T_DOT */
%token T_ELSE
%token T_EQ
%token T_EXTERN
%token T_FALSE
%token T_FOR
/* %token T_FOR */
%token T_FUNC
%token T_GEQ
%token T_GT
/* %token T_GT */
%token <string> T_ID
%token T_IF
%token <value> T_INTCONSTANT
@@ -62,7 +63,7 @@ from LEX and how the operators are associated */
%token T_LEFTSHIFT
%token T_LEQ
%token T_NEQ
%token T_NULL
/* %token T_NULL */
%token T_OR
%token T_PACKAGE
%token T_RETURN
@@ -79,48 +80,15 @@ from LEX and how the operators are associated */
%type <astnode> Externs ExternDefn FullExternParmList ExternParmList FieldDecl
%type <astnode> FieldDecls ArrayType MethodDecl MethodDecls Block VarDecl
%type <astnode> IdTypeList IdTypeList1 VarDecls
%type <astnode> IdTypeList IdTypeList1 VarDecls Statement Statements ReturnStmt
%type <astnode> Constant BoolConstant BreakStmt Additiveexpression Term
%type <astnode> Factor Simpleexpression Expr Assign Lvalue IfStmt WhileStmt
%type <astnode> MethodCall MethodArgs FullMethodArgs MethodArg ContinueStmt
%type <asttype> MethodType Type ExternType
%type <value> Constant BoolConstant
/* %type <astnode> ExternParmList
%type <astnode> FieldDecls
%type <astnode> MethodDecls
%type <astnode> IdTypeList
%type <astnode> VarDecls
%type <astnode> Statements
%type <astnode> MethodArgs
%type <astnode> Program
%type <astnode> ExternDefn
%type <astnode> FullExternParmList
%type <astnode> FieldDecl
%type <astnode> MethodDecl
%type <astnode> Block
%type <astnode> VarDecl
%type <astnode> Statement
%type <operator> Multop Addop Relop
%type <astnode> Assign
%type <astnode> Assigns
%type <astnode> Lvalue
%type <astnode> MethodCall
%type <astnode> MethodArg
%type <astnode> FullMethodArgs
%type <astnode> Expr
%type <astnode> Simpleexpression
%type <astnode> Relop
%type <astnode> Additiveexpression
%type <astnode> Addop
%type <astnode> Term
%type <astnode> Multop
%type <astnode> Factor
%type <astnode> ExternType
%type <astnode> Type
%type <astnode> MethodType
%type <astnode> BoolConstant
%type <astnode> ArrayType
%type <astnode> Constant */
/* added all the rules from DECAF and reformatted them to work with Bison */
/* for each of the rules with a list of tokens I created a recursive rule
that allows for one or more of the token.
*/
@@ -128,22 +96,19 @@ that allows for one or more of the token.
/* Replaced any instance of '=' with T_ASSIGN */
%% /* end specs, begin rules */
Program: Externs T_PACKAGE T_ID '{' FieldDecls MethodDecls '}'
{
PROGRAM = ASTCreateNode(A_PROGRAM);
PROGRAM->S1 = $1;
PROGRAM->S2 = ASTCreateNode(A_PACKAGE);
PROGRAM->S2->name = $3;
PROGRAM->S2->S1 = $5; // TODO: Field Decls $5
PROGRAM->S2->S2 = $6; // TODO: Method Decls $6
Program:
Externs T_PACKAGE T_ID '{' FieldDecls MethodDecls '}' {
PROGRAM = ASTCreateNode(A_PROGRAM);
PROGRAM->S1 = $1;
PROGRAM->S2 = ASTCreateNode(A_PACKAGE);
PROGRAM->S2->name = $3;
PROGRAM->S2->S1 = $5;
PROGRAM->S2->S2 = $6;
};
}
;
Externs: /* empty */ { $$ = NULL; }
| ExternDefn Externs
{ $1->next = $2; }
;
Externs:
/* empty */ { $$ = NULL; }
| ExternDefn Externs { $1->next = $2; };
ExternDefn:
T_EXTERN T_FUNC T_ID '(' ExternParmList ')' MethodType ';' {
@@ -153,10 +118,9 @@ ExternDefn:
$$->S1 = $5;
};
ExternParmList: /* empty */ { $$ = NULL; }
| FullExternParmList {$$ = $1;}
;
ExternParmList:
/* empty */ { $$ = NULL; }
| FullExternParmList {$$ = $1;} ;
FullExternParmList:
ExternType {
@@ -169,47 +133,44 @@ FullExternParmList:
$$->next = $3;
};
FieldDecls: /* empty */ {$$ = NULL;}
| FieldDecl FieldDecls
{
FieldDecls:
/* empty */ {$$ = NULL;}
| FieldDecl FieldDecls {
$$ = $1;
$$->next = $2;
}
;
};
FieldDecl: T_VAR T_ID Type ';'
{
$$ = ASTCreateNode(A_VARDEC);
$$->name = $2;
$$->A_Declared_Type = $3;
FieldDecl:
T_VAR T_ID Type ';' {
$$ = ASTCreateNode(A_VARDEC);
$$->name = $2;
$$->A_Declared_Type = $3;
}
| T_VAR T_ID ArrayType ';'
{
$$ = ASTCreateNode(A_VARDEC);
$$->name = $2;
$$->S1 = $3;
$$->A_Declared_Type = $3->A_Declared_Type;
}
| T_VAR T_ID ArrayType ';' {
$$ = ASTCreateNode(A_VARDEC);
$$->name = $2;
$$->S1 = $3;
$$->A_Declared_Type = $3->A_Declared_Type;
}
| T_VAR T_ID Type T_ASSIGN Constant ';'
{
$$ = ASTCreateNode(A_VARDEC);
$$->name = $2;
$$->A_Declared_Type = $3;
$$->value = $5;
}
| T_VAR T_ID Type T_ASSIGN Constant ';' {
$$ = ASTCreateNode(A_VARDEC);
$$->name = $2;
$$->A_Declared_Type = $3;
$$->S2 = $5;
}
;
};
MethodDecls: /* empty */ {$$ = NULL;}
| MethodDecl MethodDecls {
$$ = $1;
$$->next = $2;
}
;
MethodDecls:
/* empty */ {$$ = NULL;}
| MethodDecl MethodDecls {
$$ = $1;
$$->next = $2;
};
MethodDecl: T_FUNC T_ID '(' IdTypeList ')' MethodType Block {
MethodDecl:
T_FUNC T_ID '(' IdTypeList ')' MethodType Block {
$$ = ASTCreateNode(A_METHODDEC);
$$->name = $2;
$$->A_Declared_Type = $6;
@@ -217,11 +178,12 @@ MethodDecl: T_FUNC T_ID '(' IdTypeList ')' MethodType Block {
$$->S2 = $7; // $7;
};
IdTypeList: /* empty */ { $$ = NULL; }
| IdTypeList1 { $$ = $1; }
;
IdTypeList:
/* empty */ { $$ = NULL; }
| IdTypeList1 { $$ = $1; };
IdTypeList1: T_ID Type {
IdTypeList1:
T_ID Type {
$$ = ASTCreateNode(A_METHODID);
$$->name = $1;
$$->A_Declared_Type = $2;
@@ -233,18 +195,22 @@ IdTypeList1: T_ID Type {
$$->next = $4;
};
Block: '{' VarDecls Statements '}' {
Block:
'{' VarDecls Statements '}' {
$$ = ASTCreateNode(A_BLOCK);
$$->S1 = $2;
$$->S2 = $3;
};
VarDecls: /* empty */ { $$ = NULL; }
VarDecls:
/* empty */ { $$ = NULL; }
| VarDecl VarDecls {
$$ = $1;
$$->next = $2;
};
VarDecl: T_VAR T_ID Type ';' {
VarDecl:
T_VAR T_ID Type ';' {
$$ = ASTCreateNode(A_VARDEC);
$$->name = $2;
$$->A_Declared_Type = $3;
@@ -256,49 +222,111 @@ VarDecl: T_VAR T_ID Type ';' {
$$->A_Declared_Type = $$->S1->A_Declared_Type;
};
Statements: /* empty */
| Statement Statements
Statements:
/* empty */ { $$ = NULL; }
| Statement Statements {
$$ = $1;
$$->next = $2;
};
Statement:
Block { $$ = $1; }
| Assign ';' { $$ = $1; }
| MethodCall ';' { $$ = $1; }
| IfStmt { $$ = $1; }
| WhileStmt { $$ = $1; }
| ReturnStmt { $$ = $1; }
| BreakStmt { $$ = $1; }
| ContinueStmt { $$ = $1; }
;
Statement: Block
| Assign ';'
| MethodCall ';'
| T_IF '(' Expr ')' Block T_ELSE Block
| T_IF '(' Expr ')' Block
| T_WHILE '(' Expr ')' Block
| T_RETURN ';'
| T_RETURN '(' ')' ';'
| T_RETURN '(' Expr ')' ';'
| T_BREAK ';'
| T_CONTINUE ';'
;
ContinueStmt: T_CONTINUE ';' {$$ = ASTCreateNode(A_CONTINUE);};
WhileStmt:
T_WHILE '(' Expr ')' Block {
$$ = ASTCreateNode(A_WHILESTMT);
$$->S1 = $3;
$$->S2 = $5;
};
BreakStmt: T_BREAK ';' { $$ = ASTCreateNode(A_BREAK); };
IfStmt:
T_IF '(' Expr ')' Block T_ELSE Block {
$$ = ASTCreateNode(A_IF);
$$->S1 = $3;
$$->S2 = ASTCreateNode(A_IFBODY);
$$->S2->S1 = $5;
$$->S2->S2 = ASTCreateNode(A_ELSE);
$$->S2->S2->S1 = $7;
}
| T_IF '(' Expr ')' Block {
$$ = ASTCreateNode(A_IF);
$$->S1 = $3;
$$->S2 = ASTCreateNode(A_IFBODY);
$$->S2->S1 = $5;
$$->S2->S2 = NULL;
}
;
ReturnStmt:
T_RETURN ';' { $$ = ASTCreateNode(A_RETURN);}
| T_RETURN '(' ')' ';' { $$ = ASTCreateNode(A_RETURN);}
| T_RETURN '(' Expr ')' ';' {
$$ = ASTCreateNode(A_RETURN);
$$->S1 = $3;
};
Assign:
Lvalue T_ASSIGN Expr {
$$ = ASTCreateNode(A_ASSIGN);
$$->S1 = $1;
$$->S2 = $3;
};
Assign: Lvalue T_ASSIGN Expr
;
/* Assigns: Assign
| Assign ',' Assigns
; */
Lvalue: T_ID
| T_ID '[' Expr ']'
;
Lvalue:
T_ID {
$$ = ASTCreateNode(A_VAR_RVALUE);
$$->name = $1;
}
| T_ID '[' Expr ']' {
$$ = ASTCreateNode(A_VAR_RVALUE);
$$->name = $1;
$$->S1 = $3;
};
MethodCall: T_ID '(' MethodArgs ')'
;
MethodCall:
T_ID '(' MethodArgs ')' {
$$ = ASTCreateNode(A_METHODCALL);
$$->name = $1;
$$->S1 = $3;
};
MethodArg: Expr
;
MethodArg: Expr { $$ = $1; };
MethodArgs: /* empty */
| FullMethodArgs
;
MethodArgs:
/* empty */ {
$$ = NULL;
}
| FullMethodArgs {
$$ = $1;
};
FullMethodArgs: MethodArg
| MethodArg ',' MethodArgs
;
FullMethodArgs:
MethodArg {
$$ = $1;
}
| MethodArg ',' MethodArgs {
$$ = $1;
$$->next = $3;
}
;
@@ -310,103 +338,146 @@ FullMethodArgs: MethodArg
Expr: Simpleexpression
;
Simpleexpression: Additiveexpression
| Simpleexpression Relop Additiveexpression
;
Expr: Simpleexpression { $$ = $1; };
Relop: T_LEQ | '<' | T_GT | T_GEQ | T_EQ | T_NEQ
;
Simpleexpression:
Additiveexpression { $$ = $1; }
| Simpleexpression Relop Additiveexpression {
$$ = ASTCreateNode(A_EXPR);
$$->S1 = $1;
$$->operator = $2;
$$->S2 = $3;
};
Additiveexpression: Term
| Additiveexpression Addop Term
;
Relop:
'<' { $$ = A_LT; }
| '>' { $$ = A_GT; }
| T_EQ { $$ = A_EQ; }
| T_LEQ { $$ = A_LEQ; }
| T_GEQ { $$ = A_GEQ; }
| T_NEQ { $$ = A_NEQ; };
Addop: '+' | '-'
Additiveexpression:
Term { $$ = $1; }
| Additiveexpression Addop Term {
$$ = ASTCreateNode(A_EXPR);
$$->S1 = $1;
$$->operator = $2;
$$->S2 = $3;
};
Addop:
'+' { $$ = A_PLUS; }
| '-' { $$ = A_MINUS; };
Term: Factor
| Term Multop Factor
;
Term:
Factor { $$ = $1; }
| Term Multop Factor {
$$ = ASTCreateNode(A_EXPR);
$$->S1 = $1;
$$->operator = $2;
$$->S2 = $3;
};
/* added modulus as a multi operator */
Multop: '*' | '/' | '%' | T_AND | T_OR | T_LEFTSHIFT | T_RIGHTSHIFT
;
/*
A_PLUS,
A_MINUS,
A_TIMES,
A_DIVIDE,
A_MOD,
A_AND,
A_OR,
A_LEFTSHIFT,
A_RIGHTSHIFT,
A_NOT
*/
Multop:
'*' { $$ = A_TIMES; }
| '/' { $$ = A_DIVIDE; }
| '%' { $$ = A_MOD; }
| T_AND { $$ = A_AND; }
| T_OR { $$ = A_OR; }
| T_LEFTSHIFT { $$ = A_LEFTSHIFT; }
| T_RIGHTSHIFT { $$ = A_RIGHTSHIFT; };
Factor: T_ID
| MethodCall
| T_ID '[' Expr ']'
| Constant
| '(' Expr ')'
| '!' Factor
| '-' Factor
;
Factor:
T_ID {
$$ = ASTCreateNode(A_VAR_RVALUE);
$$->name = $1;
}
| MethodCall { $$ = $1; }
| T_ID '[' Expr ']' {
$$ = ASTCreateNode(A_VAR_RVALUE);
$$->name = $1;
$$->S1 = $3;
}
| Constant { $$ = $1; }
| '(' Expr ')' { $$ = $2; }
| '!' Factor {
$$ = ASTCreateNode(A_EXPR);
$$->operator = A_NOT;
$$->S1 = $2;
}
| T_STRINGCONSTANT {
$$ = ASTCreateNode(A_CONSTANT_STRING);
$$->name = $1;
}
| '-' Factor {
$$ = ASTCreateNode(A_EXPR);
$$->operator = A_UMINUS;
$$->S1 = $2;
};
ExternType: T_STRINGTYPE { $$ = A_Decaf_STRING; }
| Type
;
ExternType:
T_STRINGTYPE { $$ = A_Decaf_STRING; }
| Type
;
Type: T_INTTYPE
{ $$ = A_Decaf_INT;}
| T_BOOLTYPE
{ $$ = A_Decaf_BOOL;}
;
Type:
T_INTTYPE { $$ = A_Decaf_INT; }
| T_BOOLTYPE{ $$ = A_Decaf_BOOL; };
MethodType: T_VOID { $$ = A_Decaf_VOID; }
| Type { $$ = $1;}
;
MethodType:
T_VOID { $$ = A_Decaf_VOID; }
| Type { $$ = $1; };
BoolConstant: T_TRUE {
$$ = 1;
}
| T_FALSE {
$$ = 0;
}
;
BoolConstant:
T_TRUE {
$$ = ASTCreateNode(A_CONSTANT_BOOL);
$$->value = 1;
}
| T_FALSE {
$$ = ASTCreateNode(A_CONSTANT_BOOL);
$$->value = 0;
};
ArrayType: '[' T_INTCONSTANT ']' Type {
$$ = ASTCreateNode(A_ARRAYTYPE);
$$->value = $2;
$$->A_Declared_Type = $4;
}
ArrayType:
'[' T_INTCONSTANT ']' Type {
$$ = ASTCreateNode(A_ARRAYTYPE);
$$->value = $2;
$$->A_Declared_Type = $4;
}
;
/* added String and Char as constants */
Constant: T_INTCONSTANT {
$$ = $1;
}
/* | T_STRINGCONSTANT {
$$ = $1;
} */
Constant:
T_INTCONSTANT {
$$ = ASTCreateNode(A_CONSTANT_INT);
$$->value = $1;
}
/* | T_CHARCONSTANT {
$$ = $1;
} */
| BoolConstant {
$$ = $1;
}
;
| BoolConstant {
$$ = $1;
};
%% /* end of rules, start of program */
int main(){
yyparse();
printf("parsing done\n");
printf("Parsing completed\n");
// We know PROGRAM points to our AST
ASTprint(1, PROGRAM);
}
ASTprint(0, PROGRAM);
printf("Finished printing AST ");
}
+24 -15
View File
@@ -1,20 +1,29 @@
extern func print_int (int, bool) void;
extern func read_int (int, string) int;
// extern func read_str (string) int;
// extern func read_int (string) string;
extern func print_int (int) int;
package foo {
var x int;
var y_me int = 10;
var A[100] bool;
func main ( a int, y bool) void
{
var x int;
var B[100] int;
var x int = 3;
var Y bool = true;
var A [100] int;
func foo (x int, y bool) int {
var x int ;
var A[100] bool;
break;
x = y + z[A];
x = true;
y = false;
if ( x> y) { x = 3+ 4;}
else
{ x = y;}
return;
return();
return( x << y >> z);
while (true != false)
{ x = 3 && 2;
print_string("hello world");
x = f( g ( ! (x && y || z)));
}
} // end of foo
}
} // end of foo
-5
View File
@@ -2,15 +2,10 @@ Parsing completed
EXTERN FUNC print_int
EXTERN Type: INT
END EXTERN with Type: INT
Package : foo
Variable x with type INT = 3
Variable Y with type BOOLEAN = true
Variable A [100] with type INT
METHOD FUNCTION foo INT
(
PARAMETER INT x
+864 -790
View File
File diff suppressed because it is too large. Load diff
+87
View File
@@ -0,0 +1,87 @@
Parsing completed
EXTERN FUNC print_int
EXTERN Type INT
END EXTERN with Type:
INT Package : foo
Variable x with type INT = 3
Variable Y with type BOOLEAN = 1
Variable A[100] with type INT
METHOD FUNCTION 'foo' with type INT
(
Method Variable x INT
Method Variable y BOOLEAN
)
BLOCK STATEMENT
Variable x with type INT
Variable A[100] with type BOOLEAN
BREAK STATEMENT
ASSIGNMENT STATEMENT
Variable x
EXPR +
Variable y
Variable z
[
Variable A
]
ASSIGNMENT STATEMENT
Variable x
CONSTANT BOOLEAN true
ASSIGNMENT STATEMENT
Variable y
CONSTANT BOOLEAN false
IF STATEMENT
EXPR >
Variable x
Variable y
BLOCK STATEMENT
ASSIGNMENT STATEMENT
Variable x
EXPR +
CONSTANT INTEGER 3
CONSTANT INTEGER 4
ELSE
BLOCK STATEMENT
ASSIGNMENT STATEMENT
Variable x
Variable y
RETURN STATEMENT
RETURN STATEMENT
RETURN STATEMENT
EXPR >>
EXPR <<
Variable x
Variable y
Variable z
WHILE STATEMENT
EXPR !=
CONSTANT BOOLEAN true
CONSTANT BOOLEAN false
BLOCK STATEMENT
ASSIGNMENT STATEMENT
Variable x
EXPR &&
CONSTANT INTEGER 3
CONSTANT INTEGER 2
METHOD CALL name: print_string
(
METHOD ARG
CONSTANT STRING "hello world"
)
ASSIGNMENT STATEMENT
Variable x
METHOD CALL name: f
(
METHOD ARG
METHOD CALL name: g
(
METHOD ARG
EXPR !
EXPR ||
EXPR &&
Variable x
Variable y
Variable z
)
)
Finished printing AST