[antlr-interest] Tree walker grammer help.

jack zhang jackgzhang2 at yahoo.com
Mon Sep 22 00:43:14 PDT 2008


Sorry that I made a mistake in the previous post. So please ignore the previous one.

I want to achieve following: (convert the input logic query to a standard format)
(1) a AND b  ==> (a and b)
(2) a OR b   ==> (a or b)
(3) a OR b AND c ==> (a or (b and c))
(4) a b c ==> a b c (No change, since there is no logic involved)
(5) a b AND c ==> a (b and c)

By using the Antworks tool I know that the lexer and parser grammar works just fine. 
(It generate the correct AST tree). 
But the strange thing is that my tree walker grammar doesn't work. 

If I use the TreeWalker version 1(see attached code),  test case 4 and 5 are not working.

(4) a b c ==> a
(5) a b AND c ==> a

If I use the TreeWalker version 2(see attached code), test case 1,2,3 and 5 are not working.

(1) a AND b ==> (a b and)
(2) a OR b ==> (a b or)
(3) a OR b AND c ==> ( a or ( b c and ))
(5) a b AND c ==> a


Also TreeWalker version 2 report warning:

-Decision can match input such as "WORD" using multiple alternatives: 1, 2
As a result, alternative(s) 2 were disabled for that input-


I know something is wrong with the matching of multiple WORD, But I just cannot figure out.
The only difference with version 1 and verson 2 is the WORD rule.

Where am I wrong?
Thx !

----------------------Lexer and Parser ---------------------
grammar Query;

//=== Parser Option ===//
options {
  output = AST;
  k=*;
}




//=== Lexer ===//

LEFT_PAREN: '(';
NOT: 'NOT';
AND: 'AND';
OR: 'OR';
WORD: ('a'..'z' | 'A'..'Z' | '.' | ',' | '0'..'9')+ | '"'.+'"';
RIGHT_PAREN: ')';
WHITESPACE: (' ' | '\t' | '\r' | '\n') { $channel = HIDDEN; } ;



//=== Parser ===//
expr: orexpression*;

orexpression
    :   andexpression (OR^ andexpression)*
    ;

andexpression
    : notexpression (AND^ notexpression)*
    ;

notexpression
    : (NOT^)? atom
    ;

atom
    : WORD
    | LEFT_PAREN! expr RIGHT_PAREN!
    ;

----------------------Tree Walker Version 1 ---------------------
tree grammar QueryTree;

options { 
  output = AST;
  ASTLabelType = CommonTree;
  tokenVocab=Query;
}



expr returns [String value=""]
    : ^(AND a=expr b=expr) {
        $value = "(" + $a.value + " and " + $b.value + ")";
    }
    | ^(OR a=expr b=expr) {
        $value = "(" + $a.value + " or " + $b.value+ ")";
    }
    | ^(NOT a=expr) {
        $value = "(not " + $a.value +")";
    }
    |  ( WORD {  $value = $WORD.text;  }) ;
    
    
----------------------Tree Walker Version 2 ---------------------
tree grammar QueryTree;


options { 
  output = AST;
  ASTLabelType = CommonTree;
  tokenVocab=Query;
}



expr returns [String value=""]
    : ^(AND a=expr b=expr) {
        $value = "(" + $a.value + " and " + $b.value + ")";
    }
    | ^(OR a=expr b=expr) {
        $value = "(" + $a.value + " or " + $b.value+ ")";
    }
    | ^(NOT a=expr) {
        $value = "(not " + $a.value +")";
    }
    |  ( WORD {  $value +=  " " + $WORD.text;  })+  ;
    
    

    




      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080922/86b9d4cc/attachment.html 


More information about the antlr-interest mailing list