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
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address
--
You received this message because you are subscribed to the Google Groups
"il-antlr-interest" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/il-antlr-interest?hl=en.