I might be asking a stupid/basic question but i had been confused about ANTLR AST building.
What i want to made is a kind of Boolean expression parser such that on parent nodes i have operator and its operands as children. for instance, a sentence
( ( A B C & D ) | ( E & ( F | G ) ) )
should ideally be representing
|
/ \
/ \
/ \
/ \
& &
/ \ / \
/ \ / \
/ D E |
/|\ / \
A B C / \
F G
From the following grammar.
grammar Test;
options
{
language = 'Java';
output=AST;
}
exp : word (expRest^)? | '('! exp ')'! (expRest^)? ;
expRest : (('&'|'|'|'!'|'&!'|'|!')^) exp | (('~'^) digit+ exp);
word : letter letter* -> ^(letter letter*);
letter : '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'|'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'|'j'|'k'|'l'|'m'|'n'|'o'|'p'|'q'|'r'|'s'|'t'|'u'|'v'|'w'|'x'|'y'|'z'|'A'|'B'|'C'|'D'|'E'|'F'|'G'|'H'|'I'|'J'|'K'|'L'|'M'|'N'|'O'|'P'|'Q'|'R'|'S'|'T'|'U'|'V'|'W'|'X'|'Y'|'Z';
digit : '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9';
The problem is, that i am getting 'A B C' as either a list(array) of nodes as children of '&'.
Is it possible to restrict it as a single string??? i.e. 'A B C' or in other words, is it possible to have multiple characters at root node in AST??? If yes then how can i achieve it?
for reference, i want to make a syntax tree of 'risk factors & current economic state'
P.S. I have also tried :
word : (letter letter*)^ ;
And just for a reference, I am using .NET environment.