flex lexer - How do I only print a statement when the end of file is reached in Bison? -


for simplification of original problem, supposed wanted parse string of left , right parentheses. flex file have tokens

"(" return lparen ")" return rparen 

and bison file have productions

completedparse: pair {printf("success!\n");} ; pair: lparen pair rparen     | lparen rparen pair     | lparen rparen ; 

if given string "(()()(()))" or "()()(())" succeed.

the problem have "(()()))" print success statement, before failure. know bison creates production

$accept: completedparse $end 

which how bison accepts or rejects string. problems go away if write own $accept production , include print statements in production. according 3.0.2 manual $accept cannot used in grammar.

my problems go away if modify return value of yyparse() (assume need more information 0 => success, 1 => failure). don't think possible though.

if have insight please respond, , help!

one simple solution:

completedparse: pair {printf("success!\n");}                | pair error               ; 

the error pseudo-terminal won't match errors, match 1 eof expected , else found. if error encountered in other context, error production won't apply. in general, should use yyerror perform action on syntax error; above rule way avoid invoking completedparse action in case there garbage @ end of input.

by way, grammar not match (())(). might want use:

pair:         /* empty */     |         '(' pair ')' pair     ; 

additionally:

in response addendum in comments:

  1. you can use action { yyabort } in error production in order return (with return value of 1, indicating syntax error). if want print generic error message, better off doing in yyerror implementation, since called on syntax error, while error production reduced on errors. can use error productions generate more specific error messages, that's lot more work.

  2. you cannot customize $accept pseudo-production. however, above technique more or less equivalent.

  3. you cannot change return type or values of yyparse (0 success, 1 syntax error, 2 allocation error). however, useful return more data parse (for example, ast). "traditional" solution use global variable, of distaste global variables, solution use [%parse-param][1] declaration add 1 or more additional arguments; normally, start production set value of objects these arguments point. if this, should check return value of yyparse before relying on additional return information, because start production may evaluated in case of error.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -