[antlr-interest] White space needed in the parsing.

Markus Fröb grey_earl at web.de
Thu Sep 18 13:42:43 PDT 2008


> -----Ursprüngliche Nachricht-----
> Von: "jack zhang" <jackgzhang2 at yahoo.com>
> Gesendet: 18.09.08 22:22:29
> An: antlr-interest at antlr.org
> Betreff: [antlr-interest] White space needed in the parsing.

> 
> 
> Thanks! it works. Another question is that (Sorry that I am pretty 
> new to Antlr) in the tree walker, how to walk the WORD+ parser.
> 
> Currently I have following tree walker:
> 
------------------ CUT ----------------------
> 
> expr returns [String s]
>  : ^(NOT a=expr) {
>  s = "(not " + a + ")";
>  }
>  |
>  ^(AND a=expr b=expr) {
>  s = "(" + a + " and " + b + ")";
>  }
>  | ^(OR a=expr b=expr) {
>  s = "(" + a + " or " + b + ")";
>  }
>  | WORD {
>  s = dateParser.parse($WORD.text);
>  }
>  ;
> 
> 
> 
> 
> 
> But it only matches the first word and totally ignored the rest. Is 
> there a way to do this:
> 
> WORD+ { for (String WORD :WORD+ { // do something to each WORD} }
> 
> Thx !

Your expression matches only one word, as you only specified one. I think the following should work (but I cannot guarantee this, as I haven't worked much with tree parsers yet):

expr returns [String s]:
... and, or, not
 | w=word_expr { s = w; }
 ;

word_expr returns [String s]
 : w=WORD e=word_expr { s = w + e; }
 | { s = ""; }
 ;

This is right recursion as word_expr can match either a word and something following, or nothing at all, which then returns the empty string. Since ANTLR is greedy be default, and I think the tree parsers, too, this should catch all words.
________________________________________________________________________
"50 erste Dates" mit Adam Sandler u. Drew Barrymore kostenlos anschauen!
Exklusiv für alle WEB.DE Nutzer. http://www.blockbuster.web.de



More information about the antlr-interest mailing list