I started writing a simple LR(1) parser with Bison. When processing lists I used a simple implementation of a vector, for example when parsing a list of expressions:
tuple-expression-list: expression[first] ',' expression[second] {
foo_vector_init(&$$);
foo_vector_push_back(&$$, $first);
foo_vector_push_back(&$$, $second); }
tuple-expression-list: tuple-expression-list[list] ',' expression[expr] {
foo_vector_push_back(&$list, $expr); $$ = $list; }
This worked fine. But then the grammar changed and I had to go for the GLR parser. Suddenly the compiler complained about $list
being constant. I found that for GLR parsers:
yyval
in an action (which is the lookahead).Questions:
$list
to a local variable first a valid fix or should I copy the vector deeply?
- when I never have to merge am I sure that only the actions that result in the final parse are executed?
Yes. The documentation specifies that
During the time that there are multiple parsers, semantic actions are recorded, but not performed. When a parser disappears, its recorded semantic actions disappear as well, and are never performed.
The point is precisely that the semantic actions performed are (only) those associated with the actual reductions accepted for the overall parse.
- Is assigning $list to a local variable first a valid fix or should I copy the vector deeply?
It's hard to be sure without knowing anything about your particular vector implementation, or about what other semantic actions are present in the grammar. Supposing that
a shallow copy should be sufficient.