[antlr-interest] Inserting missing nodes
Jean-Sebastien Vachon
jean-sebastien.vachon at wantedtech.com
Thu Apr 28 13:06:33 PDT 2011
Hi All,
First, I'd like to make it clear that I'm new to ANTLR so please be kind with me ;)
Second, my main problem right now is that I'm currently building a grammar that will let
me validate and parse a Boolean query with some special features. I got 90% of my parser working but I'm stuck with the last
feature that is required. Basically, I need to be able to insert missing operators (AND/OR) where required.
Considering the following query: "software engineer java"
I need to build a tree representing the query as if it was "software AND engineer AND java" but I also need to be able to change the inserted operator 'AND' to something else.
My first thought was to push a new type of node (let's say DEFAULT_OP) into my tree using a rewrite rule that I could rewrite to the proper operator using a tree walker and/or translator.
I made a few tries and got it working in some situations but I can't get it to parse everything I'm throwing at it. My best try so far is shown in the listing below... I did not include the lexer as it is pretty straight forward...
All hints and comments are welcomed...
Thanks for your help
===============================
grammar MyGrammar;
options {
language = Java;
output = AST;
ASTLabelType = CommonTree;
}
query
: and_expr+ EOF!
;
and_expr
: (expr expr+) => default_op
| (u1=or_expr (AND^ u2=or_expr)*);
or_expr
: u1=expr (OR^ u2=expr)*
;
default_op
: (e1=or_expr e2=or_expr) -> ^(DEFAULT_OP $e1 $e2)
;
expr
: (NOT^)? (operand)
;
operand
: (FIELD^)(operand)
| PREFIX
| WORD
| SENTENCE
| WORDLIST
| NEGATIVE(w=PREFIX|w=WORD|w=SENTENCE|w=WORDLIST) -> ^(NOT $w)
| MUST
| LPAREN! expr RPAREN!
;
More information about the antlr-interest
mailing list