





























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
cfg, ambiguity, derivation and parse trees etc
Typology: Slides
1 / 37
This page cannot be seen from the preview
Don't miss anything!
If S โ x1x2 โฆโฆ xn is a production rule in a CFG, then the parse tree / derivation tree will be as follows โ
There are two different approaches to draw a derivation tree โ Top-down Approach โ
Leftmost and Rightmost Derivation of a String Leftmost derivation โ A leftmost derivation is obtained by applying production to the leftmost variable in each step. Rightmost derivation โ A rightmost derivation is obtained by applying production to the rightmost variable in each step. Example Let any set of production rules in a CFG be X โ X+X | XX |X| a over an alphabet {a}. The leftmost derivation for the string "a+aa" may be โ X โ X+X โ a+X โ a + XX โ a+aX โ a+a*a The stepwise derivation of the above string is shown as-
Left and Right Recursive Grammars In a context-free grammar G, if there is a production in the form X โ Xa where X is a non-terminal and โaโ is a string of terminals, it is called a left recursive production. The grammar having a left recursive production is called a left recursive grammar.
And if in a context-free grammar G, if there is a production is in the form X โ aX where X is a non-terminal and โaโ is a string of terminals, it is called a right recursive production. The grammar having a right recursive production is called a right recursive grammar.
Ambiguity in Grammar A grammar is said to be ambiguous if there exists more than one leftmost derivation or more than one rightmost derivation or more than one parse tree for the given input string. If the grammar is not ambiguous, then it is called unambiguous. If the grammar has ambiguity, then it is not good for compiler construction. No method can automatically detect and remove the ambiguity, but we can remove ambiguity by re-writing the whole grammar without ambiguity. Example 1: Let us consider a grammar G with the production rule
Since there are two parse trees for a single string "3 * 2 + 5", the grammar G is ambiguous. QUES 2: Check whether the given grammar G is ambiguous or not. E โ E + E E โ E - E E โ id Solution: From the above grammar String "id + id - id" can be derived in 2 ways: First Leftmost derivation E โ E + E โ id + E โ id + E - E โ id + id - E โ id + id- id
Second Leftmost derivation E โ E - E โ E + E - E โ id + E - E โ id + id - E โ id + id - id Since there are two leftmost derivation for a single string "id + id - id", the grammar G is ambiguous.
3. Check whether the given grammar G is ambiguous or not. S โ aSb | SS S โ ฮต FOR INPUT STRING โaabbโ