Friday 15 March 2013

c - yacc gets zero value from non terminal -



c - yacc gets zero value from non terminal -

i making c- compiler next [compiler construction, principles , practice]. , have made parser using yacc 1 rule in yacc making error.

return_stmt : homecoming semi { $$ = newstmtnode(returnk);} | homecoming look semi { $$ = newstmtnode(returnk); $$->child[0] = $1; }

when parsing homecoming statement using "return look semi" rule $1 has value zero. whole source below %% /* grammar c- */

program : declaration_list { savedtree = $1; } ; declaration_list : declaration_list declaration { yystype t = $1; if (t != null) { while (t->sibling != null) t = t->sibling; t->sibling = $2; $$ = $1; } else $$ = $2; } | declaration { $$ =$1; } ; declaration : var_declaration { $$ = $1; } | fun_declaration { $$ = $1; } ; var_declaration : type_specifier id semi { $$ = newexpnode(vark); $$->type = (exptype)$1; $$->attr.name = namestackpop(); $$->lineno = savedlineno; } | type_specifier id lbrace num rbrace semi { $$ = newexpnode(vararrayk); $$->type = (exptype)$1; $$->attr.name = namestackpop(); $$->lineno = savedlineno; $$->child[0] = $4; } ; type_specifier : int { $$ = integer; } | void { $$ = void; } ; fun_declaration : type_specifier id lparen params rparen compound_stmt { $$ = newstmtnode(functionk); $$->type = (exptype)$1; $$->attr.name = namestackpop(); $$->lineno = savedlineno; $$->child[0] = $4; $$->child[1] = $6; } ; params : param_list { $$ = $1; } | void { $$ = newexpnode(singleparamk); $$->type = void; } ; param_list : param_list comma param { yystype t = $1; if (t != null) { while (t->sibling != null) t = t->sibling; t->sibling = $3; $$ = $1; } else $$ = $3; } | param { $$ = $1; } ; param : type_specifier id { $$ = newexpnode(singleparamk); $$->type = (exptype)$1; $$->attr.name = namestackpop(); } | type_specifier id lbrace rbrace { $$ = newexpnode(arrayparamk); $$->type = (exptype)$1; $$->attr.name = namestackpop(); $$->lineno = savedlineno; } ; compound_stmt : lcurly local_declarations statement_list rcurly { $$ = newstmtnode(compoundk); $$->child[0] = $2; $$->child[1] = $3; } ; local_declarations : local_declarations var_declaration { yystype t = $1; if (t != null) { while (t->sibling != null) t = t->sibling; t->sibling = $2; $$ = $1; } else $$ = $2; } | empty { $$ = $1; } ; statement_list : statement_list statement { yystype t = $1; if (t != null) { while (t->sibling != null) t = t->sibling; t->sibling = $2; $$ = $1; } else $$ = $2; } | empty { $$ = $1; } ; statement : expression_stmt { $$ = $1; } | compound_stmt { $$ = $1; } | selection_stmt { $$ = $1; } | iteration_stmt { $$ = $1; } | return_stmt { $$ = $1; } ; expression_stmt : look semi { $$ = $1;} | semi { $$ = null; } ; selection_stmt : if lparen look rparen statement { $$ = newstmtnode(ifk); $$->child[0] = $3; $$->child[1] = $5; $$->attr.withelse = false; } | if lparen look rparen statement else statement { $$ = newstmtnode(ifk); $$->child[0] = $3; $$->child[1] = $5; $$->child[2] = $7; $$->attr.withelse = true; } ; iteration_stmt : while lparen look rparen statement { $$ = newstmtnode(whilek); $$->child[0] = $3; $$->child[1] = $5; } ; return_stmt : homecoming semi { $$ = newstmtnode(returnk);} | homecoming look semi { $$ = newstmtnode(returnk); $$->child[0] = $1; } ; look : var assign look { $$ = newexpnode(assignk); $$->type = integer; $$->child[0] = $1; $$->child[1] = $3; } | simple_expression { $$ = $1; } ; var : id { $$ = newexpnode(idk); $$->type = integer; $$->attr.name = namestackpop(); } | id lbrace look rbrace { $$ = newexpnode(idk); $$->attr.name = namestackpop(); $$->child[0] = $3; } ; simple_expression : additive_expression relop additive_expression { $$ = newexpnode(opk); $$->type = integer; $$->child[0] = $1; $$->child[1] = $3; $$->attr.op = $2; } | additive_expression { $$ = $1; } ; relop : le { $$ = le; } | lt { $$ = lt; } | gt { $$ = gt; } | ge { $$ = ge; } | eq { $$ = eq; } | ne { $$ = ne; } ; additive_expression : additive_expression addop term { $$ = newexpnode(opk); $$->type = integer; $$->child[0] = $1; $$->child[1] = $3; $$->attr.op = $2; } | term { $$ = $1; } ; addop : plus { $$ = plus; } | minus { $$ = minus; } ; term : term mulop factor { $$ = newexpnode(opk); $$->type = integer; $$->child[0] = $1; $$->child[1] = $3; $$->attr.op = $2; } | factor { $$ = $1; } ; mulop : times { $$ = times; } | on { $$ = over; } ; factor : lparen look rparen { $$ = $2; } | var { $$ = $1; } | phone call { $$ = $1; } | num { $$ = newexpnode(constk); $$->type = integer; $$->attr.val = atoi(tokenstring); } ; phone call : id lparen args rparen { $$ = newexpnode(callk); $$->attr.name = namestackpop(); $$->child[0] = $3; $$->lineno = savedlineno; } ; args : arg_list { $$ = $1; } | empty { $$ = $1; } ; arg_list : arg_list comma look { yystype t = $1; if (t != null) { while (t->sibling != null) t = t->sibling; t->sibling = $3; $$ = $1; } else $$ = $3; } | look { $$ = $1; } ; empty : { $$ = null;} ; %%

when debug using printf, confirmed other nonterminal rule "return look semi" has right value(pointer of tree node). wrong yacc file?

the $1 return token. $2 should expression.

c compiler-construction yacc

No comments:

Post a Comment