a. Leftmost Derivation for Sentence S: (1 then # 2 else 3) $ 4
-
Start with the non-terminal 'expr'.
-
Apply the rule expr → expr '$' expr because we have a '′operatorinthesentence.‘expr=>expr′' expr`
-
Apply the rule expr → '(' expr ')' to the leftmost 'expr' to handle the parentheses.
expr '$' expr => '(' expr ')' '$' expr
-
Apply the rule expr → expr 'then' expr 'else' expr to the 'expr' inside the parentheses.
'(' expr ')' '$' expr => '(' expr 'then' expr 'else' expr ')' '$' expr
-
Replace the first 'expr' after 'then' with '#' expr since we see a use of the unary operator '#' before '2'.
'(' expr 'then' expr 'else' expr ')' '$' expr => '(' expr 'then' '#' expr 'else' expr ')' '$' expr
-
Apply the rule expr → INT to all 'expr' that represent integer values to obtain the numeral '1', '2', and '3'.
'(' expr 'then' '#' expr 'else' expr ')' '$' expr => '(' INT 'then' '#' expr 'else' INT ')' '$' expr
'(' INT 'then' '#' INT 'else' INT ')' '$' expr => '(' INT 'then' '#' INT 'else' INT ')' '$' INT
-
Apply the rule expr → INT to the 'expr' representing the number '4'.
'(' INT 'then' '#' INT 'else' INT ')' '$' INT => '(' INT 'then' '#' INT 'else' INT ')' '$' INT
-
Plug in the actual INT values into our derivation.
'(' INT 'then' '#' INT 'else' INT ')' '$' INT => '(1 then # 2 else 3) $ 4
And this completes the leftmost derivation.
b. Proof of Ambiguity for the Given Grammar
A grammar is considered ambiguous if there exists a string that can be generated by the grammar in more than one way, meaning it has more than one leftmost derivation (or more than one parse tree). The given grammar is ambiguous because there are expressions which can be derived in multiple ways due to the lack of precedence or association rules among the operators 'then', 'else', '$', ':', and '#'.
To demonstrate the ambiguity, consider the expression "1 $ 2 : 3". This expression can be derived in two different ways:
First derivation:
expr
- Apply the rule
expr → expr '$' expr
expr => expr '$' expr
- Apply the rule
expr → expr ':' expr to the second 'expr' (right association)
expr '$' expr => expr '$' (expr ':' expr)
- Convert all 'expr' to INT
expr '$' (expr ':' expr) => INT '$' (INT ':' INT)
- Substitute INT values
INT '$' (INT ':' INT) => 1 $ 2 : 3
Second derivation:
expr
- Apply the rule
expr → expr ':' expr
expr => expr ':' expr
- Apply the rule
expr → expr '$' expr to the first 'expr' (left association)
expr ':' expr => (expr '$' expr) ':' expr
- Convert all 'expr' to INT
(expr '$' expr) ':' expr => (INT '$' INT) ':' INT
- Substitute INT values
(INT '$' INT) ':' INT => 1 $ 2 : 3
These two derivations lead to different parse trees, each implying a different precedence between the '′and′:' as having higher precedence than ':', while right association does the opposite. Since the grammar does not specify operator precedence or associativity, it is ambiguous.
Similarly, expressions involving the 'then' and 'else' tokens without clear precedence would also demonstrate the ambiguity, as well as expressions mixing the unary '#' with other operators, since the grammar does not specify how these operations associate in relation to one another.