[antlr-interest] Equivalent of TOKEN{x,y}

Kaleb Pederson kaleb.pederson at gmail.com
Thu Nov 12 20:08:11 PST 2009


On Wednesday 11 November 2009 11:40:44 am Jim Idle wrote:
> Just add counters in the parser rules (scope probably), let there be any number of them, then issue errors if the counts are wrong. You really don't want to do things like this at the grammar level - the messages you will issue will be confusing to users "Error at 'y', expecting '}'" leaves you to guess the meaning. It is generally better to produce a CommonTree, verify it and output whatever you need. Every task is different though.

Jim,

Thanks for the counter suggestion, I hadn't thought of that.  Since my token is a fixed length, I started off measuring the total length of the production's child nodes in order to determine the number of tokens, but it didn't sit right with me.

I will definitely need good readable error messages so I've relaxed the main grammar a bit and now I handle the rest in my semantic processing phase.

> You can also use gated predicates or ordinary predicates perhaps, but you will end up with a messy grammar that isn't as easy to deal with.

That's an interesting thought, but I believe you're right, that will make things messy and more intertwined than need be.

Thanks.

--
Kaleb Pederson

Blog - http://kalebpederson.com
Twitter - http://twitter.com/kalebpederson


More information about the antlr-interest mailing list