Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of an LR grammar that cannot be represented by LL?

Tags:

parsing

ll

lr

All LL grammars are LR grammars, but not the other way around, but I still struggle to deal with the distinction. I'm curious about small examples, if any exist, of LR grammars which do not have an equivalent LL representation.

like image 987
Puppy Avatar asked Jan 10 '12 19:01

Puppy


People also ask

Can an LL 1 grammar be left recursive?

If a grammar contain left recursion it can not be LL(1) Eg - S -> Sa | b S -> Sa goes to FIRST(S) = b S -> b goes to b, thus b has 2 entries hence not LL(1) 3. If a grammar is ambiguous then it can not be LL(1) 4.

What is LL and LR grammar?

First L of LL is for left to right and second L is for leftmost derivation. L of LR is for left to right and R is for rightmost derivation. It follows the left most derivation. It follows reverse of right most derivation. Using LL parser parser tree is constructed in top down manner.

How do you check if a grammar is LR?

A grammar is LR(1) if the following two conditions are satisfied for each configurating set: 1. For any item in the set [A –> u•xv, a] with x a terminal, there is no item in the set of the form [B –> v•, x]. In the action table, this translates no shiftreduce conflict for any state.

What is LR grammar?

LR parser is a bottom-up parser for context-free grammar that is very generally used by computer programming language compiler and other associated tools.


1 Answers

Well, as far as grammars are concerned, its easy -- any simple left-recursive grammar is LR (probably LR(1)) and not LL. So a list grammar like:

list ::= list ',' element | element

is LR(1) (assuming the production for element is) but not LL. Such grammars can be fairly easily converted into LL grammars by left-factoring and such, so this is not too interesting however.

Of more interest is LANGUAGES that are LR but not LL -- that is a language for which there exists an LR(1) grammar but no LL(k) grammar for any k. An example is things that need optional trailing matches. For example, the language of any number of a symbols followed by the same number or fewer b symbols, but not more bs -- { a^i b^j | i >= j }. There's a trivial LR(1) grammar:

S ::= a S | P
P ::= a P b | \epsilon

but no LL(k) grammar. The reason is that an LL grammar needs to decide whether to match an a+b pair or an odd a when looking at an a, while the LR grammar can defer that decision until after it sees the b or the end of the input.

This post on cs.stackechange.com has lots of references about this

like image 50
Chris Dodd Avatar answered Oct 04 '22 21:10

Chris Dodd