Search code examples
parsingcompiler-constructioncontext-free-grammar

Nested Grammars in Compiler Design


This seems like a simple question, but I could not find a good explanation of this anywhere. How do you handle nested grammars [cfgs] (aka. grammars referencing other grammars.) For example,

S => Eb
E => cA

Separate Grammar:

A => d

Is this possible? If not, how does one approach dealing with this. For grammars, like variable declaration, is it necessary to simply rewrite the expression grammars associated with the main grammar or can they be referenced from it? If so, how does one integrate this feature into an LL(1) parser properly.

Thanks.


Solution

  • I believe your question is dealing with the physical separation, in which case you could merge all together to form a complete main grammar.

    Conceptually, a grammar with missing rules would be considered incoherent and invalid.