[antlr-interest] Empty AST entry stops parsing

Jim Idle jimi at temporal-wave.com
Tue Sep 15 08:17:51 PDT 2009


On 09/15/2009 04:41 AM, 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 refined the grammar to 
> show the point. The problem line is ^(statements). With that taken out 
> the parsing works.
>
> grammar Trial;
>
> options {
> output = AST;
>         k = 2;
> }
>
> start : sentences EOF
> ;
> sentences
> : sentence? (DOT sentences)?
> -> sentence? sentences?
> ;
> sentence: WORD | block;
> block : '[' sentences ']'
> -> ^(sentences)
> ;
>
You probably want to use an imaginary token as the root node of your 
block. ->^(BLOCK sentences?). Sentences acan be empty and you cannot 
rewrite it as the root node. Also, you probably want tokens for SENTENCE 
and perahps SENTENCES. If BLOCK is empty then deal with it specially in 
the tree grammar:

block : ^(BLOCK sentences)
           | BLOCK // Empty block
           ;


Jim
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090915/77ea3614/attachment.html 


More information about the antlr-interest mailing list