LR(1) - How to compute Lookaheads - parsing

I have trouble understanding how to compute the lookaheads.
Lets say that I have this extend grammar:
S'-> S
S -> L=R | R
L -> *R | i
R -> L
I wrote the State 0 so:
S'-> .S, {$}
S -> .L=R, {$}
S -> .R, {$}
L -> .*R, {=,$}
L -> .i, {=,$}
R -> .L {=,$}
Using many parsing emulator i see that all calculators says:
R -> .L {$}
Why? Can't the R be followed by a "="?

Related

checking if the following grammar is LR(0) and LR(1)

I have the following grammar:
S -> A
A -> B | B[*]
B -> [AB]
AB -> *,AB | epsilon
S,A,B,AB are variables and [ ] , * are terminals
Is it:
LR(0)?
LR(1)?
and how can I prove it?

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 parsing table.
I'm not sure if my automaton is correct, but after doing the parsing table for SLR(1) grammar I did not find any errors.
Below is my attempt at the automaton.
I0:
S' -> .S
S -> .[B
I1 (I0 -> S):
S -> [.B
B -> .]
B -> .C
C -> .A]
C -> .A,C
A -> .int
A -> .[B
I3 (I2 -> B)
S -> [B.
I4 (I2 -> ])
B -> ].
I5 (I2 -> C)
B -> C.
I6 (I2 -> A)
C -> A.]
C -> A.,C
I7 (I2 -> int)
A -> int.
I8 (I2 -> [)
A -> [.B
B -> .]
B -> .C
C -> .A]
C -> .A,C
A -> .int
A -> .[B
I8 -> ] = I4
I8 -> C = I5
I8 -> A = I6
I8 -> int = I7
I8 -> [ = I8
I9 (I6 -> ])
C -> A].
I10 (I6 -> ,)
C -> A,.C
C -> .A]
C -> .A,C
A -> .int
A -> .[B
I11 (I8 -> B)
A -> [B.
I12 (I10 -> C)
C -> A,C.
I10 -> A = I6
I10 -> int = I7
I10 -> [ = I8
Considering that you have done the hard work, to find all the states, now it is time to check the Rules of SLR(1).
https://en.wikipedia.org/wiki/SLR_grammar
As you can realize, your question misses the Follow() set which is mandatory in the Rules.
Yes, you can find out on your table parser if there is a conflict or not, but this is an answer depended more on experience than actual science.
Check the Rules one-by-one and you will be fine :)
Depending on the fact that you are sure about the correctness of your states, I see no Shift/Reduce or Reduce/Reduce conflict, so the Grammar is SLR(1)

Coq: usage of `PartialOrder` typeclass

I am trying to define lexicographic ordering on strings over posets, but I'm not completely sure how to use the PartialOrder typeclass.
Require Import List RelationClasses.
Fail Inductive lex_leq {A : Type} `{po : PartialOrder A} : list A -> list A -> Prop :=
| lnil: forall l, lex_leq nil l
| lcons:
forall (hd1 hd2 : A) (tl1 tl2 : list A),
hd1 <= hd2 -> (* error *)
(hd1 = hd2 -> lex_leq tl1 tl2) ->
lex_leq (hd1 :: tl1) (hd2 :: tl2).
Partial output:
The term "hd1" has type "A" while it is expected to have type "nat".
Clearly <= is the wrong notation to use here; I'm wondering how I can obtain an ordering relation from my po instance.
One can bind the names explicitly to make things more obvious. Before we can do this we need to tell Coq not to complain about unbound variables using the Generalizable Variables command:
From Coq Require Import List RelationClasses.
Generalizable Variables A eqA R.
Inductive lex_leq `{PartialOrder A eqA R} : list A -> list A -> Prop :=
| lnil: forall l, lex_leq nil l
| lcons:
forall (hd1 hd2 : A) (tl1 tl2 : list A),
R hd1 hd2 ->
(hd1 = hd2 -> lex_leq tl1 tl2) ->
lex_leq (hd1 :: tl1) (hd2 :: tl2).
You can find more information in the manual (here).

proving that a grammar is LL(1)

I'm given the following grammar :
S -> A a A b | B b B a
A -> epsilon
B -> epsilon
I know that it's obvious that it's LL(1), but I'm facing troubles constructing the parsing table.. I followed the algorithm word by word to find the first and follow of each non-terminal , correct me if I'm wrong:
First(S) = {a,b}
First(A) = First(B) = epsilon
Follow(S) = {$}
Follow(A) = {a,b}
Follow(B) = {a,b}
when I construct the parsing table, according to the algorithm, I get a conflict under the $ symbol... what the hell am I doing wrong??
a b $
A A-> epsilon
B B-> epsilon
S S -> AaAb
S -> BbBa
is it ok if I get 2 productions under $ or something?? or am I constructing the parsing table wrong? please help I'm new to the compiler course
There is a tiny mistake. Algorithm is as follows from dragon book,
for each rule (S -> A):
for each terminal a in First(A):
add (S -> A) to M[S, a]
if First(A) contains empty:
for each terminal b in Follow(S):
add (S -> A) to M[S, b]
Let's take them one by one.
S -> AaAb. Here, First(AaAb) = {a}. So add S -> AaAb to M[S, a].
S -> BbBa. Here, First(BbBa) = {b}. So add S -> BbBa to M[S, b].
A -> epsilon. Here, Follow(A) = {a, b}. So add A -> epsilon to M[A, a] and M[A, b].
B -> epsilon. Here, Follow(B) = {a, b}. So add B -> epsilon to M[B, a] and M[B, b].

Extending Grammar for LR Parser

I have the following grammar for basic arithmetic expressions
E -> E + T
E -> T
T -> T * F
T -> F
F -> (E)
F -> id
Where E is expression, T is term, F is factor. I'm wondering how I can extend this grammar to support further arithmetic operations such exponents possibly represented with ^ or logarithm.
Thanks
Since exponentation has higher precedence you could use the following grammar:
E -> E + T
E -> T
T -> T * F
T -> F
F -> G ^ F
F -> G
G -> log(E)
G -> (E)
G -> id

Resources