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
0
votes
1 answer
How does gcc/clang parse c++ if it can't be parsed by a LR(1) parser?
GCC/Clang are handwritten parsers. I read a post saying that C++ can't be parsed by an LR(1) parser (Why can't C++ be parsed with a LR(1) parser?). If so, how come GCC/Clang are handwritten recursive descent parsers, when LR(1) is more powerful than…

xilpex
- 3,097
- 2
- 14
- 45
0
votes
1 answer
LR(1) parser - Making inline productions
How would you implement inline productions in an LR(1) parser? By inline productions I mean the production is there, but only for parsing-- it won't get generated in the parse tree/AST. How would I implement this? I have a list of inline…

xilpex
- 3,097
- 2
- 14
- 45
0
votes
1 answer
LR parser for epsilon
I've recently implemented an LR(1) parser (without epsilon), and have been wondering how I would implement epsilon in the parsing algorithm (note: not the table construction algorithm). This is my grammar:
start -> A b
b -> B | \epsilon
I've…

xilpex
- 3,097
- 2
- 14
- 45
0
votes
1 answer
LR Parsing for arithmetic expressions
I have got LR grammar and LR table for expressions like (1+1),1+(a+1)
0: E’->E
1: E ->E + T
2: E-> T
3: T ->T * F
4: T-> F
5: F ->( E )
6: F-> id
string[,] ActionTable =
//+ * ( ) id end E T F
{
…

azh
- 13
- 1
- 1
- 4
0
votes
1 answer
How do I check if grammar is SLR(1)?
How do I check if this grammar is SLR(1)?
S' -> S
S -> [ B
A -> int
A -> [ B
B -> ]
B -> C
C -> A ]
C -> A , C
First I've created it's automaton, then computed the follow sets for non-terminals and then created the…

Octavian
- 33
- 2
- 7
0
votes
2 answers
How can LR parsers generate parse trees?
Suppose I have a grammar:
S -> Aaa
A -> a | ε
Clearly, this grammar can parse only sequences aa and aaa. A simple LR(1) parser (or even LL) can parse these when transformed into an equivalent grammar:
S -> aaA
A -> a | ε
Although these grammars…

Dennis
- 323
- 7
- 17
0
votes
1 answer
How easy is to find a string that leads to conflict in a SLR(1) parser compared to a LR(1)
It is known that SLR(1) parsers usually have less states than LR(1). But is it easier or harder because of this to find a string that leads to conflict in a SLR(1) parser compared to a LR(1) and why?
Thank you in advance.

Dragonite LvL 100
- 11
- 1
- 7
0
votes
2 answers
How to calculate expression result using LR parser in C
I am writing a program for calculating the expression value using LR parser (2 stack-track). The code is working fine for single digits. It adds or multiplies two integers from 0-9 but for double digits like 12+5*10 it fails. The code is below. I…

Ammar Ul Hassan
- 826
- 1
- 18
- 48
0
votes
1 answer
Is these 2 grammars equal?
I have the following grammar which is ambiguous and of course not slr1:
E -> E+A+A | E+A-A | E-A+A | E-A-A | T
T -> T+A | T-A | A
A -> A*B | A/B | B
B -> (E) | x
I used the rule of transformation which is:
E -> E + T -----> E -> TE'
…

Kostas Ayfantop
- 53
- 1
- 1
- 10
0
votes
0 answers
solving the shift/reduce conflict
I am currently having the following grammar which I am asked if it is SLR(1) or not. The grammar is:
E -> E + A + A | E - A + A | E + A - A | E - A - A | T .
T -> T + A | T - A | A .
A -> A * B | A / B | B .
B -> ( E ) | x .
The grammar is…

Kostas Ayfantop
- 53
- 1
- 1
- 10
0
votes
2 answers
LR(1) BNF grammar for function parameters with trailing elipsis
I would like to write an LR(1) grammar in BNF form for the language described by these two rules from The Complete Syntax of Lua:
parlist ::= namelist [`,´ `...´] | `...´
namelist ::= Name {`,´ Name}
I have tried the following grammars, but…

user200783
- 13,722
- 12
- 69
- 135
0
votes
1 answer
Finding an equivalent LR grammar for the same number of "a" and "b" grammar?
I can't seem to find an equivalent LR grammar for:
S → aSbS | bSaS | ε
which I think recognize strings with the same number of 'a' than 'b'.
What would be a workaround for this? Is it possible to find and LR grammar for this?
Thanks in…

DarK_FirefoX
- 887
- 8
- 20
0
votes
1 answer
Why are the only the states 0 and 2 present in line 8?
LR Parsing:
LR Parsing Table:
In line 7, we reduce by T->T*F.
And State 7 on T does not have any transition.
In line 8, why do we have only the states 0 and 2?

Ujjayanta Bhaumik
- 73
- 2
- 8
0
votes
1 answer
How can I eliminate shift-reduce conflicts by the same operator
I intended to use bison to parse some scripting language, in this language I can write code like the following:
a = input()
b = a + 1
function myfunc
a = input()
b = a + 1
end function
I found that the block
a = input()
b = a + 1
which…

calvin
- 2,125
- 2
- 21
- 38
0
votes
1 answer
Look ahead in LR(1) parsing
I have some problem to understand how to make look ahead in LR(1). I've already found this question about the same problem LR(1) - Items, Look Ahead but this don't help me.
S'->.S,$
S->.L=R,$
S->.R,$
L->.*R,=/$
L->.id,=/$
R->.L,$
I understand the…

JEricaM
- 794
- 2
- 15
- 37