0
votes

I have the following piece of grammar

prod: (ID IN)? subrule (COMMA (ID IN)? subrule)*

The ID, IN and COMMA are the token terminals and the prod rule produces a list of subrule expressions.

I’m using the c++ runtime with a visitor pattern and now I’m stuck with the following problem.

How can I find out which subrule does the ID token belong to? Calling ID() method allows me to ask for a specific k-th ID token but it doesn’t have to correspond to the K-th subrule. I’d like to disambiguate that. Here’s an example stream of tokens

subrule COMMA subrule COMMA
 ID IN subrule COMMA

If I iterate over the subrule vector there is no corresponding parallel ID iterator and requesting the first ID match and the first subrule match in the above token stream would give me the first subrule and the ID of the third subrule instead of telling me that the first subrule has no ID.

It seems iterating over the children() vector is promising, but there’s no API to distinguish terminals from non terminals it seems. Is there a way to resolve this using a visitor pattern?

Thanks!

1

1 Answers

0
votes

Why not to move (ID IN)? subrule into a separate rule, say subruleDescription?

In this case you will know for sure which ID corresponds to which subrule, since in the parse tree you will see a new node: subruleDescriptionContext

An example:

subruleDescription: (ID IN)? subrule
prod: subruleDescription (COMMA subruleDescription)*