plan for 2 nd half
play

Plan for 2 nd half Ambiguous Grammars and Parse Trees Context Free - PDF document

Plan for 2 nd half Ambiguous Grammars and Parse Trees Context Free Languages Questions? Parse Trees and Ambiguity Text note Parse Trees We will not be covering the conversions / Graphical means to illustrate a derivation of


  1. Plan for 2 nd half • Ambiguous Grammars and Parse Trees Context Free Languages • Questions? Parse Trees and Ambiguity Text note Parse Trees • We will not be covering the conversions / • Graphical means to illustrate a derivation of proofs . a string from a grammar – Root of the tree = start variable – Interior nodes = other variables • Children of nodes = application of a production rule – Leaf nodes = Terminal symbols Another example Another example • Find a CFG to describe: • Let’s derive a string from L: aabbcc – L = {a i b j c k | i = k} – S ⇒ aSc rule 2 • S → B (1) S ⇒ aaScc rule 2 • S → aSc (2) S ⇒ aaBcc rule 1 • B → bB (3) S ⇒ aabBcc rule 3 • B → ε (4) S ⇒ aabbBcc rule 3 – Can also write as S ⇒ aabb ε cc rule 4 • S → B | aSc = aabbcc • B → bB | ε

  2. Recall our example from last Parse Tree time S • Defining the grammar for algebraic • An inorder traversal of Rule 2 expressions – Production rules the tree will give the a S c the string derived. – S → S + S (1) Rule 2 a S c S → S – S (2) B Rule 1 S → S * S (3) S → S / S (4) b B Rule 3 S → (S) (5) Rule 3 b B S → a (6) Rule 4 ε One more example Parse Tree – S ⇒ S + S • Show derivation for a + a * a S rule 1 – S ⇒ S + S rule 1 S ⇒ a + S S + S S ⇒ a + S rule 6 rule 6 S ⇒ a + S * S rule 3 S ⇒ a + S * S S * S a rule 3 S ⇒ a + a * S rule 6 S ⇒ a + a * S S ⇒ a + a * a rule 6 a a rule 6 S ⇒ a + a * a rule 6 One more example Parse Tree – S ⇒ S * S • Another derivation for a + a * a S rule 3 – S ⇒ S * S rule 3 S ⇒ S * a S * S S ⇒ S * a rule 6 rule 6 a S + S S ⇒ S + S * a rule 1 S ⇒ S + S * a rule 1 S ⇒ a + S * a rule 6 a a S ⇒ a + S * a S ⇒ a + a * a rule 6 rule 6 S ⇒ a + a * a rule 6

  3. Parse trees Ambiguity • A CFG is said to be ambiguous if there is at S S least 1 string in L(G) having two or more distinct derivations. S + S S * S a S + S S * S a a a a a Same string, 2 derivations Famous programming language ambiguity Famous programming language ambiguity stmt • Dangling else – <stmt> → if (<expr>) <stmt> | if ( expr ) stmt else stmt if (<expr>) <stmt> else <stmt> | expr1 g(x); <some_other_stmt> if ( expr ) stmt if (expr1) if (expr2) f(); else g(); expr2 f(x); To which if does the else belong? In this derivation, the else belongs to the 1 st if if (expr1) if (expr2) f(); else g(); Famous programming language ambiguity Famous programming language ambiguity stmt • A way to fix this – <stmt> → <s1> | <s2> if ( expr ) stmt <s1> → if (<expr>) <s1> else <s1> | <otherstmt> <s2> → if (<expr>) <stmt> | expr1 if (<expr>) <s1> else <s2> if ( expr ) stmt else stmt <s1> represents if statements with matching else <s2> represent if statements with at least 1 unmatched if expr2 f(x); g(x); The <s1> in the rule for <s2> will assure that all statements between if and else will not have a dangling else. if (expr1) if (expr2) f(); else g();

  4. Ambiguity Famous programming language ambiguity stmt s2 • Some languages are inherently ambiguous if ( expr ) s1 – All possible grammars that generate the language are ambiguous expr1 • Unfortunately, there is no algorithm that can tells us whether a grammar is ambiguous or if ( expr ) s1 else s1 not. expr2 f(x); g(x); if (expr1) if (expr2) f(); else g(); Ambiguity Derivations • Showing a grammar is ambiguous is easy • Leftmost derivations – Find a string x in the L(G) that has two – A leftmost derivation is one where the leftmost derivations variable in the current string is always the first to get replaced via a production rule. • Showing a particular grammar is not – A rightmost derivation is one where the ambiguous is usually difficult. rightmost variable in the current string is • Showing that any grammar is not always the first to get replaced via a production ambiguous is not possible. rule. Derivations Ambiguity a + a + a • As it turns out (we won’t prove this) S S – In unambiguous grammars,leftmost derivations will always be unique. S + S S + S – In unambiguous grammars,rightmost a S + S derivations will always be unique. S + S a a a a a rightmost derivation leftmost derivation

  5. Removing ambiguities Example • Since some languages are inherently ambiguous • Abbreviated grammar for algebraic – This cannot always be done expressions – Production rules • In fact, – S → S + S (1) – We can/will show there is no “algorithm” for S → S * S (2) determining if a CFG is ambiguous S → (S) (3) • However, S → a (4) – On a case by case basis, ambiguities can be eliminated Example Example • This grammar has two problems • Solution – Introduce some new variables 1. Precedence of operators is not respected • Factor – expression that cannot be broken up by either * or + a * a + a should be interpreted as (a*a) + a – a 2. Sequence of identical operators can be – (S) grouped either from the left or the right • Term – expression that cannot be broken up by + – All Factors a + a + a can be interpreted as either (a+a)+a or a + (a – T * F + a) • Expression – all possible expression – All Terms – S + T Example Example S • Our new grammar a + a + a + a * a – S → S + T | T S + T – T → T * F | F S + T T * F – F → (S) | a • Note that a S + T F F – all recursion is leftmost a a – * has higher precedent than + F a – a + a + a + a * a is interpreted as a • ((a+a) + a) + (a*a)

  6. Example Summary • It can be shown • Ambiguity – A grammar is ambiguous if there is a string generated – That every string x, that is generated by this by the grammar that has two distinct derivations. new grammar, has only one leftmost derivation – Some languages are inherently ambiguous – As such this new grammar is unambiguous • All grammars that generate the language are ambiguous – Done using induction on the |x|. – There is no algorithm to determine if any given grammar is ambiguous • Proving a grammar to be ambiguous is easy • Proving that a grammar is not is hard. – Questions? Summary – Today Next time • Context Free Grammars • Exam 1 • Parse Trees and Ambiguity • CFG Problem Session

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend