Grammar Ambiguity-
Before you go through this article, make sure that you have gone through the previous article on Grammar Ambiguity.
We have discussed-
- There exists no general algorithm to remove the ambiguity from grammar.
- To check grammar ambiguity, we try finding a string that has more than one parse tree.
- If any such string exists, then the grammar is ambiguous otherwise not.
In this article, we will discuss how to convert ambiguous grammar into unambiguous grammar.
Converting Ambiguous Grammar Into Unambiguous Grammar-
- Causes such as left recursion, common prefixes etc makes the grammar ambiguous.
- The removal of these causes may convert the grammar into unambiguous grammar.
- However, it is not always compulsory.
NOTEIt is not always possible to convert an ambiguous grammar into an unambiguous grammar. |
Methods To Remove Ambiguity-
The ambiguity from the grammar may be removed using the following methods-
- By fixing the grammar
- By adding grouping rules
- By using semantics and choosing the parse that makes the most sense
- By adding the precedence rules or other context sensitive parsing rules
Removing Ambiguity By Precedence & Associativity Rules-
An ambiguous grammar may be converted into an unambiguous grammar by implementing-
- Precedence Constraints
- Associativity Constraints
These constraints are implemented using the following rules-
Rule-01:
The precedence constraint is implemented using the following rules-
- The level at which the production is present defines the priority of the operator contained in it.
- The higher the level of the production, the lower the priority of operator.
- The lower the level of the production, the higher the priority of operator.
Rule-02:
The associativity constraint is implemented using the following rules-
- If the operator is left associative, induce left recursion in its production.
- If the operator is right associative, induce right recursion in its production.
PROBLEMS BASED ON CONVERSION INTO UNAMBIGUOUS GRAMMAR-
Problem-01:
Convert the following ambiguous grammar into unambiguous grammar-
R → R + R / R . R / R* / a / b
where * is kleen closure and . is concatenation.
Solution-
To convert the given grammar into its corresponding unambiguous grammar, we implement the precedence and associativity constraints.
We have-
- Given grammar consists of the following operators-
+ , . , *
- Given grammar consists of the following operands-
a , b
The priority order is-
(a , b) > * > . > +
where-
- . operator is left associative
- + operator is left associative
Using the precedence and associativity rules, we write the corresponding unambiguous grammar as-
E → E + T / T
T → T . F / F
F → F* / G
G → a / b
Unambiguous Grammar
OR
E → E + T / T
T → T . F / F
F → F* / a / b
Unambiguous Grammar
Problem-02:
Convert the following ambiguous grammar into unambiguous grammar-
bexp → bexp or bexp / bexp and bexp / not bexp / T / F
where bexp represents Boolean expression, T represents True and F represents False.
Solution-
To convert the given grammar into its corresponding unambiguous grammar, we implement the precedence and associativity constraints.
We have-
- Given grammar consists of the following operators-
or , and , not
- Given grammar consists of the following operands-
T , F
The priority order is-
(T , F) > not > and > or
where-
- and operator is left associative
- or operator is left associative
Using the precedence and associativity rules, we write the corresponding unambiguous grammar as-
bexp → bexp or M / M
M → M and N / N
N → not N / G
G → T / F
Unambiguous Grammar
OR
bexp → bexp or M / M
M → M and N / N
N → not N / T / F
Unambiguous Grammar
To gain better understanding about Conversion into Unambiguous Grammar-
Next Article-Evaluating Expressions Based On Given Grammar
Get more notes and other study material of Theory of Automata and Computation.
Watch video lectures by visiting our YouTube channel LearnVidFun.