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!