You ask how Lepl can be made to reject the grammar. It cannot.
On Wed, May 30, 2012 at 05:49:25AM -0700, maxime-esa wrote:
> Dear LEPL experts,
> In trying to find a good parser for Python to implement a rather
> complex grammar. I already know that Pyparsing will dot detect the
> ambiguities of my grammar, and so far only ANTLR does. But as I am
> trying to avoid having to compile the grammar before I can use it, I
> am now looking at LEPL, which seems quite powerful.
> However, on my first tests, I am facing the same issue as with
> Pyparsing - I hope someone can tell me if I am doing things wrong or
> if LEPL will ever be able to detect my grammar error.
> Here is what I wrote:
> from def build():
> stmt = Delayed()
> with DroppedSpace():
> expr = Literal('EXPR')
> end = Literal ('endif')
> cond = stmt | end
> stmt += 'if' & expr & 'then' & cond & Optional('else' & cond)
> line = Trace(stmt) & Eos()
> return line.get_parse()
> parser = build()
> print parser('if EXPR then if EXPR then endif else endif')
> ['if', 'EXPR', 'then', 'if', 'EXPR', 'then', 'endif', 'else', 'endif']
> Basically, this result is wrong. According to the grammar, the "else"
> can belong either to the first "if" statement or to the second one. I
> would expect the parser to detect this ambiguity and reject the
> grammar.
> The following is the equivalent grammar I wrote in ANTLR:
> cond : stmt | END;
> stmt : 'if' EXPR 'then' cond ('else' cond)* ;
> EXPR : 'EXPR';
> END : 'endif';
> And when I compile it I get the following error:
> warning(200): test.g:18:42: Decision can match input such as "'else'"
> using multiple alternatives: 1, 2
> As a result, alternative(s) 2 were disabled for that input
> error(201): test.g:18:42: The following alternatives can never be
> matched: 2
> I hope this is clear and makes sense - so my question: is my grammar
> badly specified? How would I need to write it so that LEPL would raise
> an error ?
> Thanks
> --
> You received this message because you are subscribed to the Google Groups "lepl" group.
> To post to this group, send email to lepl@googlegroups.com.
> To unsubscribe from this group, send email to lepl+unsubscribe@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/lepl?hl=en.