Professional Documents
Culture Documents
16 Ambiguity
16 Ambiguity
16 Ambiguity
Arles Rodríguez
aerodriguezp@unal.edu.co
Facultad de Ciencias
Departamento de Matemáticas
Universidad Nacional de Colombia
Motivation
• Given the following grammar:
𝐸
𝐸 +¿ 𝐸
𝐸 ∗ 𝐸 𝑖𝑑
𝑖𝑑 𝑖𝑑
Motivation
• How many parse trees we can generate?
𝐸 𝐸
𝐸 +¿ 𝐸 𝐸 ∗ 𝐸
𝐸 ∗ 𝐸 𝑖𝑑 𝑖𝑑 𝐸 +¿ 𝐸
𝑖𝑑 𝑖𝑑 𝑖𝑑 𝑖𝑑
Motivation
• The string has two parse trees.
𝐸 𝐸
𝐸 +¿ 𝐸 𝐸 ∗ 𝐸
𝐸 ∗ 𝐸 𝑖𝑑 𝑖𝑑 𝐸 +¿ 𝐸
𝑖𝑑 𝑖𝑑 𝑖𝑑 𝑖𝑑
Motivation
• The string has two parse trees.
𝐸 𝐸
𝐸 +¿ 𝐸 𝐸 ∗ 𝐸
𝐸 ∗ 𝐸 𝑖𝑑 𝑖𝑑 𝐸 +¿ 𝐸
𝑖𝑑 𝑖𝑑 𝑖𝑑 𝑖𝑑
• Here we have two derivations that yields completely
different parse trees.
Ambiguous grammar
• A grammar is ambiguous if it has more than
one parse tree for some string.
– Equivalently, there is more than one right-most or
left most derivation for some string.
• Ambiguity is BAD:
– multiple parse trees for some program left the
compiler to pick which of those to possible
interpretations of the program.
– To generate code like this is not a good idea.
Exercise
Handling ambiguity
Handling Ambiguity
• There are several ways to handle ambiguity:
– Given:
– Most direct:
• Rewrite grammar unambiguously
𝐸
′
• Now E controls the generation of 𝐸 +𝐸
plus and controls the generation of
′ ′
multiplications 𝑖𝑑 ∗ 𝐸 𝐸
𝑖𝑑∗ 𝑖𝑑+𝑖𝑑 𝑖𝑑 𝑖𝑑
• This is known as stratify the grammar
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
Can generate:
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
Can generate: +
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
+
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
Can generate: +
There are summed together.
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
Can generate:
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
Can generate:
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
Can generate:
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
Can generate:
Stratify the grammar
• Consist of dividing the production in two
classes.
– One that handles plus
– One that handles times
• There is one non-terminal for each operator.
Can generate:
• Handles id multiplication
Stratify the grammar
• There is one non-terminal for each operator.
𝐸 𝐸
𝐸 +¿ 𝐸 𝐸 ∗ 𝐸
𝐸 ∗ 𝐸 𝑖𝑑 𝑖𝑑 𝐸 +¿ 𝐸
𝑖𝑑 𝑖𝑑 𝑖𝑑 𝑖𝑑
Ambiguity
• The string had only one parse tree for
grammar:
𝐸 𝐸
𝐸 +¿ 𝐸 𝐸 ∗ 𝐸
𝐸 ∗ 𝐸 𝑖𝑑 𝑖𝑑 𝐸 +¿ 𝐸
𝑖𝑑 𝑖𝑑 𝑖𝑑 𝑖𝑑
Ambiguity
• The string had only one parse tree for
grammar:
Ambiguity
• The string had only one parse tree for
grammar:
𝐸
𝐸 ′
+¿ 𝐸
𝑖𝑑 ∗ 𝐸 𝐸 ′ ′
𝑖𝑑 𝑖𝑑
Another example
• Consider the grammar:
• The expression:
• The expression:
• The expression:
• The expression: 𝐸
𝑖𝑓 𝐸 1 𝑡
𝑒𝑙𝑠𝑒
h𝑒𝑛 𝐸 𝐸
Which one do we want in a programming language?
𝑖𝑓 𝐸 2 𝑡h𝑒𝑛 𝐸 3
Another example: How many parse trees it
has?
Which one do we want in a programming language?
• We want else associated to the closest matching if 𝐸
• Consider the grammar:𝑖𝑓 𝐸 1 𝑡h𝑒𝑛 𝐸
𝑖𝑓 𝐸2 𝑡h𝑒𝑛 𝐸 3 𝑒𝑙𝑠𝑒 𝐸4
𝐸
𝑖𝑓 𝐸 1 𝑡
• We want the expression: 𝑒𝑙𝑠𝑒
h𝑒𝑛 𝐸 𝐸
𝑖𝑓 𝐸 2 𝑡h𝑒𝑛 𝐸 3
Another example: How many parse trees it
has?
Which one do we want in a programming language?
• We want else associated to the closest matching if
• Consider the grammar:
𝐸
𝑖𝑓 𝐸 1 𝑡h𝑒𝑛 𝐸
𝑖𝑓 𝐸2 𝑡h𝑒𝑛 𝐸 3 𝑒𝑙𝑠𝑒 𝐸4
• We want the expression:
Another example: How many parse trees it
has?
Which one do we want in a programming language?
• We want else associated to the closest unmatched then
• We can define
/*all then are matched */
/*some then is unmatched*/
Another example: How many parse trees it
has?
Which one do we want in a programming language?
• We want else associated to the closest unmatched then
• We can define
/*all then are matched */
/*some then is unmatched*/
Another example: How many parse trees it
has?
Which one do we want in a programming language?
• We want else associated to the closest unmatched then
• We can define
/*all then are matched */
/*some then is unmatched*/
Another example: How many parse trees it
has?
Which one do we want in a programming language?
• We want else associated to the closest unmatched then
• We can define
/*all then are matched */
/*some then is unmatched*/
• We can define
/*all then are matched */
Unmatched if
/*some then is unmatched*/
must be in the
else part.
Then branch
must be a
matched if
¿ 𝑖𝑓 𝐸 𝑡h𝑒𝑛𝑀𝐼𝐹 𝑒𝑙𝑠𝑒𝑈𝐼𝐹
Another example: How many parse trees it
has?
Given this grammar:
/*all then are matched */
/*some then is unmatched*/
• Simple solution: to 𝐸
have associativity
declarations.
𝐸 +¿ 𝐸
• We can declare plus to 𝐸 +¿ 𝐸 ∫¿
be left associative. In ∫¿ ∫¿
bison: %left +
Ambiguity: Example 2
• Consider the grammar:
𝐸 𝐸
𝐸 𝐸+¿ 𝐸 ∗ 𝐸
𝐸 ∗ 𝐸 𝑖𝑛𝑡 𝑖𝑛𝑡 𝐸 +¿ 𝐸
𝑖𝑛𝑡 𝑖𝑛𝑡 𝑖𝑛𝑡 𝑖𝑛𝑡
Ambiguity: Example 2
• Consider the grammar: