a. Leftmost Derivation for the sentence S: (1 then # 2 else 3) $ 4
To perform a leftmost derivation, we start with the start symbol and in each step, we replace the leftmost non-terminal with one of its productions until we reach the terminal symbols that make up the sentence S.
- Start with the start symbol
expr
- Since we have a parenthesis at the beginning of S, we should use the production
expr -> '(' expr ')'.
expr -> '(' expr ')'
- Inside the parentheses, we have another expression that follows the structure of a conditional expression
expr 'then' expr 'else' expr. We apply this production.
expr -> '(' expr 'then' expr 'else' expr ')'
- Now we have to derive the leftmost
expr which is a plain INT (the number 1 in our sentence).
expr -> '(' INT 'then' expr 'else' expr ')'
- Next, we have the
expr that follows the '#' unary operator. We use the production expr -> '#' expr.
expr -> '(' INT 'then' '#' expr 'else' expr ')'
- The
expr after the '#' is another INT (the number 2 in our sentence).
expr -> '(' INT 'then' '#' INT 'else' expr ')'
- Now, the only
expr left inside the parentheses is the INT 3.
expr -> '(' INT 'then' '#' INT 'else' INT ')'
- We replace the INTs with their corresponding tokens:
expr -> '(' 1 'then' '#' 2 'else' 3 ')'
- Finally, we have to append the
'$' expr part of the sentence S. Since we have a binary operator structure, we use the production expr -> expr '$' expr.
expr -> expr '$' expr
- The left side of the '' operator is now fully derived as `'( 1 then # 2 else 3 )'`. So only the remaining right side of the '' operator is an INT (the number 4 in our sentence):
expr -> '(' 1 'then' '#' 2 'else' 3 ')' '$' INT
- Replacing the INT with the token 4 gives the final derived sentence S:
expr -> '(' 1 'then' '#' 2 'else' 3 ')' '$' 4
Putting the steps together, we have the leftmost derivation sequence for the given sentence S:
expr
-> '(' expr ')'
-> '(' expr 'then' expr 'else' expr ')'
-> '(' INT 'then' expr 'else' expr ')'
-> '(' 1 'then' expr 'else' expr ')'
-> '(' 1 'then' '#' expr 'else' expr ')'
-> '(' 1 'then' '#' INT 'else' expr ')'
-> '(' 1 'then' '#' 2 'else' expr ')'
-> '(' 1 'then' '#' 2 'else' INT ')'
-> '(' 1 'then' '#' 2 'else' 3 ')'
-> expr '$' expr
-> '(' 1 'then' '#' 2 'else' 3 ')' '$' expr
-> '(' 1 'then' '#' 2 'else' 3 ')' '$' INT
-> '(' 1 'then' '#' 2 'else' 3 ')' '$' 4
b. Showing Ambiguity in the Grammar
To demonstrate that a grammar is ambiguous, we need to show that there is at least one string in the language that the grammar generates that has two different parse trees (or equivalently, two different leftmost or rightmost derivations that result in the same string).
Consider the string 1 $ 2 $ 3. This string can be derived in at least two different ways:
First derivation (left associativity of $):
- expr -> expr '$' expr
- expr -> expr '′expr′' expr
- expr -> INT '′ex' expr
Second derivation (right associativity of $):
- expr -> expr '$' expr
- expr -> expr '′(expr′' expr)
- expr -> expr '′(' expr)
Both derivations yield the same string 1 $ 2 $ 3 but associate the operations differently, meaning the grammar allows for two different interpretations (parse trees) of the same string. This demonstrates the grammar is ambiguous since it does not unambiguously dictate the structure of the expression. Specifically, the binary operator $ does not have a defined associativity within the grammar, leading to ambiguity.