2009/9/15 Gavin Lambert <an...@mirality.co.nz>

> At 23:41 15/09/2009, Kevin Twidle wrote:
> >I have been using my PonderTalk (Smalltalk like) language for a
> >while now and I have just noticed that leaving a block empty
> >causes a parser error. This is with ANTLRWorks 1.2.3. I have
>
...
>
> You should define an imaginary token to use as the root
> here.  Things go a little strange when you try to use a
> multi-token rule as the root, especially (as you've found) if it's
> optional.
>
> tokens { BLOCK; }
>
> block : '[' sentences ']'
>   -> ^(BLOCK sentences)
>   ;
>
> Using imaginary tokens to indicate structure like this also helps
> when reading the tree (either via a tree parser or directly).
>
>
Thanks for the fast reply.  Actually in my PonderTalk grammar I do have
those tokens. I have now added the token to my example and it still fails
with:
hello. [ ]. bye

The input of ANTLRWorks shows:
hello.[]

Ah! It does crash I wasn't looking at the output of ANTLRWorks (it's not in
the console):

line 1:6 no viable alternative at character ' '
line 1:8 no viable alternative at character ' '
line 1:11 no viable alternative at character ' '
Exception in thread "main"
org.antlr.runtime.tree.RewriteEmptyStreamException: rule sentences
 at
org.antlr.runtime.tree.RewriteRuleElementStream._next(RewriteRuleElementStream.java:158)
at
org.antlr.runtime.tree.RewriteRuleElementStream.nextTree(RewriteRuleElementStream.java:145)
 at TrialParser.block(TrialParser.java:504)
at TrialParser.sentence(TrialParser.java:395)
 at TrialParser.sentences(TrialParser.java:211)
at TrialParser.sentences(TrialParser.java:248)
 at TrialParser.start(TrialParser.java:116)
at __Test__.main(__Test__.java:14)


My grammar is now:

grammar Trial;

options {
output = AST;
        k = 2;
}

tokens{
        BLOCK = '_block';
}

start : sentences EOF
 ;
sentences
: sentence? (DOT sentences)?
 -> sentence? sentences?
;
sentence: WORD | block;
block : '[' sentences ']'
-> ^(BLOCK sentences)
 ;

WORD : LETTER LETTER+;
LETTER : 'a'..'z';
DOT : '.';

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"il-antlr-interest" group.
To post to this group, send email to il-antlr-interest@googlegroups.com
To unsubscribe from this group, send email to 
il-antlr-interest+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/il-antlr-interest?hl=en
-~----------~----~----~----~------~----~------~--~---

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

Reply via email to