I’m having a trouble about rewrite rule to convert from parsing tree into AST tree in antlr.
Here’s my antlr code:
grammar MyGrammar;
options {
output= AST;
ASTLabelType=CommonTree;
backtrack = true;
}
tokens {
NP;
NOUN;
ADJ;
}
//NOUN PHRASE
np : ( (adj)* n+ (adj)* -> ^(ADJ adj)* ^(NOUN n)+ ^(ADJ adj)* )
;
adj : 'adj1'|'adj2';
n : 'noun1';
When I input “adj1 noun1 adj2” , the result of parse tree like this:

But the AST tree after rewrite rule seem not exactly like the parse tree, the adj is double and not in order, like this:

So my question is how can I rewrite rule to have a result like the parsing tree above?
Your noun phrase rule collects all the adjectives and copies them to both sides of the nouns because ANTLR can’t automatically distinguish between one group of matched
adjs and another.Here is a break-down of the
nprule:One way to separate the two groups is to collect them into their own respective lists. Here’s an example, applied to rule
np:This way ANTLR knows which
adjs to write out before and after thens.