1
votes

I have a rule that looks like this:

a : (b | c) d;

b : 'B';

c : 'C';

d : 'D';

With this grammar ANTLR builds a flat parse tree. How can I rewrite the first rule (and leave the other two unchanged) so that whatever is matched is being returned under a root node called A?

If the first production rule was like this:

a : b d;

then it could have been rewritten as

a : b d -> ^(A b d)

and it would have solved my problem. However the first grammar rule yields more than one possibility for the resulting parse tree ^(A b d) or ^(A c d).

How do I express this when rewriting the rule?

1
What does this have to do with C#? Tag removedtnw

1 Answers

2
votes

You can use the ? operator in the rewrite as follows.

a : (b | c) d -> ^(A b? c? d);