6

This is more of an "in principle" question than a practical one. Is the order in which Yacc reduces productions, and reads new tokens from the lexer defined. That is, if I had the following set of tokens:

INTEGER_BEGIN
INTEGER_VALUE
LESS_THAN
INTEGER_BEGIN
INTEGER_VALUE

Can Yacc, within its semantics, read the LESS_THAN token from the lexer, before it reduces INTEGER BEGIN INTEGER_VALUE to a single thing, given a set of productions like:

expr : expr LESS_THAN expr
     | integer

integer : INTEGER_BEGIN INTEGER_VALUE

Do the rules for this change if these are defined with semantic actions?

Alex Gaynor
  • 14,353
  • 9
  • 63
  • 113

1 Answers1

4

Yes it can. Yacc creates an LALR(1) parser -- the (1) means 1 token of lookahead -- so it may read ahead 1 token past the end of the tokens for a rule before reducing that rule. The existence of semantic actions is irrelevant, as the semantic action is just some C code to run just before reducing a rule.

Note that there's no guarantee that it will ALWAYS read ahead a token. The parser created by yacc or bison sometimes uses 'default reductions' -- states where it can reduce a rule WITHOUT having to read the next token first. This happens whenever the reduction of a rule is independent of the next token.

In this specific example, a default reduction could be used for the integer rule, so it might reduce it without lookahead, but again there's no guarantee -- default reductions are an optimization used by some (but not all) implementations of yacc.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Is there some way to know if this happened? – Alex Gaynor Sep 09 '12 at 13:54
  • http://www.gnu.org/software/bison/manual/html_node/Default-Reductions.html contains a full description of the semantics of default reductions, and, as you noted causes a delayed invocation of the lexer. – Alex Gaynor Sep 09 '12 at 16:04
  • In some versions of bison, you can check `if (yychar == YYEMPTY)` in the action to see if you're in a default reduction (so no lookahead has been read), but that's not terribly portable. – Chris Dodd Sep 10 '12 at 01:04