You are on page 1of 11

University of Zakho

Faculty of Science
Computer Department
Second Stage

Ambiguity in compatibility theory

Group:-B

Report by: Lecturer: Ms. Tibba


Rasheed shamsadin
Haji tahsin
Saadia Mikael
Gazi salah
Sidra abdullstar

1|Page
 Contents
 Introduction....................................................................................................3
 Context-Free Grammar (CFG).................................................................3
 Derivation....................................................................................................3
 Derivation Tree...........................................................................................3
 What is Ambiguous and Unambiguous Grammar..................................3
 Difference Between Ambiguous and Unambiguous Grammar.................4
 Types of Ambiguous Grammar....................................................................5
 Trivial language..........................................................................................5
 Unary string.................................................................................................5
 Addition and subtraction...........................................................................5
 Dangling else................................................................................................6
 An unambiguous grammar with multiple derivations................................7
 Some Examples...............................................................................................8
 Example 1:...................................................................................................8
 Example 2:...................................................................................................8
 Example 3:....................................................................................................9
 Recognizing ambiguous grammars...............................................................10
 Inherently ambiguous Language...................................................................10
 Conclusion....................................................................................................10
 References........................................................................................................11

2|Page
 Introduction
Before we go into Ambiguous Grammar we first need to know about these
topics (Context-Free Grammar (CFG), Derivation, Derivation Tree).
 Context-Free Grammar (CFG)
CFG stands for context-free grammar. It is a formal grammar which is used
to generate all possible patterns of strings in a given formal language [1].
 Derivation
Derivation is a sequence of production rules. It is used to get the input
string through these production rules. During parsing, we have to take two
decisions. These are as follows:
1) We have to decide the non-terminal which is to be replaced.
2) We have to decide the production rule by which the non-terminal
will be replaced.
We have two options to decide which non-terminal to be placed with
production rule [1].
 Derivation Tree
Derivation tree is a graphical representation for the derivation of the given
production rules for a given CFG. It is the simple way to show how the
derivation can be done to obtain some string from a given set of production
rules. The derivation tree is also called a parse tree.
Parse tree follows the precedence of operators. The deepest sub-tree
traversed first. So, the operator in the parent node has less precedence
over the operator in the sub-tree [1].
 What is Ambiguous and Unambiguous Grammar
In computer science, an ambiguous grammar is a context-free grammar for
which there exists a string that can have more than one leftmost derivation
or parse tree, while an unambiguous grammar is a context-free grammar
for which every valid string has a unique leftmost derivation or parse tree.
Many languages admit both ambiguous and unambiguous grammars, while
some languages admit only ambiguous grammars[2]. Any non-empty
language admits an ambiguous grammar by taking an unambiguous
grammar and introducing a duplicate rule or synonym (the only language
without ambiguous grammars is the empty language). A language that only

3|Page
admits ambiguous grammars is called an inherently ambiguous language,
and there are inherently ambiguous context-free languages. Deterministic
context-free grammars are always unambiguous, and are an important
subclass of unambiguous grammars; there are non-deterministic
unambiguous grammars [2].

 Difference Between Ambiguous and Unambiguous Grammar

Ambiguous Grammar Unambiguous Grammar


A grammar is said to be ambiguous if for at least A grammar is said to be unambiguous if for all the
one string generated by it, it produces more than strings generated by it, it produces exactly one-
one-  parse tree
 parse tree  or derivation tree
 or derivation tree  or syntax tree
 or syntax tree  or leftmost derivation
 or leftmost derivation  or rightmost derivation
 or rightmost derivation
For ambiguous grammar, leftmost derivation and For unambiguous grammar, leftmost derivation
rightmost derivation represents different parse and rightmost derivation represents the same
trees. parse tree.
Ambiguous grammar contains a smaller number Unambiguous grammar contains a greater
of non-terminals. number of non-terminals.
For ambiguous grammar, length of parse tree is For unambiguous grammar, length of parse tree
less. is large.
Ambiguous grammar is faster than unambiguous Unambiguous grammar is slower than ambiguous
grammar in the derivation of a tree. grammar in the derivation of a tree.
(Reason is above 2 points)

Example- Example-
E→E+T/T

E → E + E / E x E / id T→TxF/F

(Ambiguous Grammar) F → id

(Unambiguous Grammar)
[3].

4|Page
 Types of Ambiguous Grammar

 Trivial language
The simplest example is the following ambiguous grammar for the trivial
language, which consists of only the empty string:
A→A|ε
…meaning that a production can either be itself again, or the empty string.
Thus, the empty string has leftmost derivations of length 1, 2, 3, and indeed
of any length, depending on how many times the rule A → A is used [2].
This language also has the unambiguous grammar, consisting of a single
production rule:
A→ε
…meaning that the unique production can only produce the empty string,
which is the unique string in the language.
In the same way, any grammar for a non-empty language can be made
ambiguous by adding duplicates [2].
 Unary string
The regular language of unary strings of a given character, say 'a' (the
regular expression a*), has the unambiguous grammar [2]:
A → aA | ε
…but also has the ambiguous grammar:
A → aA | Aa | ε
These correspond to producing a right-associative tree (for the
unambiguous grammar) or allowing both left- and right- association [2].
 Addition and subtraction
The context free grammar
A→A+A|A−A|a
is ambiguous since there are two leftmost derivations for the string a + a +
a:
A→ A + A A→ A + A
→a+A → A + A + A (First A is replaced by A+A.)

5|Page
Replacement of the second A would yield a similar derivation
→a+A+A →a+A+A
→a+a+A →a+a+A
→a+a+a →a+a+a
As another example, the grammar is ambiguous since there are two parse
trees for the string a + a − a:

The language that it generates, however, is not inherently ambiguous; the


following is a non-ambiguous grammar generating the same language [2]:
A→A+a|A−a|a
 Dangling else
A common example of ambiguity in computer programming languages is the
dangling else problem. In many languages, the else in an If–then(–else)
statement is optional, which results in nested conditionals having multiple
ways of being recognized in terms of the context-free grammar [1].
Concretely, in many languages one may write conditionals in two valid forms:
the if-then form, and the if-then-else form – in effect, making the else clause
optional:
In a grammar containing the rules
Statement → if Condition then Statement |
if Condition then Statement else Statement | ...
Condition → ...
some ambiguous phrase structures can appear. The expression
if a then if b then s else s2

6|Page
can be parsed as either
if a then begin if b then s end else s2
or as
if a then begin if b then s else s2 end
depending on whether the else is associated with the first if or second if [1].
This is resolved in various ways in different languages. Sometimes the
grammar is modified so that it is unambiguous, such as by requiring an
endif statement or making else mandatory. In other cases, the grammar is
left ambiguous, but the ambiguity is resolved by making the overall phrase
grammar context-sensitive, such as by associating an else with the nearest
if. In this latter case the grammar is unambiguous, but the context-free
grammar is ambiguous [1].
 An unambiguous grammar with multiple derivations
The existence of multiple derivations of the same string does not suffice to
indicate that the grammar is ambiguous; only multiple leftmost derivations (or,
equivalently, multiple parse trees) indicate ambiguity.
For example, the simple grammar
S→A+A
A→0|1
is an unambiguous grammar for the language { 0+0, 0+1, 1+0, 1+1 }. While
each of these four strings has only one leftmost derivation, it has two
different derivations, for example
S⇒A+A⇒0+A⇒0+0
and
S⇒A+A⇒A+0⇒0+0
Only the former derivation is a leftmost one.

7|Page
 Some Examples

 Example 1:
Let us consider a grammar G with the production rule
E→I
E→E+E
E→E*E
E → (E)
I → ε | 0 | 1 | 2 | ... | 9
Solution:
For the string "3 * 2 + 5", the above grammar can generate two parse trees
by leftmost derivation:

Since there are two parse trees for a single string "3 * 2 + 5", the grammar
G is ambiguous [1].
 Example 2:
Check whether the given grammar G is ambiguous or not.
E→E+E
E→E-E
E → id

8|Page
Solution:
From the above grammar String "id + id - id" can be derived in 2 ways:
First Leftmost derivation Second Leftmost derivation
E→E+E E→E-E
→ id + E →E+E-E
→ id + E - E → id + E - E
→ id + id - E → id + id - E
→ id + id- id → id + id - id
Since there are two leftmost derivation for a single string "id + id - id", the
grammar G is ambiguous [1].
 Example 3:
Check whether the given grammar G is ambiguous or not.
S → aSb | SS
S→ε
Solution:

For the string "aabb" the above grammar can generate two parse trees

Since there are two parse trees for a single string "aabb", the grammar G is
ambiguous [1].

9|Page
 Recognizing ambiguous grammars
The decision problem of whether an arbitrary grammar is ambiguous is
undecidable because it can be shown that it is equivalent to the Post
correspondence problem.[3] At least, there are tools implementing some
semi-decision procedure for detecting ambiguity of context-free grammars
[2].
The efficiency of context-free grammar parsing is determined by the
automaton that accepts it. Deterministic context-free grammars are
accepted by deterministic pushdown automata and can be parsed in linear
time, for example by the LR parser.[5] This is a subset of the context-free
grammars which are accepted by the pushdown automaton and can be
parsed in polynomial time, for example by the CYK algorithm. Unambiguous
context-free grammars can be nondeterministic [2].
 Inherently ambiguous Language
Let L be a Context Free Language (CFL). If every Context Free Grammar G
with Language L = L(G) is ambiguous, then L is said to be inherently
ambiguous Language. Ambiguity is a property of grammar not languages.
Ambiguous grammar is unlikely to be useful for a programming language,
because two parse trees structures (or more) for the same string(program)
implies two different meanings (executable programs) for the program.
An inherently ambiguous language would be absolutely unsuitable as a
programming language, because we would not have any way of fixing a
unique structure for all its programs [4].
For example,
L = {anbncm} ∪ {anbmcm}.
 Conclusion
Ambiguity in a grammar is detrimental to the syntactic structure of a language. Not only
does ambiguity pose a serious problem for compilers when trying to determine what code
to generate, but also the readability of code for programmers. It is essential that you
examine your code and try to eliminate all instances of ambiguity, making your code more
efficient and readable.

10 | P a g e
 References

[1]: “Automata Ambiguity in Grammar - Javatpoint,” www.javatpoint.com.


[Online]. Available: https://www.javatpoint.com/automata-ambiguity-in-
grammar. [Accessed: 15-Nov-2019].
[2]: “Ambiguous grammar,” Wikipedia, 09-Nov-2019. [Online]. Available:
https://en.wikipedia.org/wiki/Ambiguous_grammar. [Accessed: 15-Nov-
2019].
[3]: A. Singhal, “Akshay Singhal,” Gate Vidyalay, 26-Apr-2019. [Online].
Available: https://www.gatevidyalay.com/difference-between-ambiguous-
and-unambiguous-grammar/. [Accessed: 15-Nov-2019].
[4]: “Compiler Design: Ambiguous Grammar,” GeeksforGeeks, 05-Mar-
2019. [Online]. Available: https://www.geeksforgeeks.org/ambiguous-
grammar/. [Accessed: 15-Nov-2019].

11 | P a g e

You might also like