how to force bison to immediate compilation -
i'd compose "classic" basic syntax mixed bit more free format, e.g.:
a=5:b=6:c=a+b // writing several instructrions single line // using colon separator // eliminating mandatory terminals (like ';' in c) while (x < 3 ) { // status has mandatory parentheses around, x=x+1:y=y+2 // body of 'while' may have multi-statements line(s) z=y+y // , may have several lines } to realise set next rules. flex duty, correctly eliminates unnecessary linefeeds, etc:
^[ \t\r\n]*\n /*ignore empty line */ \/\/.*\n ; /* skip comment */ [ \t\r\n]+ ; /* ignore whitespace */ \{[ \t\r\n]*\} ; homecoming empty; \{[ \t\r\n]* ; homecoming '{'; \}[ \t\r\n]* ; homecoming '}'; unfortunately stmt followed colon not forcefulness compiler immediate compilation. expected in sec half of line proceeding code beingness compiled. here construction of bison file.
%type <nptr> stmt stmt_list expr %% line: line stmt_list ':' { // <-- problem ex($2); freenode($2); } | line stmt_list '\n' { ex($2); freenode($2); } | /* null */ ; stmt: variable '=' expr { $$ = opr('=', 2, $3, id($1)); } | port '=' expr { $$ = opr('=', 2, $3, id($1)); } | .... etc. | while '(' expr ')' empty { $$ = opr(while, 1, $3); } | while '(' expr ')' stmt_list { $$ = opr(while, 2, $3, $5); } ; stmt_list: stmt { $$ = $1; } | '{' stmt_list '}' { $$ = $2; } | '{' error '}' { errorflag=1; } | error '\n' { errorflag=1; } ; expr: integer { $$ = con($1); } | variable { $$ = id($1); } | .... etc. | '(' expr ')' { $$ = $2; } ; how can modify create expected behavior?
the of import thing remember bison actions executed when right-hand side has been parsed.
consider 2 simple recursive productions:
a: n | %empty ; and
a: n | %empty ; let's apply them input:
n1 n2 n3 with right-recursive case (the first one), productions follows (the subscripts clarity):
a0 → n1 a1 a1 → n2 a2 a2 → n3 a3 a3 → ε
and parse tree is:
a0 +-----+-----+ | | | a1 | +---+---+ | | | | | a2 | | +-+-+ | | | | | | | a3 | | | | n1 n2 n3 ε
while left-recursive 1 (the sec one), produces:
a0 → a1 n3 a1 → a2 n2 a2 → a3 n1 a3 → ε
and parse tree is:
a0 +-----+-----+ | | a1 | +---+---+ | | | | a2 | | +-+-+ | | | | | | a3 | | | | | | | ε n1 n2 n3
the of import thing observe in right-recursive case, productions include entire input string, , parser actions hence take place right left. in left-recursive case, in contrast, productions successively produce prefixes of string, , parser actions take place left right.
in conclusion, if want actions occur left right, utilize left recursion.
bison
No comments:
Post a Comment