[antlr-interest] Empty AST entry stops parsing

Kevin Twidle kpt at doc.ic.ac.uk
Wed Sep 16 01:35:39 PDT 2009


Gavin,
Again, thanks for the fast response.

2009/9/15 Gavin Lambert <antlr at mirality.co.nz>

>
> >tokens{
> >         BLOCK = '_block';
> >}
>
> That is *not* an imaginary token.  Remove the '= ...' part or
> it'll actually try to match that in the input.
>

I didn't understand this at first because it seems to work the same both
ways. However, I just remembered that I used a variable name in my language
that got flagged by the compiler as being the same as one of the virtual
tokens like the one above. I could not understand it at the time and put an
'_' in front of all the virtual tokens so that there would be no clash. In
my language I have many like this because I saw it in an example somewhere.
 I shall strip out all the values, I assume that it will also make the
parsing more efficient.


> 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.)
>
> Much better, thanks. I have done the appropriate changes to my language and
can now have an empty block (of course I am now wondering whether that
should be a valid construct but that's another matter!).

You have also indirectly helped me with my lexer include files.

I think ANTLR is an amazing tool. When I think back to my yacc days ...

Cheers,

Kevin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20090916/bf81bee9/attachment.html 


More information about the antlr-interest mailing list