Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Detection of ambiguous grammar
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
maxime-esa  
View profile  
 More options May 30 2012, 8:49 am
From: maxime-esa <maxime1...@gmail.com>
Date: Wed, 30 May 2012 05:49:25 -0700 (PDT)
Local: Wed, May 30 2012 8:49 am
Subject: Detection of ambiguous grammar
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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
andrew cooke  
View profile  
 More options Jun 2 2012, 2:20 pm
From: andrew cooke <and...@acooke.org>
Date: Sat, 2 Jun 2012 14:20:29 -0400
Local: Sat, Jun 2 2012 2:20 pm
Subject: Re: [LEPL] Detection of ambiguous grammar

Hi,

[Sorry seems this was waiting a few days in the Google groups spam filter]

Lepl is a recursive descent parser (like pyparsing, I think) and, as such, has
a well defined ordering when parsing grammar.  So it will resolve ambiguities
according to the order implicit in the way the matchers are defined.  The only
errors it will flag are left recursion and, by default, the failure to match
the entire input.

You ask how Lepl can be made to reject the grammar.  It cannot.

However, you can, with care, define the grammar so that the ordering resolves
ambiguities in the way you feel most appropriate.  For example,

Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.

>>> from lepl import *
>>> def build():

...   stmt = Delayed()
...   with DroppedSpace():
...     expr = Literal('EXPR')
...     end = Literal ('endif')
...     cond = stmt | end
...     stmt += ('if' & expr & 'then' & cond & Optional('else' & cond) > list)
...     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']]]

where the nesting shows how the ambiguity was resolved.

Andrew


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »