Tutorial 2
Question 1
Part AFor each of the following grammars, describe the language (set of strings) that the grammar generates
-
The grammar produces any non-empty string of “0” or “1” with colons separating the digits
-
The grammar produces any string of “0” or “1”, including the empty string
-
The grammar produces any non-empty string of “0” or “1”
-
The grammar produces any string of “0” or “1” including the empty string.
Part BFor each of the grammars in Q1a state whether or not the grammar is ambiguous. If the grammar is ambiguous, give a string for which there is more than one parse tree, and show the two different parse trees.
-
The grammar is unambiguous
-
The grammar is unambiguous
-
Ambiguous - Consider the string parsing of the string 000
-
Ambiguous - Consider the parsing of the string 0
Question 2
Part AGiven the grammar, describe the language it generates
This language generates any string of arbitrary set of matched parentheses including the empty string
Part BGiven the grammar, show that the grammar is ambiguous, e.g. consider possible parse trees for the string “()” or even the empty string “”
- Consider the empty string
. There is ambiguity in the way that we parse it
-
The image to the right describes the two possible symbols for the empty string
. -
It is also possible to generate a parse tree using the second option in our grammar definition
-
We could also show that the parse tree of a single set of matched parentheses is ambiguous
Question 3
Given the grammar:
write down the leftmost derivations (i.e. expand the leftmost non-terminal in each step), parse trees and abstract syntax trees for the following expressions. You may abbreviate a number token by . (a).
The leftmost derivation is shown below, where the non-terminal symbol being expanded is over-lined.
(b)
(c)
(d)
Question 4
The following grammar for if-then-else statements is proposed to remedy the dangling-else ambiguity:
Show that it is still ambiguous
-
Consider the string
-
This string can be parsed as either of the following, in which the braces are not part of the string, but to show grouping:
if false then {
if false then
other
else {
if false then
other
else
other
}
}
if false then {
if false then
other
else {
if false then {
other
}
} else
other
}
Question 5
Unary minus can be added in several ways to the simple arithmetic expression grammar:
Revise the grammar for each of the cases that follow, so that it satisfies the stated rule.
-
At most one unary minus is allowed in each expression, and it must come at the beginning of an expression
✅
✅
❌First Solution
-
We can make a distinction at the top level between an expression that starts with a unary minus (NE) and one that doesn’t (E) - those have similar syntax:
Second Solution
The tutor had this solution, but the expression with the unary operator on a separate line - this is the same as this solution.
-
An alternative simpler syntax also derives the correct sentence:
-
Note that while both of these grammars derive the same language (set of strings), they produce different parse trees.
-
At most one unary minus is allowed before a number or left parenthesis
✅
❌
❌Sample Solution (Same as Tutor’s Solution)
-
Arbitrarily many unary minuses are allowed before numbers and left parentheses, so everything above is legal.
Sample Solution
Tutor’s Solution