a. Leftmost derivation for the sentence S containing tokens (1 then # 2 else 3) $ 4:
- Start with the non-terminal
expr.
- Apply the rule
expr → expr '$' expr to introduce $.
expr
→ expr '$' expr
- Apply the rule
expr → '(' expr ')' to handle the left expression of $.
expr '$' expr
→ '(' expr ')' '$' expr
- Inside the parentheses, apply the rule
expr → expr 'then' expr 'else' expr to introduce then and else.
'(' expr ')' '$' expr
→ '(' expr 'then' expr 'else' expr ')' '$' expr
- Resolve the first expression within parentheses to an
INT.
'(' expr 'then' expr 'else' expr ')' '$' expr
→ '(' INT 'then' expr 'else' expr ')' '$' expr
→ '(' 1 'then' expr 'else' expr ')' '$' expr
- Apply the rule
expr → '#' expr for next part of the then expression.
'(' 1 'then' expr 'else' expr ')' '$' expr
→ '(' 1 'then' '#' expr 'else' expr ')' '$' expr
- Resolve the expression after
# to an INT.
'(' 1 'then' '#' expr 'else' expr ')' '$' expr
→ '(' 1 'then' '#' INT 'else' expr ')' '$' expr
→ '(' 1 'then' '#' 2 'else' expr ')' '$' expr
- Resolve the last expression within parentheses, following the
else, to an INT.
'(' 1 'then' '#' 2 'else' expr ')' '$' expr
→ '(' 1 'then' '#' 2 'else' INT ')' '$' expr
→ '(' 1 'then' '#' 2 'else' 3 ')' '$' expr
- Finally, resolve the rightmost
expr to an INT to complete the sentence.
'(' 1 'then' '#' 2 'else' 3 ')' '$' expr
→ '(' 1 'then' '#' 2 'else' 3 ')' '$' INT
→ '(' 1 'then' '#' 2 'else' 3 ')' '$' 4
So, the leftmost derivation for the sentence S is:
expr
→ expr '$' expr
→ '(' expr ')' '$' expr
→ '(' expr 'then' expr 'else' expr ')' '$' expr
→ '(' INT 'then' expr 'else' expr ')' '$' expr
→ '(' 1 'then' expr 'else' expr ')' '$' expr
→ '(' 1 'then' '#' expr 'else' expr ')' '$' expr
→ '(' 1 'then' '#' INT 'else' expr ')' '$' expr
→ '(' 1 'then' '#' 2 'else' expr ')' '$' expr
→ '(' 1 'then' '#' 2 'else' INT ')' '$' expr
→ '(' 1 'then' '#' 2 'else' 3 ')' '$' expr
→ '(' 1 'then' '#' 2 'else' 3 ')' '$' INT
→ '(' 1 'then' '#' 2 'else' 3 ')' '$' 4
b. To show that the grammar is ambiguous, we need to find a string for which there are two different parse trees (or equivalently, two different leftmost or rightmost derivations).
Consider the string 1 $ 2 : 3. There are two possible interpretations (parse trees) for this string:
Interpretation 1:
expr
→ expr '$' expr
→ INT '$' expr
→ 1 '$' expr
→ 1 '$' expr ':' expr
→ 1 '$' INT ':' expr
→ 1 '$' 2 ':' expr
→ 1 '$' 2 ':' INT
→ 1 '$' 2 ':' 3
Here, the expression is grouped as (1 $ 2) : 3.
Interpretation 2:
expr
→ expr ':' expr
→ expr '$' expr ':' expr
→ INT '$' expr ':' expr
→ 1 '$' expr ':' expr
→ 1 '$' expr ':' INT
→ 1 '$' INT ':' INT
→ 1 '$' 2 ':' INT
→ 1 '$' 2 ':' 3
Here, the expression is grouped as 1 $ (2 : 3).
Each interpretation provides a different parse tree, which means the grammar is ambiguous because the string 1 $ 2 : 3 can be parsed in two different ways, leading to two different meanings without additional context or precedence rules. This ambiguity arises because the grammar does not specify operator precedence or associativity, allowing such expressions to group in various ways.
To resolve the ambiguity, one would typically introduce different levels of non-terminals for different precedence levels, or use additional rules that enforce associativity.