Questions tagged [lr-k]
LR(k) is a common type of parser for context-free grammars
46
questions
1
vote
1
answer
61
views
How is the lookahead for an LR(1) automaton computed?
I'm reading Douglas Thain's book called "Introduction to Compilers and Language Design" and I've come across the following grammar:
, with the following LR(1) automaton:
, and I am not ...
-1
votes
1
answer
29
views
What is the time complexity of SLR and LALR parsers?
What is the difference between time complexity of SLR(k) and SLR(1)? and also what about LALR(k) and LALR(1)?
How can I calculate time complexity of parsers? I think time complexity of LR(k) is O(n) ...
0
votes
1
answer
48
views
find LR(1) items of the first state
I need to calculate the LR(1) items of the following grammar:
S -> E
E -> E + T
E -> T
T -> ID
T -> ( E )
I can not even calculate the first group {[...
0
votes
0
answers
55
views
Why is the LR(1) automaton deterministic?
Let $G = (V, \Sigma, P, S)$ be an LR(1) grammar. We can define a non deterministic $\lambda$-automaton[1] as follows. First of all we extend the grammar with an additional symbol $S'$ which will be ...
0
votes
0
answers
33
views
How to prove a number of LR(0) states?
I have this grammar $G_n, n\ge 2$:
$$\begin{align}S&\to A_iB_i, 1\le i \le n\\
A_i&\to a_jA_i\mid a_j, 1\le i\land j\le n\land j\ne i\\
\end{align}
$$
How can I prove that this grammar has ...
0
votes
1
answer
81
views
Reduce-reduce conflict in SLR vs LALR
I was wondering if I could say any of the following is true.
Given a grammar $G$,
If the LALR parser has reduce-reduce conflict for $G$, then the SLR parser also has reduce-reduce conflict for $G$.
...
1
vote
1
answer
82
views
Why can't a LR(0) parser have GOTO and Reduce Items in the same canonical set?
I am talking in reference to this question from this compiler test. I think it is actually true that a GOTO and REDUCE can't be present in the same set, but am having a hard time coming up with an ...
0
votes
1
answer
35
views
Merging states in LALR parser
I was learning LALR parsers from the dragon book (Compilers: Principles, Techniques, and Tools by Alfred V. Aho, Monica S. Lam, Ravi Sethi, and Jeffrey D. Ullman) and I came across the example on page ...
2
votes
1
answer
51
views
How does LALR(1) parser behave compared to LR(1) paser?
In Section 4.7.4 of the book "Compilers: Principles, Techniques, and Tools" (2nd Edition), it reads:
"The revised parser (LALR(1)) behaves essentially like the original (LR(1)), .... ...
0
votes
1
answer
124
views
What to do following a reduce in LR(1) parsing?
I am using this standard question from Dragonbook as an example, (the first problem) . I have trouble with what happens in State 4 on LR(1) parsing. Once it is reduced by the rule C->d, now what ...
1
vote
0
answers
29
views
Is there a direct way to obtain the RE for the handle-finding DFA of a grammar?
LR parser for a (CFG) grammar uses a handle-find automaton (which is a DFA) to find the handles. Such automata can be constructed by computing the canonical collection of sets of LR(0)/LR(1) items.
Is ...
1
vote
1
answer
53
views
What are cases where we cannot perform LR parsing without augmented grammar?
Many standard Compiler Design texts mention constructing augmented grammar as first step of LR parsing.
The reason frequently is
It is required in cases where start symbol comes on right hand of any ...
0
votes
1
answer
21
views
Why shift means push terminal symbol and state into the stack? Why not push only state?
Shift means that you need to put the status and terminal symbol on the stack. But what is the terminal symbol used for? After all, if the reduce command is executed, both the state and the terminal ...
0
votes
1
answer
37
views
How to create a LR(k) grammar for an arbitrary k
Is there a simple procedure for constructing a grammar that is LR(k) but not LR(k-1) for any k?
0
votes
1
answer
889
views
Is following grammar LR(0)?
I know how to verify whether grammar is LR(0) or not. But this particular case is little tricky and hence the question.
Grammar:
$SL \rightarrow SL ; S \space | \space \epsilon$
$S \...
0
votes
1
answer
325
views
Analysis of a LALR shift-reduce conflict
I have created an extremely simple byacc grammar (ok, it's not really that simple but I simplified purposefully to be a minimal complete verifiable example):
...
0
votes
2
answers
520
views
Are shift and goto moves for all LR parsers ( LR(0), SLR(1),CLR(1),LALR(1) ) same?
I understand the difference in the parsing tables of the above 4 parsers.
I understand that CLR>LALR>SLR>LR(0) in terms of power.
Are shift and goto moves for all LR parsers ( LR(0), SLR(1),CLR(1),...
1
vote
1
answer
74
views
Wikipedia says this grammar is LR(0), but Grammophone says it is not; is it?
E -> E * B .
E -> E + B .
E -> B .
B -> 0 .
B -> 1 .
I am confused because Wikipedia cites this grammar as an example of an LR(0) grammar ...
0
votes
1
answer
165
views
Is there a LL(K) Grammar which is not LALR(K) Grammar?
It is easy to know that there are LALR(K) grammars which are not LL(K) because any grammar with left recursion which is LALR(K), is not LL(K) because all LL(K) grammar must be left recursion free. And ...
2
votes
1
answer
1k
views
When will SLR(1) parser fail but CLR(1) will not
Dragon book says following about where SLR(1) parser can fail but CLR(1) wont:
Recall that in the SLR method, state $i$ calls for reduction by $A→α$ if the set of items $I_i$ contains item $[A→α.]$...
1
vote
1
answer
643
views
LR parsers and ambiguous and non deterministic grammars
Dragon book says:
An ambiguous grammar can never be LR.
And then immediately further it says:
For example, consider the dangling-else grammar:
$\begin{align} stmt \rightarrow & \textbf{...
3
votes
1
answer
1k
views
What is handle in bottom up LR parsing?
I was taking course on compilers by Alex Aiken. You can find the slide discussing handles on this page. On the page of the slide, the instructor defines handle as follows:
Assume a rightmost ...
1
vote
0
answers
804
views
Are LL(1) grammars a subset of LR(0) grammars?
As far as I've understood it seems that LL1 are a subset of LR0, but studying the following grammar I end up in a contraddiction.
Grammar :
S -> (S) | [S] | {S} | ε
This is an LL1 while it seems ...
1
vote
1
answer
147
views
5
votes
1
answer
1k
views
can every LR(k) grammer be transformed to LR(1)
when i was studying compilers i came across this in Wikipedia every DCFL has a corresponding unambiguous grammar and hence can be parsed by an LR(1) parser which is the most powerful parser then what ...
1
vote
0
answers
247
views
Is the Java 8 Grammar still a LALR(1) grammar?
As I found out here: http://www.eclipse.org/jdt/core/howto/generate%20parser/generateParser.html , the Java Version of 2005 (might be Java 6, I'm not sure) is a LALR(1) grammar. However, I couldn't ...
2
votes
0
answers
218
views
Intuitive Explanation on Converting BNF Grammar to LR(1)
Consider the following BNF Grammar G:
...
7
votes
2
answers
2k
views
DCFL with prefix property have LR(0) grammar?
There are two important theorems about LR(k) grammars and DCFL. Mentioned here.
A language has an LR(1) grammar iff it is DCFL.
A language has an LR(0) grammar iff it is DCFL and has prefix property.
...
6
votes
1
answer
856
views
Are LR(k) languages and DCFLs equivalent?
In the familiar book of Theory of Computation by M. Sipser, the author proved that for endmarked context-free languages, the set of languages having a LR(k) grammar for a predefined $k \in \mathbb{N}$ ...
0
votes
1
answer
67
views
Can I mix LL and LR parsing?
I want to go RD parsing with Shunting Yard for expression (for the sake of easier operator precedences and less left-recusion), is that considered no-good because nobody did that before?
3
votes
1
answer
154
views
Is $\{a^nb^n\}\cup\{a^nb^{2n}\}$ LR(k)?
I was reading Knuth's paper "On The Translation of Languages from Left to Right", my particular interest being on RL($k$) languages (not a typo). By the end of the paper, he puts the grammar:
$$
S \...
2
votes
1
answer
3k
views
Need for an augmented grammar
The question is under the concept of LR parser.
Suppose the original grammar is
S->AA
A->aA|b
Is it necessary to add an additional rule S'->S to make it as augmented grammar?
In short, I want to ...
2
votes
0
answers
366
views
How to find conflicts in LALR parser?
I have a LR analyser and I can't find out how to know whether the grammar is or is not LALR(1).
I think that I have to merge states with the same rules. So I have to merge states: $2 - 12, 3 - 13, 4 -...
2
votes
0
answers
38
views
Determine if two LR(1) action/goto tables always produce the same derivation
I am experimenting with implementing minimal LR(1) algorithms/techniques, but I find it very difficult to verify the correctness of what I've implemented.
Is there a fast algorithm to determine if ...
7
votes
1
answer
93
views
Is THEADS the same as FIRST?
I'm reading the paper "A Practical General Method for Constructing LR(k) Parsers" by David Pager, and it contains the following paragraph:
We will first of all briefly review the LR(1) ...
1
vote
1
answer
921
views
How to remove empty strings a.k.a (lambda, epsilon) from a LR(0) grammar
Say I have the following grammar:
E -> TE'
E' -> +TE'| -TE' | λ
T -> (E) | id
I need to build the finite state machine with the LR(0) items,
But I ...
4
votes
1
answer
147
views
Item lookaheads versus dot lookaheads for $LR(k)$ with $k \gt 1$?
I was reading "Parsing Techniques: A Practical Guide, Second Edition" by Grune and Jacobs, which details a bunch of different parsing algorithms. In the section on $LR(2)$ parsing, they mention that ...
4
votes
1
answer
201
views
An intuitive explanation for LR grammars?
With LL grammars, there seems to be a very intuitive explanation, in terms of first and follow tables.( The first letter/s encountered in a particular state, can only mean a particular rule). With LR ...
2
votes
2
answers
288
views
Use DFA minimization on LR(1) states
I have recently started reading more about context-free parsing techniques, in particular LR parsing.
As I have read, LR state transition graph (or table(s)) used for finding handles in sentential ...
1
vote
0
answers
26
views
How equality of item (production state) is defined in LR parser?
Whenever I read about equality I took it for granted, but few days ago I wondered if my understanding is not too shallow. Consider such example (taken from: http://david.tribble.com/text/honalee.html):...
0
votes
1
answer
218
views
Chomsky NF or Greibach NF benefits for LR parser?
Are there any benefits of having grammar in CNF/GNF for LR parser? I have those in mind:
total speed of creating a parser (that is time for creating CNF/GNF out of the grammar, and then creating ...
4
votes
1
answer
174
views
Are there automatic transformation of grammar algorithms?
From time to time you can write a grammar that creates shift/reduce conflict (for example) but after reading the grammar you notice that if you rewrite the productions the conflict will go away, ...
11
votes
2
answers
762
views
How much bigger can an LR(1) automaton for a language be than the corresponding LR(0) automaton?
In an LR(0) parser, each state consists of a collection of LR(0) items, which are productions annotated with a position. In an LR(1) parser, each state consists of a collection of LR(1) items, which ...
1
vote
1
answer
399
views
Is this grammar LR(1)?
A bit confused about whether this grammar is ambiguous or not
C' -> C
C -> d C u C
C -> d C
C -> ε
I tried building the DFA for this but I get this ...
0
votes
1
answer
223
views
How to create an LR($k$) grammar for a given language?
How can I construct an LR($k$) grammar (which $k$ is not really of interest for now) for a given language, say:
$$L=\left\{w \in \{a, b\}^\star \ | \ \exists u \in \{a, b\}, v \in \{a, b\}^\star: w = ...
13
votes
2
answers
6k
views
Is there any way to distinguish between LL(k) and LR(k) grammar?
I am recently studying about Compilers designing. I came to know about two types of grammar one is LL grammar and other is LR grammar.
We also know the facts that every LL grammar is LR that is LL ...