I need to find a context free grammar for the following language:
L= { w from { a,b,c,d }* : #a+2#b=2#c+#d }
Here is my attempt, but I doubt it is correct:
S -> aSd|dSa|BSC|CSB|abSdc|baScd|dcSab|cdSba|SS|λ
B -> c|dd
C -> b|aa
I need to find a context free grammar for the following language:
L= { w from { a,b,c,d }* : #a+2#b=2#c+#d }
Here is my attempt, but I doubt it is correct:
S -> aSd|dSa|BSC|CSB|abSdc|baScd|dcSab|cdSba|SS|λ
B -> c|dd
C -> b|aa
You can construct a PDA that recognizes the language like this:
a
and b
in the input correspond to a
on the stack.c
and d
correspond to c
on the stack.a
, and a
is on the input, consume it and push a
to the stack.a
, and b
is on the input, consume it and push aa
to the stack.c
, and c
is on the input, consume it and push cc
to the stack.c
, and d
is on the input, consume it and push c
to the stack.c
and a
is on the input, consume a
and pop the c
.c
and b
is on the input, consume b
and pop the c
, moving to a special state that either pop another c
or pushes an a
if the stack is empty.That means you must be able to construct a CFG. I think you're on the right track, but this CFG is going to be a pain to write since there are no ordering constraints. This means you'll have a lot of permutations of the same basic rule.