views:

79

answers:

1

Like the title says, I would like to enable/disable certain grammar rules in a yacc or bison grammar file.

Is there a way to do so?

A: 

If you mean, at compile time, yacc uses standard C /* */ comment syntax.

If you mean, at run time, you still have to work with the tables you have, so they need to include the entire grammar with the optional phrases.

So I would suggest making a fake terminal symbol. Rules that are optional would be preceded by the fake terminal. You would only return this terminal if you were including the optional productions.

A variation on this approach would involve defining two versions of a real terminal that actually exists. This only works for grammars that lead strings with terminals but if you have such an input then one terminal can mean one set of rules and another terminal might appear in two sets of rules, that is:

T_A dynamic_phrase_in_grammar;

always_on static_phrase_in_grammar;

always_on: T_A | T_B;

So, to enable the dynamic phrase, the real terminal is returned as T_A, to disable it, return as T_B.

DigitalRoss