Noeud:Exercises on Bison, Noeud « Next »:Further Reading On Parsing, Noeud « Previous »:Using Bison with the GNU Build System, Noeud « Up »:Parsing
error
token to recover from errors.
The general idea is that when the parser finds an error, it tries to
find the nearest rule which claims to be ready to return to normal
processing after having thrown away embarrassing symbols. For instance:
exp: '(' error ')'
But including recovery means you must pretend everything went right. In
particular, an exp
is expected to have a value, this rule
must provide a valid $$
. In our case, the absence of a
rule means proceeding with a random integer, but in other applications
it may be a wandering pointer!
Equip yleval
with proper error recovery. Voir Writing rules for error recovery, for detailed explanations on error
and
yyerrok
.
grammar: rule | grammar rule rule: symbol ':' ride-hand-side right-hand-side: /* Nothing */ | right-hand-side symbol symbol: 's'
Convince yourself that Bison cannot accept this grammar either by (i)
feeding it to bison
and understanding the conflict, (ii)
drawing the LR(1) automaton and understanding the conflict, (iii)
looking at the strategy you, human, use to read the text s
: s s s : s : s
.
Yes, the bottom line is that, ironically, Yacc and Bison cannot use themselves! (More rigorously, they cannot handle the natural grammar describing their syntax.)
Once you have understood the problem, (i) imagine you were the designer
of Yacc and propose the grammar for a better syntax, (ii) stop dreaming,
you are not Steven C. Johnson, so you can't change this grammar:
instead, imagine how the scanner could help the parser to
distinguish a symbol
at the right hand side of a rule from the
one at the left hand side.