I am working on LR(1) items and I am having a bit of doubt and hoping someone can clarify for me. Given the following grammar, I must generate the LR(1) items. I generated the first item but am unsure if its correct, so before I continue, I want to make sure I have the first one correct. If anyone can help me/clarify for me, I'd really appreciate it. Thank you. Here is what I have:
Augmented Grammar:
S' -> S
S -> [ ]
S -> [ S ]
S -> S S
I0: S' -> • S, $
S -> • [ ], $
S -> • [ S ], $
S -> • S S, $
Related
Lets say we have this grammar
B -> S
S -> ( S )
S -> S S
S -> c
This is grammar which is not LR(0) . I have built the parse table and found that it is LR(0) . But The question is asking , If there is a conflict fix and make it LR(0). I don't know what does it mean? Could someone help me out? How to proceed?
Question:
Given the following grammar, fix it to an LR(O) grammar:
S -> S' $
S'-> aS'b | T
T -> cT | c
Thoughts
I've been trying this for quite sometime, using automatic tools for checking my fixed grammars, with no success. Our professor likes asking this kind of questions on test without giving us a methodology for approaching this (except for repeated trying). Is there any method that can be applied to answer these kind of questions? Can anyone show this method can be applied on this example?
I don't know of an automatic procedure, but the basic idea is to defer decisions. That is, if at a particular state in the parse, both shift and reduce actions are possible, find a way to defer the reduction.
In the LR(0) parser, you can make a decision based on the token you just shifted, but not on the token you (might be) about to shift. So you need to move decisions to the end of productions, in a manner of speaking.
For example, your language consists of all sentences { ancmbn$ | n ≥ 0, m > 0}. If we restrict that to n > 0, then an LR(0) grammar can be constructed by deferring the reduction decision to the point following a b:
S -> S' $.
S' -> U | a S' b.
U -> a c T.
T -> b | c T.
That grammar is LR(0). In the original grammar, at the itemset including T -> c . and T -> c . T, both shift and reduce are possible: shift c and reduce before b. By moving the b into the production for T, we defer the decision until after the shift: after shifting b, a reduction is required; after c, the reduction is impossible.
But that forces every sentence to have at least one b. It omits sentences for which n = 0 (that is, the regular language c*$). That subset has an LR(0) grammar:
S -> S' $.
S' -> c | S' c.
We can construct the union of these two languages in a straight-forward manner, renaming one of the S's:
S -> S1' $ | S2' $.
S1' -> U | a S1' b.
U -> a c T.
T -> b | c T.
S2' -> c | S2' c.
This grammar is LR(0), but the form in which the end-of-input sentinel $ has been included seems to be cheating. At least, it violates the rule for augmented grammars, because an augmented grammar's base rule is always S -> S' $ where S' and $ are symbols not used in the original grammar.
It might seem that we could avoid that technicality by right-factoring:
S -> S' $
S' -> S1' | S2'
Unfortunately, while that grammar is still deterministic, and does recognise exactly the original language, it is not LR(0).
(Many thanks to #templatetypedef for checking the original answer, and identifying a flaw, and also to #Dennis, who observed that c* was omitted.)
Recently I faced the problem for finding first and follow
S->cAd
A->Ab|a
Here I am confused with first of A
which one is correct {a} , {empty,a} as there is left recursion in A's production .
I am confused whether to include empty string in first of A or not
Any help would be appreciated.
-------------edited---------------
what wil be the first and follow of this ,,This is so confusing grammar i have ever seen
S->SA|A
A->a
I need to prove this grammar is not in LL(1) using parsing table but unable to do because i didnot get 2 entry in single cell.
Firstly,you'll need to remove left-recursion leading to
S -> cAd
A -> aA'
A' -> bA' | epsilon
Then, you can calculate
FIRST(A) = a // as a is the only terminal nderived first from A.
EDIT :-
For your second question,
S -> AS'
S' -> AS' | epsilon
A -> a
FIRST(A) = a
FIRST(S) = a
FIRST(S') = {a,epsilon}.
The idea of removing left-recursion before calculating FIRST() and FOLLOW() can be learnt here.
I have the following grammar, which I'm told is LR(1) but not SLR(1):
S ::= a A | b A c | d c | b d a
A ::= d
I don't understand why this is. How would you prove this?
I don't have enough reputation to comment on the above answer, and I'm a bit late to this question, but...
I've seen this grammar as an example elsewhere and the OP actually made a typo. It should be:
S ::= A a | b A c | d c | b d a
A ::= d
i.e., the very first clause of S is 'A a', not 'a A'.
In this case the FOLLOWS set for A is { $, a, c} and there is an SLR conflict in state 8.
One way to figure this out would be to try to construct LR(1) and SLR(1) parsers for the grammar. If we find any shift/reduce or reduce/reduce conflicts in the course of doing so, we can show that the grammar must not belong to one of those classes of languages.
Let's start off with the SLR(1) parser. First, we need to compute the LR(0) configurating sets for the grammar. These are seen here:
(1)
S -> .aA
S -> .bAc
S -> .dc
S -> .bda
(2)
S -> a.A
A -> .d
(3)
S -> aA.
(4)
A -> d.
(5)
S -> b.Ac
S -> b.da
A -> .d
(6)
S -> bA.c
(7)
S -> bAc.
(8)
S -> bd.a
A -> d.
(9)
S -> bda.
(10)
S -> d.c
(11)
S -> dc.
Next, we need to get the FOLLOW sets for all the nonterminals. This is shown here:
FOLLOW(S) = { $ }
FOLLOW(A) = { $, c }
Given this, we can go back and upgrade the LR(0) configurating sets into SLR(1) configurating sets:
(1)
S -> .aA [ $ ]
S -> .bAc [ $ ]
S -> .dc [ $ ]
S -> .bda [ $ ]
(2)
S -> a.A [ $ ]
A -> .d [ $, c ]
(3)
S -> aA. [ $ ]
(4)
A -> d. [ $, c ]
(5)
S -> b.Ac [ $ ]
S -> b.da [ $ ]
A -> .d [ $, c ]
(6)
S -> bA.c [ $ ]
(7)
S -> bAc. [ $ ]
(8)
S -> bd.a [ $ ]
A -> d. [ $, c ]
(9)
S -> bda. [ $ ]
(10)
S -> d.c [ $ ]
(11)
S -> dc. [ $ ]
Interestingly enough, there are no conflicts here! The only possible shift/reduce conflict is in state (8), but there is no conflict here because the shift action is on a and the reduce is on $. Consequently, this grammar actually is SLR(1). Since any SLR(1) grammar is also LR(1), this means that the grammar is both SLR(1) and LR(1).
Hope this helps!
I thought about writing a web-app to determine first- and follow-sets for CFGs and also LR(0), SLR(1) and LR(1) tables. This would have allowed you to try it out easily.
But luckily I googled first and found that such a tool already exists (I didn't necessarily expect this to be the case). You can find the tool here:
http://smlweb.cpsc.ucalgary.ca/
It expects the input in the following format:
S -> a A | b A c | d c | b d a.
A -> d.
Using this tool, I have verified what others have already stated: the grammar in question is SLR(1). (I give -1 to the question for this).
After the modification suggested by Toby Hutton, it isn't SLR(1) anymore, but still LR(1).
1)The given grammar is LL(1) in top down parsing, and LALR(1) in bottom up parsing.
2)while you are creating the parsing table, and the parsing table has No multiple entries, then the grammar tends to attend LALR(1).
3)If your parsing table has multiple entries(i mean the conflict occurrence), then the grammar is said to be SLR(1).
4)A grammar is said to be LR(1) since it's parsing table or ACTION/GOTO table has no conflict, and we all know that during a conflict occurrence in LR(1) we merge the data and we get the LALR.
LR(0) OR SLR OR SLR(1) are same
LR(1) OR CLR are same
LALR OR LALR(1) are same
the (1) parameter defines the inner build efficiency type of the syntax analyzer.
thank you.
I'm currently constructing LR(1) states from the following grammar.
S->AS
S->c
A->aA
A->b
where A,S are nonterminals and a,b,c are terminals.
This is the construction of I0
I0: S' -> .S, epsilon
---------------
S -> .AS, epsilon
S -> .c, epsilon
---------------
S -> .AS, a
S -> .c, c
A -> .aA, a
A -> .b, b
And I1.
From S, I1: S' -> S., epsilon //DONE
And so on. But when I get to constructing I4...
From a, I4: A -> a.A, a
-----------
A -> .aA, a
A -> .b, b
The problem is
A -> .aA
When I attempt to construct the next state from a, I'm going to once again get the exact same content of I4, and this continues infinitely. A similar loop occurs with
S -> .AS
So, what am I doing wrong? There has to be some detail that I'm missing, but I've browsed my notes and my book and either can't find or just don't understand what's wrong here. Any help?
I'm pretty sure I figured out the answer. Obviously, states can point to each other, so that eliminates the need to create new ones if it's content already exists. I'd still like it if someone can confirm this, though.