/* * * File : chiaa.yacc * Last Modified : 14 December 2002 * * */ %{ #include typedef enum {STR, NUMBER, DUMMY, LOGOPER, NUMOPER, ASSIGNOPER, TYPE, RESWORD} myType; typedef union{ char *c; int i; }types; typedef struct tnode{ myType type; types info; struct tnode *left; struct tnode *right; } typenode, *typeptr; static int count = 1; static int label = 1; typeptr empty_node(); typeptr new_node(char *info, myType t); typeptr new_int_node (int info); typeptr new_dummy_node(); typeptr join_child_node(typeptr t1, typeptr t2, typeptr t3); void print_tree(typeptr t); void generate(typeptr t); %} %union{ typeptr tNode; char *cVal; int iVal; } %type program method_declaration type variable_type statement_block %type statement simple_statement assignment_statement declarative_statement declarative_statement2 %type expr or_expr and_expr relop_expr ltgt_expr addop_expr mulop_expr term value %type compound_statement if_statement loop_statement while_statement dowhile_statement for_statement %type for_expr for_expr2 %token VOID INT NAME ASSIGNOP OR AND NOT RELOP LTGT ADDOP MULOP %token IF FOR WHILE DO %token NUMBER %token LPAREN RPAREN OBRACE EBRACE COMMA SEMICOLON %nonassoc IFX %nonassoc ELSE %% program : method_declaration { printf("Abstract Syntax Tree:\n("); print_tree($1); printf(")\n"); printf("\nTotal No Of Lines:%d\n\n",yylineno); printf("Code Generated:\n"); generate($1); } ; method_declaration : type NAME LPAREN RPAREN OBRACE statement_block EBRACE { $1 = join_child_node($1,new_node($2,STR),NULL); $$ = join_child_node(new_dummy_node(),$1, $6); } ; type : VOID {$$= new_node($1,TYPE); } | variable_type {$$=$1; } ; variable_type : INT {$$= new_node($1,TYPE); } ; statement_block : /*empty*/ {$$ = NULL;} | statement statement_block {$$ = join_child_node(new_dummy_node(),$1,$2);} ; statement : simple_statement SEMICOLON {$$ = $1; } | compound_statement {$$ = $1; } | OBRACE statement_block EBRACE {$$ = $2; } ; simple_statement : assignment_statement {$$= $1; } | declarative_statement {$$= $1; } ; declarative_statement : variable_type assignment_statement declarative_statement2 {$$ = join_child_node($1,$2,$3); } ; declarative_statement2 : /*empty*/ {$$ = NULL; } | COMMA assignment_statement declarative_statement2 {$$ = join_child_node(new_dummy_node(),$2,$3); } ; assignment_statement : NAME {$$ = new_node($1,STR); } | NAME ASSIGNOP expr {$$ = join_child_node(new_node($2,ASSIGNOPER),new_node($1,STR),$3); } ; expr : or_expr {$$ =$1; } ; or_expr : or_expr OR and_expr {$$ = join_child_node(new_node($2,LOGOPER),$1,$3);} | and_expr {$$ =$1; } ; and_expr : and_expr AND relop_expr {$$ = join_child_node(new_node($2,LOGOPER),$1,$3);} | relop_expr {$$ =$1; } ; relop_expr : relop_expr RELOP ltgt_expr {$$ = join_child_node(new_node($2,LOGOPER),$1,$3);} | ltgt_expr {$$ =$1; } ; ltgt_expr : ltgt_expr LTGT addop_expr {$$ = join_child_node(new_node($2,LOGOPER),$1,$3);} | addop_expr {$$ =$1; } ; addop_expr : addop_expr ADDOP mulop_expr {$$ = join_child_node(new_node($2,NUMOPER),$1,$3);} | mulop_expr {$$= $1; } ; mulop_expr : mulop_expr MULOP term {$$ = join_child_node(new_node($2,NUMOPER),$1,$3);} | term {$$= $1; } ; term : NOT value {$$ = join_child_node(new_node($1,LOGOPER),$2,NULL);} | ADDOP value {$$ = join_child_node(new_node($1,NUMOPER),$2,NULL);} | value {$$= $1; } ; value : NAME {$$ = new_node($1,STR);} | NUMBER {$$ = new_int_node($1);} | LPAREN expr RPAREN {$$ = $2; } ; compound_statement : if_statement { $$ = $1; } | loop_statement { $$ = $1; } ; if_statement : IF LPAREN expr RPAREN statement %prec IFX {$$=join_child_node(new_node($1,RESWORD), $3, $5);} | IF LPAREN expr RPAREN statement ELSE statement { typeptr temp = join_child_node(new_dummy_node(),$5,$7); $$= join_child_node(new_node($1,RESWORD),$3,temp); } ; loop_statement : while_statement {$$=$1; } | dowhile_statement {$$=$1; } | for_statement {$$=$1; } ; while_statement : WHILE LPAREN expr RPAREN statement {$$=join_child_node(new_node($1,RESWORD),$3,$5);} ; dowhile_statement : DO statement WHILE LPAREN expr RPAREN SEMICOLON { //typeptr temp = join_child_node(new_node($3,RESWORD),$5,NULL); $$=join_child_node(new_node($1,RESWORD),$5,$2); } ; for_statement : FOR LPAREN SEMICOLON SEMICOLON RPAREN statement {$$ = join_child_node(new_node($1,RESWORD),new_dummy_node(),$6); } | FOR LPAREN for_expr SEMICOLON SEMICOLON RPAREN statement { typeptr temp = join_child_node(new_dummy_node(),$3,NULL); $$ = join_child_node(new_node($1,RESWORD),temp,$7); } | FOR LPAREN SEMICOLON expr SEMICOLON RPAREN statement { typeptr temp1 = join_child_node(new_dummy_node(),$4,NULL); typeptr temp2 = join_child_node(new_dummy_node(),NULL,temp1); $$ = join_child_node(new_node($1,RESWORD),temp2,$7); } | FOR LPAREN SEMICOLON SEMICOLON for_expr RPAREN statement { typeptr temp1 = join_child_node(new_dummy_node(),NULL,$5); typeptr temp2 = join_child_node(new_dummy_node(),NULL,temp1); $$= join_child_node(new_node($1,RESWORD),temp2,$7); } | FOR LPAREN for_expr SEMICOLON expr SEMICOLON RPAREN statement { typeptr temp1 = join_child_node(new_dummy_node(),$5,NULL); typeptr temp2 = join_child_node(new_dummy_node(),$3,temp1); $$= join_child_node(new_node($1,RESWORD),temp2,$8); } | FOR LPAREN for_expr SEMICOLON SEMICOLON for_expr RPAREN statement { typeptr temp1 = join_child_node(new_dummy_node(),NULL,$6); typeptr temp2 = join_child_node(new_dummy_node(),$3,temp1); $$= join_child_node(new_node($1,RESWORD),temp2,$8); } | FOR LPAREN SEMICOLON expr SEMICOLON for_expr RPAREN statement { typeptr temp1 = join_child_node(new_dummy_node(),$4,$6); typeptr temp2 = join_child_node(new_dummy_node(),NULL,temp1); $$= join_child_node(new_node($1,RESWORD),temp2,$8); } | FOR LPAREN for_expr SEMICOLON expr SEMICOLON for_expr RPAREN statement { typeptr temp1 = join_child_node(new_dummy_node(),$5,$7); typeptr temp2 = join_child_node(new_dummy_node(),$3,temp1); $$= join_child_node(new_node($1,RESWORD),temp2,$9); } ; for_expr : declarative_statement {$$= $1; } | assignment_statement for_expr2 {$$= join_child_node(new_dummy_node(),$1,$2);} ; for_expr2 : /*empty*/ {$$=NULL; } | COMMA assignment_statement for_expr2 {$$= join_child_node(new_dummy_node(),$2,$3);} ; %% #include #include #include "lex.yy.c" #include "chiaa.c" main(){ yyparse(); } yyerror(char *s) { fprintf(stderr, "Line: %d ;%s\n",yylineno,s); }