[antlr-interest] Newbie tree grammar question and rewrite rules
Ted Villalba
tedv at esrconsulting.com
Wed May 9 10:50:51 PDT 2007
Hi,
I'm a little slow to pickup the concepts of the tree grammar, and looking
perhaps for some hints. I probably need to read through the chapter in the
ANTLR Reference a few more times, but in the meantime am hoping for some
guidance.
I have a fairly simple grammar which I include below, and one line with
which I am struggling is the following:
query : (field | qid | LPAREN query RPAREN) (BOOL_OP^ query )*
;
It seems the simplest way to create a tree grammar is to simply cut and
paste the AST rewrite rules , so I considered rewriting the above, using an
AST rewrite rule. My question is, is there necessarily a rewrite rule that
can replace AST construction operators, and is this the generally
recommended approach - to have a rewrite rule for every alternative?
Thanks for your help,
Ted
P.S.
Here is the grammar I am working with:
grammar QueryParser;
options{
k=1;
output=AST;
ASTLabelType=CommonTree;
}
// START:query
start : (query
{System.out.println($query.tree.toStringTree());} )+;
query : (field | qid | LPAREN query RPAREN) (BOOL_OP^ query )*
;
field : tag '=' value -> ^('=' tag value) ;
value : (term | LPAREN value RPAREN) (WOK_OP^ value )* ;
term : WCHAR+
| '"' WCHAR+ '"'
;
tag : WCHAR+ ;
qid : '#'DIGIT+ ;
BOOL_OP : ' AND '|' OR '|' NOT ';
WOK_OP : ' SAME ' | ' NEAR ';
DIGIT : ('0'..'9');
LPAREN : '(' ;
RPAREN : ')' ;
WCHAR : ('0'..'9'|'a'..'z'|'A'..'Z'|' ');
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070509/c2067ace/attachment.html
More information about the antlr-interest
mailing list