Suppose I have the following CFG:
S->aACD|BcAe
A->b | EPSILON
B->cf | d
C->fe
Now I apply the FIRST rule on the CFG:
FIRST(S)=FIRST(aAcd) U FIRST(BcAe)
={a} U FIRST(BcAe)
= {a} U FIRST(B)-{EPSILON} U FIRST(cAe)
= {a} U FIRST(B)-{EPSILON} U {c}
= {a} U FIRST(Cf) U FIRST(d) -{EPSILON} U {c}
= {a,f,d,c,EPSILON}
FIRST(A)=FIRST(b) U FIRST(EPSILON)= ={B,EPSILON}
FIRST(B)=FIRST(Cf) U FIRST(d)={d,f}
FIRST(C)=FIRST(fe)={F}
Now I apply the FOLLOW rule on the CFG:
FOLLOW(S)={$}
FOLLOW(A)={c,e}
FOLLOW(B)={c}
FOLLOW(C)={f}
Is there any wrong? If it is wrong please show me how to do it.
Your work above (question) suggest that you are not good in basic concepts. So you can make use of the this tutorial.
Grammer :
S->aACD|BcAe
A->b | EPSILON
B->cf | d
C->fe
No production means EPSILON so,
D-> EPSILON
On applying first rule :
FIRST(A)=FIRST(b) U FIRST(EPSILON)= ={b,EPSILON}
FIRST(B)=FIRST(cf) U FIRST(d)={c} U {d} = {c,d}
FIRST(C)=FIRST(fe)={f}
FIRST(D)={EPSILON}
FIRST(S)=FIRST(aACD) U FIRST(BcAe)
={a} U FIRST(BcAe)
= {a} U FIRST(B)
= {a} U {c,d}
= {a,c,d}
on applying follow rule :
FOLLOW(S)={$}
FOLLOW(A)= FIRST(C) U FIRST(e) = {f,e}
FOLLOW(B)={c}
FOLLOW(C)={$}
FOLLOW(D)={$}
Related
Assume there are four types , D, Q, P, C
data Q : Set where
q1 : Q
q2 : Q
data P : Set where
p1 : P
p2 : P
data C : Set where
c1 : C
c2 : C
data D : Set where
d1 : D
d2 : D
When trying to define function
f : Set -> Set
f D = Q
f P = C
I get the warning unreachable clause .
I assume it is because domains of Agda function are defined on sets but not category of sets.
What if I want a mapping relation which behaves like a close type family in Haskell ?
Because the Agda compiler erases types during compilation, it is not allowed to pattern match on Set directly. (The error 'unreachable clause' is a bit confusing but it results from Agda interpreting D as a pattern variable rather than the datatype.)
The usual way to work around this problem is to define your own universe, i.e. a datatype whose elements are interpreted as specific sets. Here is an example:
data Q : Set where
q1 : Q
q2 : Q
data P : Set where
p1 : P
p2 : P
data C : Set where
c1 : C
c2 : C
data D : Set where
d1 : D
d2 : D
-- Our little universe
data U : Set where
d : U
p : U
-- The interpretation function
⟦_⟧ : U → Set
⟦ d ⟧ = D
⟦ p ⟧ = P
f : U → Set
f d = Q
f p = C
-- An example of how to use f:
omo : {u : U} → ⟦ u ⟧ → f u
omo {d} d1 = q1
omo {d} d2 = q2
omo {p} p1 = c1
omo {p} p2 = c2
I have a grammar that looks like this
G = ({A, B, C},{a, c},P, A),
P = {
1. A → BaC,
2. B → aB
3. B → ε,
4. C→c
}
and I want to resolve the FIRST(2.) = {a} FOLLOW(B) = {a} conflict. But with every attempt I fail because I am not able to use the terminal symbol absorption after which the grammar looks like this
G = ({A, B, C, [Ba]},{a, c},P, A),
P = {
1. A → [Ba]C,
2. B → aB
3. B → ε,
4. C → c
5. [Ba] → aBa | a
}
but the problem remains still the same
I'm getting a strange error of the form x != y of type Y when checking that the pattern p(y) has type Z. I do not know why or how I'm getting this and would like to solve the issue. A problem instance follows; thank-you.
Suppose I've a set,
postulate A : Set
and a way to interpret its elements as sets,
postulate F : A → Set
Then using pairs of that set,
record B : Set where field s t : A
I can build a parametrised type on it:
data C : A → Set where MkC : (b : B) → F (B.s b) → C (B.t b)
Now I'd like to, for example, form a function
ABCF : ∀ a → (f : A → A) → C a → C (f a)
ABCF t f e = {!!}
and I'd do so by pattern matching on the third argument via C-c C-c
and doing so gets me
ABCF .(B.t b) f (MkC b x) = {!!}
then another C-c C-c, on b, yields
ABCF t f (MkC record { s = s ; t = .t } x) = ?
but this casing is immediately followed by an error:
B.t b != t of type A
when checking that the pattern MkC record { s = s ; t = .t } x has
type C t
Replacing .t with t' also does not solve this.
Any help indicating the reason behind this error and how to fix it would be greatly appreciated!
Edit
As answered below, the above problem may be due to a bug, but what of the converse case?
FCBA : ∀ {a} (f : A → A) → C (f a) → C a
FCBA {a} f (MkC record { s = s ; t = .(f a) } x) = ?
How would we solve this one? Which comes with the error
B.t b != f a of type A
when checking that the pattern MkC record { s = s ; t = .(f a) } x
has type C (f a)
Looks like a bug. If you swap the inaccessible pattern with the regular one, everything works:
ABCF : ∀ a → (f : A → A) → C a → C (f a)
ABCF .t f (MkC record { s = s ; t = t } x) = {!!}
But that a should anyway be implicit, since it's always inferrable from C a. Then there is no problem:
ABCF : ∀ {a} → (f : A → A) → C a → C (f a)
ABCF f (MkC record { s = s ; t = t } x) = {!!}
If you cannot pattern match directly due to the type being too specific, you can generalize it:
open import Relation.Binary.PropositionalEquality
FCBA : ∀ {a} (f : A → A) → C (f a) → C a
FCBA {a} f c with f a | inspect f a | c
... | .b | [ r ] | MkC record { s = s ; t = b } x = {!!}
Here we generalize f a to b and remember that f a ≡ b (in this case such remembering is not useful probably, but it's needed if you don't want to forget that b is actually f a). This allows to pattern match on c with swapping the inaccessible and the regular patterns like before.
But this is not a trick — it's an ugly hack. You should probably ask on the Agda mailing list why this swapping is required and whether this is the intended behavior.
In my current compilers course, I've understood how to find the first and follow sets of a grammar, and so far all of the grammars I have dealt with have contained epsilon. Now I am being asked to find the first and follow sets of a grammar without epsilon, and to determine whether it is LR(0) and SLR. Not having epsilon has thrown me off, so I don't know if I've done it correctly. I would appreciate any comments on whether I am on the right track with the first and follow sets, and how to begin determining if it is LR(0)
Consider the following grammar describing Lisp arithmetic:
S -> E // S is start symbol, E is expression
E -> (FL) // F is math function, L is a list
L -> LI | I // I is an item in a list
I -> n | E // an item is a number n or an expression E
F -> + | - | *
FIRST:
FIRST(S)= FIRST(E) = {(}
FIRST(L)= FIRST(I) = {n,(}
FIRST(F) = {+, -, *}
FOLLOW:
FOLLOW(S) = {$}
FOLLOW(E) = FOLLOW(L) = {), n, $}
FOLLOW(I) = {),$}
FOLLOW(F) = {),$}
The FIRST sets are right, but the FOLLOW sets are incorrect.
The FOLLOW(S) = {$} is right, though technically this is for the augmented grammar S' -> S$ .
E appears on the right side of S -> E and I -> E, both of which mean that the follow of that set is in the follow of E, so: FOLLOW(E) = FOLLOW(S) ∪ FOLLOW(I) .
L appears on the right hand side of L -> LI, which gives FOLLOW(L) ⊇ FIRST(I) , and E -> (FL), which gives FOLLOW(L) ⊇ {)} .
I appears on the right side of L -> LI | I , which gives FOLLOW(I) = FOLLOW(L) .
F appears on the right side in E -> (FL) , which gives FOLLOW(F) = FIRST(L)
Solving for these gives:
FOLLOW(F) = {n, (}
FOLLOW(L) = FIRST(I) ∪ {)} = {n, (, )}
FOLLOW(I) = {n, (, )}
FOLLOW(E) = {$} ∪ {n, (, )} = {n, (, ), $}
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].