compiler construction - Creating a grammar for function prototypes and declaration bison -
i'm build compiler school project. works fine having difficulty defining grammar functions. have detect if there 0 or more parameters , if function either prototype or definition. keep getting shift/reduce and/or reduce/reduce errors. have tried many variations no avail. here have, looking pointers in direction or ideas on how fix this.
function : parameters ';' {free_ast($2); $$ = $1;} | parameters block {$$ = adopt1($1, $2)} ; parameters : paramlist ')' {free_ast($2); $$ = $1;} | '(' identdecl ')' {free_ast($3); $$ = adopt_func2($1, $2);} ; paramlist : paramlist ',' identdecl {free_ast($2); $$ = adopt1($1, $2);} | '(' identdecl {$$ = adopt_func2($1, $2);} identdecl : basetype tok_array tok_ident {$$ = adopt2($1, $2, change_symbol( $3, tok_declid));} | basetype tok_ident {$$ = adopt1($1, change_symbol( $2, tok_declid));} ; block : stateseq '}' {free_ast($2); $$ = $1;} | ';' {free_ast($1);} ;
adopt_func2 adopts definition part of problem adopt_proto. thank time.
as far can see, issue has nothing declaration/definition similarity. problem have special case parameter list 1 parameter, leading ambiguity:
parameters : paramlist ')' | '(' identdecl ')' ; paramlist : paramlist ',' identdecl | '(' identdecl ;
so (type id)
can reduced using either first production parameters
, paramlist
reduced second production, or can reduced directly using second production parameters
. can't see obvious need second production parameters
, i'd suggest removing it.
however, (imho) ugly way of writing grammar, since hides symmetry of parentheses. not recognize parameter list 0 parameters. i'd suggest:
parameters : '(' paramlist ')' | '(' ')' paramlist : identdecl | paramlist ',' identdecl
which think clearer.
Comments
Post a Comment