El-Shorouk cademy cad. Year : 2013 / 2014 Higher Institute for Computer & Term : 2nd Information Technology Year : 4th Computer Science Department Supervisor: Prof. Dr. hmed bbassy Section contents: mbiguity in Grammar Dangling Else Problem Compilers Section (6) mbiguous Grammars Consider the grammar exp exp PLUS exp exp exp MINUS exp exp exp TIMES exp exp exp DIVIDE exp exp INT_LIT Construct Parse tree for 3-4/2 re there more than one parse trees? If there is more than one parse tree for a string then the grammar is ambiguous mbiguity causes problems with parsing (what is the correct structure)? Precedence in Grammars To write a grammar whose parse trees express precedence correctly, use a different nonterminal for each precedence level. Start by writing a rule for the operator(s) with the lowest precedence ("-" in our case), then write a rule for the operator(s) with the next lowest precedence, etc: exp exp MINUS exp term term term DIVIDE term factor factor INT_LIT LPREN exp RPREN Now try constructing multiple parse trees for 3-4/2 Grammar is still ambiguous. Look at associativity. Construct 2 parses tree for 5-3-2. Recursion on CFGs grammar is recursive in nonterminal X if: X derives a sequence of symbols that includes an X. grammar is left recursive in X if: X derives a sequence of symbols that starts with an X. grammar is right recursive in X if: X derives a sequence of symbols that ends with an X. For left associativity, use left recursion. For right associativity, use right recursion. mbiguity Removed in CFG exp exp MINUS term term term term DIVIDE factor factor factor INT_LIT LPREN exp RPREN Eng. Maha Talaat Page 1
One level for each order of operation Left recursion for left assiciativity Now try constructing 2 parse trees for 5-3-2. Write down an unambiguous grammar that generates the set of strings { s;, s;s;, s;s;s;,... } and give leftmost and rightmost derivation for the string s;s; using your grammar. stmt-sequence s ; stmt-sequence s; Left most derivation (1) stmt-sequence => s;stmt-sequence (2) => s;s; Right most derivation (1) stmt-sequence => s;stmt-sequence (2) => s;s; leftmost will be the same as rightmost derivation for s;s; Given the grammar ( ) e a- describe the language it generates generates the strings of all "balanced parentheses." Description : This grammar produces an infinite set of strings of the characters ( and ) in which 0 or more ( ) can be generated and each ( ) can contain 0 or more ( ) inside. b- show that it is ambiguous consider the expression ( ) ( ) ( ) It has 2 different parse trees Parse Tree(1) ( ) ( ) ( ) Eng. Maha Talaat Page 2
Parse Tree(2) ( ) ( ) ( ) So, this grammar is ambiguous The following grammar generates all regular expressions over the alphabet of letters rexp rexp rexp rexp rexp rexp * ( rexp ) letter a- give a derivation for the regular expressions ( ab b )* using this grammar. (1) rexp => rexp * [exp rexp * ] (2) rexp => ( rexp ) * [exp ( rexp ) ] (3) => (rexp rexp ) * [exp rexp rexp] (4) => (rexp rexp rexp ) * [exp rexp rexp] (5) => (letter rexp rexp ) * [exp letter] (6) =>( a rexp rexp) * [letter a] (7) =>( a letter rexp) * [exp letter] (8) =>( a b rexp) [letter b] (9) =>( a b rexp) [exp letter] (10) =>( a b b) [letter c] b- Show that this grammar is ambiguous this grammar is ambiguous because it generates 2 different parse trees for the expression ab c c- Rewrite this grammar to establish the correct precedence of the operators rexp rexp rterm rterm rterm rterm rfactor rfactor rfactor rfactor * (rexp) letter d- What associativity does your answer in part c give to the binary operator? why? the associativity is left because a series of operators with same precedence will be evaluated from left to right Eng. Maha Talaat Page 3
Write an unambiguous grammar for Boolean expressions that includes the constants true and false, and the operator and,or,not and parentheses. Be sure to give or lowest precedence and give the not the highest precedence.lso allow repeated not s. Similar to the arithmetic expression example bexp bexp or term b term or b term b term and notfactor notfactor and && notfactor not bfactor b factor not! b factor (b exp ) b literal bliteral true false Given the following grammar if-stmt other e if-stmt if( exp ) else-part else-part else e exp 0 1 a- draw a parse tree for the string if (0) if(1) other else else other If-stmt if ( exp ) Else-part else 0 other if ( exp ) Else-part 1 other else Eng. Maha Talaat Page 4
b- what is the purpose of 2 else s the purpose of 2 else is to match with 2 if c- is similar code permissible in C and C#? explain this code is permissible in C Eng. Maha Talaat Page 5