[antlr-interest] Empty AST entry stops parsing

Kevin Twidle kpt at doc.ic.ac.uk
Tue Sep 15 06:20:14 PDT 2009


2009/9/15 Gavin Lambert <antlr at 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 : '.';
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090915/9b49c46a/attachment.html 


More information about the antlr-interest mailing list