LR(k) grammars are grammars that can be parsed bottom-up from the left-to-right, producing a rightmost derivation, using k tokens of lookahead. LR(k) parsers are among the most powerful deterministic parsers available, but are often too large to use in practice.
Questions tagged [lr-grammar]
178 questions
4
votes
1 answer
How to solve LR(1) grammar ambiguity between ternary expressions (a ? b : c) and "maybe" expressions (a?)?
I have created a grammar, a stripped-down version of which is reproduced below:
(0) exp1: ternary;
(1) exp1: exp2;
(2) ternary: exp2 "?" exp1 ":" exp1;
(3) exp2: exp2 "+" exp3;
(4) exp2: exp3;
(5) exp3: maybe;
(6) exp3: "1";
(7) maybe: exp3 "?";
I…

user541686
- 205,094
- 128
- 528
- 886
4
votes
1 answer
What's are reasonable upper bounds for the number of states, symbols, and rules of LR(1) grammars?
I'm making an LR(1) parser, and I've run across performance bottlenecks in various places.
I'd like to try optimizing the the data structures for the parser, but in order to do so, I need a rough idea of how many states, rules, and terminal symbols…

user541686
- 205,094
- 128
- 528
- 886
4
votes
3 answers
LR(k) or LALR(k) parser generator with features similar to ANTLR
I'm currently in the process of writing a parser for some language. I've been given a grammar for this language, but this grammar has some left recursions and non-LL(*) constructs, so ANTLR doesn't do very well, even with backtracking.
Because…

Jost
- 5,948
- 8
- 42
- 72
4
votes
2 answers
How can a lexer extract a token in ambiguous languages?
I wish to understand how does a parser work. I learnt about the LL, LR(0), LR(1) parts, how to build, NFA, DFA, parse tables, etc.
Now the problem is, i know that a lexer should extract tokens only on the parser demand in some situation, when it's…

unomadh
- 183
- 1
- 8
3
votes
2 answers
Parsing the Context-free-Grammars
I know that a bottom-up parser is better than a top-down parser because it can accept left-recursive grammar, what can be other reasons that we prefer bottom-up parsing over top-down parsing?

Amna Ahmed
- 1,944
- 4
- 20
- 25
3
votes
1 answer
Every LR(0) grammar is SLR(1) but vice versa need not necessarily be true, why?
Every LR(0) grammar is SLR(1) but vice versa need not necessarily be true, why?

siddharth
- 579
- 1
- 8
- 18
3
votes
1 answer
How many ways are there to build a parser?
I am learning about the ANTLR v4, which is a parser generator based on so-called Adaptive LL(*) algorithm. It claims to be a big improvement over LL(*) algorithm, but I also heard about some algorithm like LR.
What's the advantage/limitation of…

smwikipedia
- 61,609
- 92
- 309
- 482
3
votes
2 answers
Is it possible to write a recursive-descent parser for this grammar?
From this question, a grammar for expressions involving binary operators (+ - * /) which disallows outer parentheses:
top_level : expression PLUS term
| expression MINUS term
| term TIMES factor
| term DIVIDE…

user200783
- 13,722
- 12
- 69
- 135
3
votes
2 answers
Resolving reduce/reduce conflicts
We have a CFG grammar and we construct LR(1) Parsing Table. We see that one cell on the parsing table have a reduce - reduce conflict. Is it possible to solve this conflict by using more input symbols of lookahead at each step? I am asking this…

Iakob Fokas
- 193
- 1
- 2
- 6
3
votes
1 answer
Generate the LR parse table from the Lemon Parser Generator
I'm trying to generate the parser table using the lemon parser generator, but the .out file generated when I run lemon grammar.y only contains the states of the automaton.
Is there a way to also get the goto table for non-terminals, not only the…

Paul
- 20,883
- 7
- 57
- 74
3
votes
3 answers
Python lrparsing module; cannot parse simple recursive grammar
expr = Ref('expr')
block = '{' + Repeat(expr) + '}'
expr = block | Token(re='[0-9]')
START = expr
Here's a grammar using the lrparsing module for Python. The module reports no conflicts in the grammar.
It fails to parse the string {{0}} with the…

user
- 4,920
- 3
- 25
- 38
3
votes
1 answer
Real-world LR(k > 1) grammars?
Making artificial LR(k) grammars for k > 1 is easy:
Input: A1 B x
Input: A2 B y (introduce reduce-reduce conflict for terminal a)
A1 : a
A2 : a
B : b b b ... b (terminal b occurs k-1 times)
However, are there…

user541686
- 205,094
- 128
- 528
- 886
3
votes
0 answers
LR(k) parse table construction: Is it possible to calculate the lookahead sets on demand?
I've began to read about LR(k) parse table construction, and all texts explaining the algorithm for k > 0 suggest that the lookaheads should be calculated for every symbol before generating the item sets, then when all item sets are generated, the…

Thiago Padilha
- 4,590
- 5
- 44
- 69
3
votes
1 answer
SLR(1) or LR(1) Parsing
We find follow(A) in case we find a production of type
A → α
Can α here be ε?
Like In the below example:
P → aPa | bPb | ε
If α could be ε, it is not LR(1)

sgoenka1
- 53
- 7
3
votes
1 answer
How to transform this grammar to LR(1)?
I have a grammar with an LR(1) conflict which I cannot resolve; yet, the grammar should be unambiguous. I'll first demonstrate the problem on a simplified grammar with five tokens: (, ), {}, , and id.
The EBNF would look like this:
args = ( id…

Catherine
- 22,492
- 3
- 32
- 47