[antlr-interest] Tree walker grammer help.

jack zhang jackgzhang2 at yahoo.com
Mon Sep 22 10:46:48 PDT 2008


Thanks a lot !. It works.

--- On Mon, 9/22/08, Jim Idle <jimi at temporal-wave.com> wrote:
From: Jim Idle <jimi at temporal-wave.com>
Subject: Re: [antlr-interest] Tree walker grammer help.
To: jackgzhang2 at yahoo.com
Cc: antlr-interest at antlr.org
Date: Monday, September 22, 2008, 8:38 AM




  
  
On Mon, 2008-09-22 at 00:43 -0700, jack zhang wrote:



Oh yeah, you will have to put your multiple words under an imaginary so that WORD WORD WORD AND WORD WORD WORD are not clashing.



use (w+=WORD)   -> ^(WORDLIST $w+)  in your parser and



expr

: .....



| ^(WORDLIST WORD+)





In your tree grammar



JIm


    


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;  })+  ;

    

    



    








    

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address



 



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


More information about the antlr-interest mailing list