[antlr-interest] Help me get started
John B. Brodie
jbb at acm.org
Tue Jun 22 05:37:31 PDT 2010
Greetings!
On Tue, 2010-06-22 at 04:22 -0400, Pavel Grinfeld wrote:
> Hi,
>
> Here's my first attempt at an ANTLR project. For practice, I just want
> to read a file with lowercase words and print them. I feed it "hi there
> how are you"
> All that the program prints is "hi".
>
> Many thanks in advance,
>
> PG
>
> grammar pg;
>
> doc
> : a = word {System.out.println($a.value);} ( WS b = word
> {System.out.println($b.value);} )*;
>
> word returns[String value]
> :LETTERS {$value = $LETTERS.text;} ;
>
> WS : ( ' '
> | '\t'
> | '\r'
> | '\n'
> ) {$channel=HIDDEN;}
> ;
>
> LETTERS: ('a'..'z')+;
Because you have directed the WS token to the HIDDEN channel in your
Lexer rule, the doc parser rule will never see a WS between your words.
The reason you just get 1 word is because your doc rule uses the * meta
operator to consume the second and subsequent words --- if there is a WS
which will never happen --- and you have not included the EOF in any
rule so ANTLR just stops after the first acceptable input (e.g. the
first word).
change your doc rule to be something like this:
doc : ( a=word { System.out.println($a.value); } )+ EOF ;
Hope this helps....
-jbb
More information about the antlr-interest
mailing list