World's most popular travel blog for travel bloggers.

[Solved]: Removing left-recursion in grammar while maintaining left-association of operator

, , No Comments
Problem Detail: 

I have a problem with this exercise:

Let G be the following ambiguous grammar for the λ-calculus:

E → v | λv.E | EE | (E)

where E is the single non-terminal symbol, λv.E represents abstraction w.r.t. the variable v in E, and EE represents application.

  1. Define an LL(1) grammar G′ such that L(G′) = L(G) and the ambiguity of G is resolved by imposing the following usual conventions:
    1. abstraction is right associative;
    2. application is left associative;
    3. application has higher priority than abstraction.
  2. Show the LL(1) parsing table for G′ and the parse tree obtained when parsing the string λv1. λv2. v1v2v1.

I eliminated ambiguity setting precedence and association, obtaining this grammar:

E -> EF | F F -> λv.G | G G -> (E) | v 

which is not LL(1), since the production E -> EF is left recursive. However, eliminating left recursion from that production I obtain:

E -> FE¹ E¹-> FE¹ | ɛ F -> λv.G | G G -> (E) | v 

that does not comply with requirement 1.2.

I looked for a solution on the Internet, but it seems like it is not possible to eliminate left-recursion preserving left associativity.

However, this exercise appeared on the compilers exam some years ago, so there must be a correct answer.

Thank you for your help.

Asked By : Marco DallaG

Answered By : babou

Compatibility of left associativity and LL(1) parsing

You just hit one of the major inconsistencies in the use of context-free (CF) syntax. People want to choose grammars so that the parse-tree will reflect the intended structure of the sentence, close to its semantics, especially in the case of non associative operators, such as application. This was pretty much the original intent of CF grammars in linguistics. But at the same time they are constraining themselves to parsing technology that will tolerate only some types of grammars.

Indeed, if the parse-tree is to reflect the left associativity of an operator, then the grammar is necessarily left-recursive, since the top application node in the parse-tree is necessarily adding the rightmost term of non-parethesized successive applications.. So LL parsing is out of the question. You are right.

There are two ways out of this. One is not to rely on the parser to give the rigth "parse-tree" to be used for later stages of processing (such as reducing the lambda expression, here). This lead to the concept of Abstract Syntax Trees (AST) that can be built from the parse-tree, but with a different structure.

The other solution is to use more general parsing Techniques that will accept any CF grammar, and parse in accordance with it. General CF parsers is a well developed technology (and I do not cease to wonder why LL remains so popular).

I have no idea what could be considered a proper answer to these contradicting requirements.

What I would do is show they are contradicting requirements. Give a first grammar that meets the associativity and priority constraints, then transform the grammar into and LL(1) grammar for the parsing.

The fact that something appeared in a journal or on an exam is not a total garantee that it is correct. And I might be wrong too ... but I did some checking, in addition to my own knowledge of the issue.

About this specific example

This being said, the first grammar you are suggesting does not seem quite correct. It does not have a way of producing λu.λv.v .

One trick to know is to start with operators (here abstraction or application) with the lowest priority (abstraction). It is the same for arithmetic expressions.

Best Answer from StackOverflow

Question Source : http://cs.stackexchange.com/questions/43066

0 comments:

Post a Comment

Let us know your responses and feedback