[antlr-interest] Empty AST entry stops parsing

Gavin Lambert antlr at mirality.co.nz
Tue Sep 15 05:45:19 PDT 2009


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 
>refined the grammar to show the point. The problem line is 
>^(statements). With that taken out the parsing works.
[...]
>sentence:WORD | block;
>block:'[' sentences ']'
>-> ^(sentences)
>;

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). 



More information about the antlr-interest mailing list