01 Mar 2010, 22:27

Peter Cashin (3 posts)

Hi Terence:

I’m enjoying the book very much, well done.

Just a bit confused trying ANTLR: in a CFG I would expect a repeat to “back-off” in order to match the next term (as also for a regular-expression), but it seems like there is more of a PEG like parsing?? eg:

zend : ‘0’..’9’+ ‘0’ – wont match “00” ?

I’m probably missing something, but currently baffled…

Cheers, Peter Cashin

PS I am a PEG fan, and don’t really want to do CFG matching like that!

10 Mar 2010, 20:28

Terence Parr (36 posts)

Hi Peter :)

zend : ‘0’..’9’+ ‘0’

matches greedily by default (which is kinda opposite of what we want in the lexer unfortunately). So, in this case it matches 00 in the loop and then looks for 0 afterwards. 00 is actually ambiguous. I think that the grammar as a problem. You could try:

zend : (options {greedy=false;}:‘0’..’9’)+ ‘0’

but then this would not match 00 either. It would skip the loop to match the first 0 but then returned from zend. Ah. I also just realized that you have provided a non-lexer rule. Like yacc, its ancestor, ANTLR uses rules that start with an uppercase letter to being lexer rule.


12 Mar 2010, 00:39

Peter Cashin (3 posts)

Hi Terence:

Thanks for your response.

Sorry, I was being rather pedantic, and not very clear…. this is probably not the right place for this discussion, but just to close it off, the zend rule can be expressed in ANTLR as:

zend : ((‘1’..’9’)+ ‘0’)+

but ANTLR may not be able to parse the classic CFG but not a PEG:

s : ‘a’ s ‘a’ ‘a’

Never seen anything like that in practice, but being pedantic it is a CFG.

I really like how much you have packed into your DSL book, lots of problems that do occur in practice, sorted out and explained. Easy to follow, and good for common practical DSLs, but you also manage to go into rather tricky deeper issues too. Its great.

Cheers, Peter.

  You must be logged in to comment