Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a Grammar LL(1)

I have the following grammar:

S → a S b S | b S a S | ε

Since I'm trying to write a small compiler for it, I'd like to make it LL(1). I see that there seems to be a FIRST/FOLLOW conflict here, and I know I have to use substitution to resolve it, but I'm not exactly sure how to go about it. Here is my proposed grammar, but I'm not sure if it's correct:

S-> aSbT | epsilon

T-> bFaF| epsilon

F-> epsilon

Can someone help out?

like image 762
John Roberts Avatar asked Mar 01 '13 15:03

John Roberts


People also ask

What are the conditions for a grammar to be LL 1?

Recognizing LL(1): Here are two properties we know must be true of a grammar if it is to be LL(1): the grammar must not be left recursive. the rule which should be chosen when developing a nonterminal must be determined by that nonterminal and the (at most) next token on the input.

How do you check a grammar is LL 1 or not?

A grammar whose parsing table has no multiply-defined en- tries is said to be LL(1) which stands for: scanning the input from Left to right producing a Leftmost derivation and using 1 input symbol of lookahead at each step to make parsing action decisions.

Can an ambiguous grammar be LL 1?

Ambiguous grammars are not LL(1) but unambiguous grammars are not necessarily LL(1) Having a non-LL(1) unambiguous grammar for a language does not mean that this language is not LL(1). But there are languages for which there exist unambiguous context-free grammars but no LL(1) grammar.


1 Answers

In his original paper on LR parsing, Knuth gives the following grammar for this language, which he conjectures "is the briefest possible unambiguous grammar for this language:"

S → ε | aAbS | bBaS

A → ε | aAbA

B → ε | bBaB

Intuitively, this tries to break up any string of As and Bs into blocks that balance out completely. Some blocks start with a and end with b, while others start with b and end with a.

We can compute FIRST and FOLLOW sets as follows:

FIRST(S) = { ε, a, b }

FIRST(A) = { ε, a }

FIRST(B) = { ε, b }

FOLLOW(S) = { $ }

FOLLOW(A) = { b }

FOLLOW(B) = { a }

Based on this, we get the following LL(1) parse table:

   |   a   |   b   |   $   
 --+-------+-------+-------
 S | aAbS  | bBaS  |  e
 A | aAbA  |   e   |
 B |   e   | bBaB  |

And so this grammar is not only LR(1), but it's LL(1) as well.

Hope this helps!

like image 92
templatetypedef Avatar answered Oct 16 '22 19:10

templatetypedef