This is not a bug. There is an error in the example.

According to the POSIX standard yyparse() should return a zero value
if the input is accepted (i.e. if it is correct), and a non-zero value
otherwise. Bison implements the entire state machine inside yyparse()
so the explicit return statement inside the semantic action for exp
will actually determine whether or not the input is accepted. That is,
valid input will be accepted if and only if the sum (or single
integer) equals zero.

Furthermore, the return statement terminates parsing before yyparse()
gets a chance to report the error. In technical terms the last FOO
token is a "look-ahead token" when the sum is reduced, and as such it
is not yet on the stack when the return statement is reached.

If you replace "return $1;" by  "$$ = $1;" (which is what you
intended, right?) the syntax error will be reported. I am not sure why
byacc produces the output you expect. It could be that it checks the
validity of the look-ahead token prior to reducing the stack but I
haven't taken the time to check.

Also note that global variables are necessary if information assembled
by the parser is needed by external functions. To get the value of
exp in the main program you could for example add
    static int exp_value;
to the declarations, and replace the start rule by
    exp : expr { exp_value = $1; };

If you are interested in all the gory details of the bison-generated
parser you could add the lines
  #define YYDEBUG 1
  int yydebug = 1;
to the declarations. This causes the parser to output information on
the current state, the parser stack, and rules applied, for each token
encountered. For the output to make sense you probably want to
add the -v option when invoking bison, which writes a textual
description of the grammar to the file y.output.

Best regards,
/Christer



-- 
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]

Reply via email to