Reputation: 75
Let's say I have the two following rules:
rule1: names INTEGER {*do something1*}
names: ID
| names ID {*do something2*}
How can I include the information INTEGER on my do something2 action? In this specific example I want to print the information INTEGER next to every ID (from names).
Upvotes: 1
Views: 571
Reputation: 370192
You can't pass information in that direction. At the time when *do something2*
executes, i.e. after each name has been read, the parser hasn't even seen the integer yet. The integer comes after the names in the input and the input is read linearly from beginning to end - there's no peeking ahead.
So if you want to print the names together with the integer, the only way to do that is to do so after the integer has been read, i.e. in the *do something1*
action.
To achieve that you can make *do something2*
store the names in an array or other data structure. Then *do something1*
can iterate over the data structure produced by *do something2*
and print each name together with the integer.
You can even go one step further and make all your actions return AST nodes and then only iterate over the AST to print things after the entire input has been parsed.
Upvotes: 2