Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine whether a language is LL(1) LR(0) SLR(1)

Tags:

Is there a simple way to determine whether a grammar is LL(1), LR(0), SLR(1)... just from looking on the grammar without doing any complex analysis?

For instance: To decide whether a BNF Grammar is LL(1) you have to calculate First and Follow sets - which can be time consuming in some cases.

Has anybody got an idea how to do this faster? Any help would really be appreciated!

like image 842
Chris Avatar asked Jan 24 '09 12:01

Chris


People also ask

How do you identify whether a grammar is LL 1 LR 0 or SLR 1 )?

Some simple checks to see whether a grammar is LL(1) or not. Check 1: The Grammar should not be left Recursive. Example: E --> E+T. is not LL(1) because it is Left recursive. Check 2: The Grammar should be Left Factored.

When the grammar is said to be LL 1 or LR 1?

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.

How do you know if grammar is SLR?

A grammar is said to be SLR(1) if the following simple LR parser algorithm results in no ambiguity.

What is the difference between LR 0 SLR 1 and LR 1 parsers?

The only difference between LR(0) and SLR(1) is this extra ability to help decide what action to take when there are conflicts. Because of this, any grammar that can be parsed by an LR(0) parser can be parsed by an SLR(1) parser. However, SLR(1) parsers can parse a larger number of grammars than LR(0).


2 Answers

First off, a bit of pedantry. You cannot determine whether a language is LL(1) from inspecting a grammar for it, you can only make statements about the grammar itself. It is perfectly possible to write non-LL(1) grammars for languages for which an LL(1) grammar exists.

With that out of the way:

  • You could write a parser for the grammar and have a program calculate first and follow sets and other properties for you. After all, that's the big advantage of BNF grammars, they are machine comprehensible.

  • Inspect the grammar and look for violations of the constraints of various grammar types. For instance: LL(1) allows for right but not left recursion, thus, a grammar that contains left recursion is not LL(1). (For other grammar properties you're going to have to spend some quality time with the definitions, because I can't remember anything else off the top of my head right now :).

like image 130
Aaron Maenpaa Avatar answered Oct 10 '22 02:10

Aaron Maenpaa


In answer to your main question: For a very simple grammar, it may be possible to determine whether it is LL(1) without constructing FIRST and FOLLOW sets, e.g.

A → A + A | a

is not LL(1), while

A → a | b

is.

But when you get more complex than that, you'll need to do some analysis.

A → B | a
B → A + A

This is not LL(1), but it may not be immediately obvious

The grammar rules for arithmetic quickly get very complex:

expr → term { '+' term }
term → factor { '*' factor }
factor → number | '(' expr ')'

This grammar handles only multiplication and addition, and already it's not immediately clear whether the grammar is LL(1). It's still possible to evaluate it by looking through the grammar, but as the grammar grows it becomes less feasable. If we're defining a grammar for an entire programming language, it's almost certainly going to take some complex analysis.

That said, there are a few obvious telltale signs that the grammar is not LL(1) — like the A → A + A above — and if you can find any of these in your grammar, you'll know it needs to be rewritten if you're writing a recursive descent parser. But there's no shortcut to verify that the grammar is LL(1).

like image 26
Bruce Alderman Avatar answered Oct 10 '22 00:10

Bruce Alderman