structural induction
play

Structural Induction Principles Suppose Context Free Languages - PDF document

Structural Induction Principles Suppose Context Free Languages U is a set, I is a subset of U (BASIS), Op is a set of operations on U (INDUCTION). Parse Trees and Ambiguity L is a subset of U defined recursively as


  1. Structural Induction • Principles – Suppose Context Free Languages – U is a set, – I is a subset of U (BASIS), – Op is a set of operations on U (INDUCTION). Parse Trees and Ambiguity – L is a subset of U defined recursively as follows: • I ⊆ L • L is closed under each operation in Op • L is the smallest set satisfying 1 & 2 Structural Induction Structural Induction • Then • Show that all strings x generated by • S → ε (1) – To prove that every element of L has some • S → 0S1 (2) property P, it is sufficient to show: • S → 1S0 (3) • 1. Every element of I has property P • S → SS (4) • 2. The set of elements of L having property P is closed under Op – Must have the property n 0 (x) = n 1 (x) – #2: If x ∈ L has property P, Op(x) also must have property P Structural Induction Structural Induction • U = { 0, 1 } * • Show for BASE case: • I = { ε } – ε has equal number of 0s and 1s • Show that P is closed under Op • Ops • S → 0S1 (2) – If x has property P (INDUCTIVE HYPOTHESIS) • S → 1S0 (3) – Then Op (x) also has property P • S → SS (4) • P: n 0 (x) = n 1 (x) – If ( n 0 (x) = n 1 (x)) then n 0 (Op(x)) = n 1 (Op(x)) 1

  2. Plan for 2 nd half Structural Induction • INDUCTION: Show true for each Op • Ambiguous Grammars and Parse Trees • S → 0S1 • S → 1S0 • Questions? • S → SS • DONE! • Questions Parse Trees Another example • Find a CFG to describe: • Graphical means to illustrate a derivation of – L = {a i b j c k | i = k} a string from a grammar • S → B (1) – Root of the tree = start variable • S → aSc (2) – Interior nodes = other variables • B → bB (3) • Children of nodes = application of a production rule • B → ε (4) – Leaf nodes = Terminal symbols – Can also write as • S → B | aSc • B → bB | ε Another example Parse Tree S • Let’s derive a string from L: aabbcc • An inorder traversal of Rule 2 – S ⇒ aSc rule 2 the tree will give the a S c S ⇒ aaScc rule 2 the string derived. Rule 2 a S c S ⇒ aaBcc rule 1 S ⇒ aabBcc rule 3 B Rule 1 S ⇒ aabbBcc rule 3 b B Rule 3 S ⇒ aabb ε cc rule 4 Rule 3 b B = aabbcc Rule 4 ε 2

  3. Recall our example from last One more example time • Defining the grammar for algebraic • Show derivation for a + a * a expressions – Production rules – S ⇒ S + S rule 1 – S → S + S (1) S ⇒ a + S rule 6 S → S – S (2) S ⇒ a + S * S rule 3 S → S * S (3) S ⇒ a + a * S rule 6 S → S / S (4) S ⇒ a + a * a rule 6 S → (S) (5) S → a (6) Parse Tree One more example – S ⇒ S + S • Another derivation for a + a * a S rule 1 – S ⇒ S * S rule 3 S ⇒ a + S S + S S ⇒ S * a rule 6 rule 6 S ⇒ a + S * S S ⇒ S + S * a rule 1 S * S a rule 3 S ⇒ a + S * a rule 6 S ⇒ a + a * S S ⇒ a + a * a rule 6 a a rule 6 S ⇒ a + a * a rule 6 Parse Tree Parse trees – S ⇒ S * S S rule 3 S S S ⇒ S * a S * S S + S S * S rule 6 a S + S S ⇒ S + S * a a S + S rule 1 S * S a S ⇒ a + S * a a a a a rule 6 a a S ⇒ a + a * a Same string, 2 derivations rule 6 3

  4. Ambiguity Famous programming language ambiguity • A CFG is said to be ambiguous if there is at • Dangling else least 1 string in L(G) having two or more – <stmt> → if (<expr>) <stmt> | distinct derivations. if (<expr>) <stmt> else <stmt> | <some_other_stmt> if (expr1) if (expr2) f(); else g(); if (expr1) if (expr2) f(); else g(); To which if does the else belong? Famous programming language ambiguity Famous programming language ambiguity stmt stmt if ( expr ) stmt else stmt if ( expr ) stmt expr1 g(x); expr1 if ( expr ) stmt if ( expr ) stmt else stmt expr2 f(x); expr2 f(x); g(x); In this derivation, the else belongs to the 1 st if if (expr1) if (expr2) f(); else g(); if (expr1) if (expr2) f(); else g(); Famous programming language ambiguity Famous programming language ambiguity stmt s2 • A way to fix this – <stmt> → <s1> | <s2> if ( expr ) s1 <s1> → if (<expr>) <s1> else <s1> | <otherstmt> <s2> → if (<expr>) <stmt> | expr1 if (<expr>) <s1> else <s2> if ( expr ) s1 else s1 <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

  5. Ambiguity Ambiguity • Some languages are inherently ambiguous • Showing a grammar is ambiguous is easy – All possible grammars that generate the – Find a string x in the L(G) that has two language are ambiguous derivations • Unfortunately, there is no algorithm that can • Showing a particular grammar is not tells us whether a grammar is ambiguous or ambiguous is usually difficult. not. • Showing that any grammar is not ambiguous is not possible. Derivations Derivations a + a + a • Leftmost derivations S S – A leftmost derivation is one where the leftmost variable in the current string is always the first S + S S + S to get replaced via a production rule. a S + S – A rightmost derivation is one where the S + S a rightmost variable in the current string is a a always the first to get replaced via a production a a rule. rightmost derivation leftmost derivation Ambiguity Removing ambiguities • As it turns out (we won’t prove this) • Since some languages are inherently ambiguous – This cannot always be done – In unambiguous grammars,leftmost derivations • In fact, will always be unique. – We can/will show there is no “algorithm” for – In unambiguous grammars,rightmost determining if a CFG is ambiguous derivations will always be unique. • However, – On a case by case basis, ambiguities can be eliminated 5

  6. Example Example • Abbreviated grammar for algebraic • This grammar has two problems expressions – Production rules 1. Precedence of operators is not respected – S → S + S (1) a * a + a should be interpreted as (a*a) + a S → S * S (2) 2. Sequence of identical operators can be grouped either from the left or the right S → (S) (3) a + a + a can be interpreted as either (a+a)+a or a + (a S → a (4) + a) Example Example • Solution • Our new grammar – S → S + T | T – Introduce some new variables – T → T * F | F • Factor – expression that cannot be broken up by either * or + – a – F → (S) | a – (S) • Note that • Term – expression that cannot be broken up by + – All Factors – all recursion is leftmost – T * F – * has higher precedent than + • Expression – all possible expression – a + a + a + a * a is interpreted as – All Terms • ((a+a) + a) + (a*a) – S + T Example Example S a + a + a + a * a • It can be shown S + T – That every string x, that is generated by this new grammar, has only one leftmost derivation S + T T * F – As such this new grammar is unambiguous a S + T F F – Done using induction on the |x|. a a F a a 6

  7. Summary Summary – Today • Ambiguity • Context Free Grammars – A grammar is ambiguous if there is a string generated • Parse Trees and Ambiguity by the grammar that has two distinct derivations. – Some languages are inherently ambiguous • All grammars that generate the language are ambiguous – 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? Next time • Pushdown Automata • CFG Problem Session 7

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