I have the following grammar defined:
S -> A|B, A -> aAb | ab, B -> aBb | epsilon;
After working for some time, I still couldn't find a string to construct a distinctive parse tree to show that this grammar is ambiguous. Like: aaabbb,abab,etc. It seemed like this grammar is unambiguous. Any help?
This grammar is ambiguous. The string aabb can be derived in two different ways:
S => A => aAb => aabb
S => B => aBb => aaBbb => aabb
Related
Here is my current grammar:
program -> stmt-sequence
stmt-sequence -> statement { TT_NEWLINE statement }
assign-stmt -> var identifier TT_EQ factor
print-stmt -> print factor
add-stmt -> add factor TT_COMMA factor
sub-stmt -> sub factor TT_COMMA factor
mul-stmt -> mul factor TT_COMMA factor
div-stmt -> div factor TT_COMMA factor
factor -> number | identifier
For my math statements (add-stmt, sub-stmt, mul-stmt, div-stmt), I want those statements to return a number, like as if they were functions.
If you don't understand what I mean by "return", then here is one example:
print add 2, 4
I want for the math statement to be "replaced" with the number, the result of the statement from adding.
print 6
^
Basically, becoming like this.
factor -> number | identifier | add-stmt | sub-stmt | mul-stmt | div-stmt
I do not know if adding alternations of the math statements in the factor is appropriate.
How can I basically be able allow these math statements to "return" in the EBNF grammar?
The math statements should not contain the return terminal, it's not their part to define the return terminal/statement. Instead you define a new symbol like
return-stmt -> TT_RETURN expr
After that you define what expr is. It is something which results in a value:
expr -> factor | add-stmt | sub-stmt | mul-stmt | div-stmt
Be careful in how you define return-stmt and how you use the existing statements/expressions you have. You shouldn't be able to generate text/code like:
return return 4 + return 2 * 3
Find a grammar for the following language:
a*b | a
(a*b | b*a)*
I think I have the answer for 1 (S -> aS | b) but I'm pretty confused on the second one. Any help would be greatly appreciated.
Think of the whole expression (a*b | b*a)* as a nonterminal, and then consider each element (i.e, a*b is one, and b*a is another) inside as separate nonterminals.
Hint:
S -> ε | ST
T -> [rule for a*b] | [rule for b*a]
T is what's inside the bracket.
Language; (ab | ba)*
S -> SA | epsilon
A here represents (ab | ba)
A -> B
A -> C
B represents (a*b)
B -> [Insert rule here]
C represents (b*a)
C -> [Insert rule here]
I have this language:
{an bm | m+n is an even number}
What's the proper grammar for this?
S -> aaS | aB | bbC | ε
B -> bbB | b
C -> bbC | ε
you see, it is a regular language. 'S' stands for "we have constructed an even number of a's and more a's may follow, 'B' stands for "we have constructed an uneven number of a's and now an uneven number of b's follows. 'C' stands for "we have constructed an even number of a's and now an even number of b's follows.
ε stands for "", the empty string
Is there a no-fuss serialization method for Haskell, similar to Erlang's term_to_binary/binary_to_term calls? Data.Binary seems unnecessarily complicated and raw. See this example where you are basically manually encoding terms to integers.
Use Data.Binary, and one of the deriving scripts that come with the package.
It's very simple to derive Binary instances, via the 'derive' or 'deriveM' functions provided in the tools set of Data.Binay.
derive :: (Data a) => a -> String
For any 'a' in Data, it derives a Binary instance for you as a String. There's a putStr version too, deriveM.
Example:
*Main> deriveM (undefined :: Drinks)
instance Binary Main.Drinks where
put (Beer a) = putWord8 0 >> put a
put Coffee = putWord8 1
put Tea = putWord8 2
put EnergyDrink = putWord8 3
put Water = putWord8 4
put Wine = putWord8 5
put Whisky = putWord8 6
get = do
tag_ <- getWord8
case tag_ of
0 -> get >>= \a -> return (Beer a)
1 -> return Coffee
2 -> return Tea
3 -> return EnergyDrink
4 -> return Water
5 -> return Wine
6 -> return Whisky
_ -> fail "no parse"
The example you cite is an example of what the machine generated output looks like -- yes, it is all bits at the lowest level! Don't write it by hand though -- use a tool to derive it for you via reflection.
I'm studying for a finite automata & grammars test and I'm stuck with this question:
Construct a grammar that generates L:
L = {a^n b^m c^m+n|n>=0, m>=0}
I believe my productions should go along this lines:
S->aA | aB
B->bB | bC
C->cC | c Here's where I have doubts
How can my production for C remember the numbers of m and n? I'm guessing this must rather be a context-free grammar, if so, how should it be?
Seems like it should be like:
A->aAc | aBc | ac | epsilon
B->bBc | bc | epsilon
You need to force C'c to be counted during construction process. In order to show it's context-free, I would consider to use Pump Lemma.
S -> X
X -> aXc | Y
Y -> bYc | e
where e == epsilon and X is unnecessary but
added for clarity
Yes, this does sound like homework, but a hint:
Every time you match an 'a', you must match a 'c'. Same for matching a 'b'.
S->aSc|A
A->bAc|λ
This means when ever you get a at least you have 1 c or if you get a and b you must have 2 c.
i hope it has been helpful
Well guys, this is how I'll do it:
P={S::=X|epsilon,
X::=aXc|M|epsilon,
M::=bMc|epsilon}
My answer:
S -> aAc | aSc
A -> bc | bAc
where S is the start symbol.
S-> aBc/epsilon
B-> bBc/S/epsilon
This takes care of the order of the alphabets as well