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

Markus Fröb grey_earl at web.de
Thu Sep 18 12:03:25 PDT 2008


> -----Ursprüngliche Nachricht-----
> Von: jack zhang <jackgzhang2 at yahoo.com>
> Gesendet: 18.09.08 20:48:12
> An: antlr-interest at antlr.org
> Betreff: [antlr-interest] White space needed in the parsing.
> 
> 
> I got a question regarding the Antlr Lexer. Basically I would like 
> to do a simple logic query parser. For example,
> 
>  (1) The input: Hello AND world OR antlr
>  The AST: (OR (AND Hello world) antlr)
> 
>  This part works fine now. But I would like to match a string (
> including 
>  white spaces) Here is the example,
> 
>  (1) The input: Hello AND how are you OR world
>  The Current AST: (AND Hello how) are (OR you world)
>  I want to achieve: (OR (And Hello how are you) world)
> 
> 
>  I would like to match "how are you" into one WORD, "hello" into 
>  another token. If I use following rules:
> 
>  WORD: .+;
> 
>  this will match everything including the "AND" into a WORD.

Now you have the lexer rule WORD accepting all characters, but what you want is to gather multiple words together. This is parser stuff.

> 
>  Did you have such situation before?
> 
>  Attached is the lexer and parser grammer file.
> 
>  Thx.
> 
>  -Jack.
> 
> ==== 
> grammar Query;
> 
> //=== Parser Option ===//
> options {
>  output = AST;
>  k=*;
> }
> 
> 
> 
> //=== Lexer ===//
> OR: 'OR';
> AND: 'AND';
> NOT: 'NOT';
> WORD : ('a'..'z' | 'A'..'Z' | '.' | ',' | '0'..'9')+ | '"'.+'"';
> LEFT_PAREN: '(';
> RIGHT_PAREN: ')';
> WHITESPACE: (' ' | '\t' | '\r' | '\n') { $channel = HIDDEN; } ;
> 
> 
> //=== Parser ===//
> expr: orexpression*;
> 
> orexpression
>  : andexpression (OR orexpression)*
>  ;
> 
> andexpression
>  : notexpression (AND andexpression)*
>  ;
> 
> notexpression
>  : (NOT)? atom
>  ;
> 
> atom
>  : WORD
>  | LEFT_PAREN! expr RIGHT_PAREN!
>  ;
> 
> ===

Changing the definition of atom to
atom
  : WORD+
  | LEFT_PAREN! expr RIGHT_PAREN!
 ;
allows atom to match multiple words together, which should work for your problem.

Markus
________________________________________________________________________
"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