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

I'm really struggling to unterstand the relationship between:
LR(0)
LL(0)
LALR(1)
SLR(1)
LR(1)
LL(1)
I'm pretty sure LALR(1) and SLR(1) are subsets of LR(1), but I'm lost about the others. Are they all exclusive? Is LL(0) a subset of LL(1)?
Thanks

The containment rules are the following:
Every LR(0) grammar is also SLR(1), but not all SLR(1) grammars are LR(0).
Every SLR(1) grammar is also LALR(1), but not all LALR(1) grammars are SLR(1).
Every LALR(1) grammar is also LR(1), but not all LR(1) grammars are LALR(1).
Every LL(1) grammar is also LR(1), but not all LR(1) grammars are LL(1).
Every LL(0) grammar is also LR(0), SLR(1), LALR(1), LR(1), and LL(1). (LL(0) grammars are basically useless; see this question for details why).
It's also the case that every language that has an LR(1) grammar also has an LR(0) grammar provided that you endmark the grammar, though the grammar isn't guaranteed to be pretty.

Related

LALR, LR(1) and SLR

Since LR (1) grammars are broader than LALR or SLR grammars, I have doubts regarding the proof of LR grammars. If a grammar is SLR or LALR, then does that mean that the grammar is also LR (1)?
In fact, since the method that uses lookaheads is more complex to carry out, I can therefore use the SLR method (without lookahead) or the LALR method to prove that a grammar is LR (1).
If a grammar is SLR(k) or LALR(k) then it also LR(k). If a conflict is found while constructing the LR parser, that same conflict will be present in an LALR or SLR parser for the same grammar.

SR conflict in LR(0) and SLR(1) will always be equal for some context free grammar?

I was told by someone that this statement is false, but I think it is true.
Consider the grammar which have no SR conflict in LR(0) then it should not have SR conflict in SLR(1) also because it is more powerful than LR(0). So in this case SR conflicts in both are equal.
So the given statement should be true.
Am I getting it wrong somewhere?
The LR(0) algorithm will produce shift-reduce conflicts in any state with a mixture of shift and reduce actions, because there is no lookahead available. An SLR(1) parser can resolve those conflicts when the token to be shifted is not in the FOLLOW set for the symbol to be reduced. Thus, the LR(0) parser has shift-reduce conflicts which are not in the SLR(1) parser for the same grammar, but every conflict in the SLR(1) parser is also in the LR(0) grammar.

Is it possible that going from LR(1) to LALR(1) introduces shift/reduce conflicts?

I am studying for a final in Language Theory and one question asks the following:
If you have a parsing table T in LR(1) and a parsing table T' in LALR(1) for the same grammar. Is it possible that T' introduces new shift/reduce conflicts that were not part of T?
My understanding is that LR(1) is "smarter" than LALR(1), so my gut is telling me the answer is "No". However, I'd like to know the correct answer and a proper justification.
Thank you in advance.
LR(1) is "smarter", so LALR(1) may have conflicts where LR(1) does not.
Given a conflict-free LR(1) grammar, though, LALR(1) will only introduce reduce/reduce conflicts, not shift/reduce conflicts.

Is every LL(1) grammar also an LR(0) grammar?

I know that every LL(1) is also an LR(1). But what about the relationship between LL(1) and LR(0), can a LL(1) be a LR(0) as well?
You ask two questions, one in the title and the other in the body of the post. Neither specify whether you are asking about languages or grammars, but the basic answers are the same:
Are all LL(1) languages LR(0)?
No. A language which contains both a string and a proper prefix of that string cannot be LR(0). But many LL(1) languages have that form.
Are some LL(1) languages LR(0)?
Sure.
(The unasked question) Are any LR(0) languages not LL(1).
Yes. For example, the language {ambnc | m≥n≥0} is LR(0), but it has no LL(1) grammar.

What is the difference between LALR and LR parsing? [duplicate]

This question already has answers here:
What is the difference between LR, SLR, and LALR parsers?
(9 answers)
Closed 3 years ago.
The community reviewed whether to reopen this question 4 months ago and left it closed:
Original close reason(s) were not resolved
I understand both LR and LALR are bottom-up parsing algorithms, but what's the difference between the two?
What's the difference between LR(0), LALR(1), and LR(1) parsing? How can I tell if a grammar is LR(0), LALR(1), or LR(1)?
At a high level, the difference between LR(0), LALR(1), and LR(1) is the following:
An LALR(1) parser is an "upgraded" version of an LR(0) parser that keeps track of more precise information to disambiguate the grammar. 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.
Any grammar that can be parsed with an LR(0) parser can be parsed with an LALR(1) parser and any grammar that can be parsed with an LALR(1) parser can be parsed with an LR(1) parser. There are grammars that are LALR(1) but not LR(0) and LR(1) but not LALR(1).
More formally, an LR(k) parser is a bottom-up parser that works by maintaining a stack of terminals and nonterminals. The parser is controlled by a finite automaton that determines, based on the current state of the parser and the next k tokens of input, whether to shift a new token onto the stack or reduce the top symbols of the stack by applying a production in reverse.
In order to keep track of enough information to make a determination about whether to shift or reduce, LR(k) parsers have each state correspond to a "configurating set," a set of productions annotated with the following information:
How much of the production has been seen so far, and
What tokens to expect after the production has been completed (the lookahead)
The first of these pieces of information is used to determine whether the parser may need to do a reduction - if none of the productions in a current state have been completed, there's no reason to do a reduction. The second of these pieces of information is used when doing a reduction to determine whether the reduction should be performed. When deciding whether to reduce, an LR(k) parser looks at the next k tokens of the input stream. If they match the lookahead tokens, the parser will reduce, and otherwise the parser does nothing.
Problems arise in an LR(k) parser when there are conflicts about what the parser should do in a given state. One type of conflict, a shift/reduce conflict, comes up when the parser is in a state where a production has been completed, but the lookahead symbols for that production conflict are also used by another uncompleted production in the state. This means that the parser can't tell whether to perform the reduction or not. A second type of conflict is a reduce/reduce conflict, where the parser knows it has to do a reduction, but two or more reductions are possible and it can't tell which to do.
Intuitively, as k gets larger and larger, the parser has more and more precise information available to it to determine when to shift and when to reduce. If a grammar is not LR(0), for example, the parser might have a state where given no lookahead at all it can't determine whether to shift or to reduce. However, that grammar might still be LR(1) because given an extra token of lookahead, it may be able to recognize that it should definitely shift and not reduce or definitely reduce and not shift.
The problem with LR(k) parsers is that as k gets larger, the number of states can increase exponentially. Lookahead in LR(k) parsers is handled by building more and more states in the parser to correspond to different combinations of productions and lookaheads, so as the number of possible lookaheads increases so does the number of states. Consequently, LR(1) parsers are commonly too large to be practical, and LR(2) or greater is almost unheard of in practice.
LALR(1) was invented as a compromise between the space efficiency of LR(0) parsers and the expressive power of LR(1) parsers. There are several ways to think about what an LALR(1) parser is. Originally, LALR(1) parsers were specified as a transformation that converts LR(1) automata into smaller automata. Although an LR(1) parser may have many more states than an LR(0) automaton, the only difference is that an LR(1) parser may have multiple copies of any particular state in an LR(0) automaton, each annotated with different lookahead information. An LALR(1) parser can be formed by starting with an LR(1) parser, then combining together all states that have the same "core" (the set of productions and their positions), then aggregating all the lookahead information together. This results in a parser that has the same number of states as an LR(0) parser but retains some amount of information about lookaheads to help avoid LR conflicts.
Another view of LALR(1) grammars uses the "LALR-by-SLR" method. LALR(1) parsers can be constructed by starting with an LR(0) parser for a grammar, then creating a new grammar for the language that annotates nonterminals with information about what states in the LR(0) parser they correspond to. The information about the FOLLOW sets of the nonterminals in that grammar can then be used to compute the lookaheads in the LR(0) parser.
The net result is that
LR(0) parsers are small, but not very expressive.
LALR(1) parsers are slightly larger due to the lookahead information, but very expressive.
LR(1) parsers are huge, but extremely expressive.
As for your second question - how do you determine whether a grammar is LR(1) or LALR(1) - the standard approach is to try to build the parsing automata for the LR(1) parser and LALR(1) parser and checking for conflicts. To build the LR(1) parser, you build up the LR(1) configurating sets, then check to see if any of those configurating sets have a shift/reduce conflict or reduce/reduce conflict. To construct an LALR(1) parser, you can either build the LR(1) parser and then condense configurating sets with the same core or can use the LALR-by-SLR method based on the LR(0) parser for the language. More details about how to construct these configurating sets are available in most compilers textbooks. You can also check out the lecture notes from a compilers course I taught in Summer 2012, which cover all of the above parsing methods and a few others.
Hope this helps!
LR(0), SLR(1), LALR(1) parsers all have the same number of states. Minimal LR(1) parsers will have a few more states if the grammar requires it, to avoid reduce-reduce conflicts.
Canonical LR(1) parsers will have many more states, too many for medium or large computer languages.
SLR(1) parser generators build an LR(0) state machine and determine the k=1 lookaheads by examining the grammar (which may report erroneous conflicts).
LALR(1) parser generators build an LR(0) state machine and determine the k=1 lookaheads by examining the LR(0) state machine (which is very complicated).
Canonical LR(1) parser generators build an LR(1) state machine.
Minimal LR(1) parser generators build an LR(1) state machine and merge compatible states during the build process.
The parsing algorithm for a good LALR(1) parser is different in two ways: (1) It should have shift-reduce actions, which reduces the number of states by about 30% and makes the parser faster, and (2) it must do one or more reductions when detecting a syntax error, which makes error recovery more complicated.
The parsing algorithm for a canonical LR(1) parser (1) does not have shift-reduce actions and (2) does not make any reductions when detecting a syntax error, which makes error recovery simpler.
There is another case, called minimal LR(1), which uses the same parsing algorithm and error recovery algorithm as LALR(1). Minimal LR(1) parsers offer the power of LR(1) and their size is almost as small as LALR(1). The LRSTAR Parser Generator creates minimal LR(1) parsers for C++ programmers.

Resources