[antlr-interest] Empty AST entry stops parsing
Gavin Lambert
antlr at mirality.co.nz
Tue Sep 15 12:41:31 PDT 2009
At 01:20 16/09/2009, Kevin Twidle wrote:
>line 1:6 no viable alternative at character ' '
>line 1:8 no viable alternative at character ' '
>line 1:11 no viable alternative at character ' '
Those are because you haven't defined any lexer rules that can
match whitespace.
>tokens{
> BLOCK = '_block';
>}
That is *not* an imaginary token. Remove the '= ...' part or
it'll actually try to match that in the input.
>Exception in thread "main"
>org.antlr.runtime.tree.RewriteEmptyStreamException: rule sentences
>at
>org.antlr.runtime.tree.RewriteRuleElementStream._next(RewriteRuleElementStream.java:158)
This is because your 'sentences' rule may match nothing; in that
case you need to use 'sentences?' in the rewrite in the block
rule.
>start:sentences EOF
>;
>sentences
>:sentence? (DOT sentences)?
>-> sentence? sentences?
>;
>sentence:WORD | block;
>block:'[' sentences ']'
>-> ^(BLOCK sentences)
>;
Is it valid to have dots without anything else? As your grammar
stands at the moment, it'll accept '.....' in sentences.
If not, then you should express the optionality externally:
start : sentences? EOF ;
sentences : sentence (DOT sentence)* -> sentence+ ;
sentence : WORD | block ;
block : '[' sentences? ']' -> ^(BLOCK sentences?) ;
(I've also changed the sentences rule to use iteration rather than
recursion, which is more efficient.)
More information about the antlr-interest
mailing list