Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is every LL(1) grammar also a LALR(1) grammar?

So one source says it is and another says its not

one source says this :

https://qph.fs.quoracdn.net/main-qimg-a94c48361571eeafdd5ba5fb63c24729-c

another says this :

enter image description here

the closest answer i found is this :

Relationship between LR(0), LL(0), LALR(1), etc?

but this doesn't answer the relation between LL(1) and LALR(1)

also if you can answer the more general question, which is what is the relation between LL(k) and LALR(k) it would be even more helpful

thanks.

like image 785
John Pence Avatar asked Mar 26 '18 13:03

John Pence


People also ask

Is LR 1 same as Lalr?

An LR(1) parser is a significantly more powerful parser that keeps track of even more precise information than an LALR(1) parser. LALR(1) parsers are a constant factor larger than LR(0) parsers, and LR(1) parsers are usually exponentially larger than LALR(1) parsers.

Is every Lalr 1 grammar need not be CLR 1?

Explanation: Statement 1:Every SLR(1) grammar is unambiguous but there are certain unambiguous grammars that are not SLR(1). As you can see in the diagram Not all Unambiguous grammars are SLR(1) but All SLR(1) grammars are Unambiguous. This Statement is True.

Does every grammar can be parsed by LL 1 grammar?

2 Answers. True : For every Regular Language there exists a LL(1) grammar. This is true because we can always get a right recursive grammar for any LL(1) that generates a Regular language.

Is every SLR 1 grammar an LR 1 grammar?

Every SLR(1) grammar is a canonical LR(1) grammar, but the canonical LR(1) parser may have more states than the SLR(1) parser.


1 Answers

The definitive answer (at least on the SE Network) can be found in this answer in the computing science site, where parsing theory questions are likely to attract better responses.

When reading the charts in that answer, note that there is a difference between containment relationships for grammars and containment relationships for languages. One of the clearest instances of this is the fact that all LR(k) grammars can be mechanically transformed into LR(1) grammars, with the consequence that there are only two categories of LR languages: LR(0) and LR(1). (In fact, you can reduce LR(k) languages to SLR(1), so the various algorithmic distinctions also disappear at the language level.) LL(k) languages, on the other hand, are a strict containment hierarchy. And the union of LL(k) languages (for finite k) is a strict subset of LR(1).

For grammars, though, the relationships are not so simple. Clearly, LL(k), LR(k), LALR(k), SLR(k), etc., form hierarchies, intuitively because it is not necessary to use all the lookahead information, and because for any grammar it is possible to add productions which require k+1 lookaheads (for both LL and LR algorithms).

LL(k) grammars are necessarily LR(k) but they are not necessarily LALR(k). There is an exercise in Appel's Modern Compiler Implementation textbook which provides an example of an LL(1) grammar which is not LALR(1); you can find the grammar transcribed in this answer. That should provide an idea on how to construct examples for k > 1. (Finding LALR(k) grammars which are not LL(k) is trivial: all you need is left recursion.)

like image 130
rici Avatar answered Oct 03 '22 23:10

rici