[antlr-interest] Inserting missing nodes
Jean-Sebastien Vachon
jean-sebastien.vachon at wantedtech.com
Wed May 4 07:12:41 PDT 2011
No one can help me with this? :S
Let me know if something is not clear. I need to fix this issue as soon as I can.
Thanks
-----Original Message-----
From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-bounces at antlr.org] On Behalf Of Jean-Sebastien Vachon
Sent: April-28-11 4:07 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Inserting missing nodes
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!
;
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
More information about the antlr-interest
mailing list