[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Sub grammars



Hi,

I would like to specify a grammar in two parts, where one grammar is a subset of the other.

For example.. 

start =						// Startnode of language A
        {expr} some_expression
      | {literal} literal;

literal = 						// Virtual startnode of language B, which is a subset of A
	numeric_literal
      | line_literal
      | circly_literal;

The start production represents the main language (A). The literal production encapsulates a separately defined language to express literals (B). I would like to be able to parse language A (including what is defined in B) and also parse only the part that is defined in language B separately.

I can't give anything in the form of a literal (B) directly to the parser for the complete language (A) since it will always give me a Start node (Start tree = parser.parse()). This means that if I pass in a literal string (B), it will fail with parse errors.

What I would like is to define the grammar of the two languages in separate files and generate separate parsers for them. Language A would than have to delegate part of the parsing process to language B in some way (possibly in a tree walker ?).

However, I don't really have a good idea on how to do this. I did find a similar post on this (http://lists.sablecc.org/pipermail/sablecc-user/msg00441.html), but it didn't help me a lot.

Are there any ideas on this?

Thanks in advance,
Bert.