cse 105
play

CSE 105 THEORY OF COMPUTATION Fall 2016 - PowerPoint PPT Presentation

CSE 105 THEORY OF COMPUTATION Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/ Today's learning goals Sipser Ch 1.4, 2.1 Non-regular languages Define context-free grammars Test if a specific string can be generated by a


  1. CSE 105 THEORY OF COMPUTATION Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/

  2. Today's learning goals Sipser Ch 1.4, 2.1 • Non-regular languages • Define context-free grammars • Test if a specific string can be generated by a given context-free grammar • CFG Design

  3. Context-free grammar Sipser Def 2.2, page 102 (V, Σ, R, S) Variables : finite set of (usually upper case) variables V Terminals : finite set of alphabet symbols Σ Rules / Productions : finite set of allowed moves R Start variable : S

  4. Context-free language Sipser p. 104 The language of a CFG (V, Σ, R, S) is { w in Σ * | Starting with the Start variable and applying zero or more rules, can get to w} Notation: Notation: S ⇒ * w S ⇒ * w Terminology: sequence of Terminology: sequence of rule applications is rule applications is derivation derivation

  5. Example CFG ● G=(V={S},T={0,1},R,S) where the rules R are 1) S → 0S1 2) S → ε ● Derivation: – S → 0S1 → 00S11 → 000S111 → 000111 ● L(G)={0 n 1 n }

  6. An example? Consider the CFG ({S}, {0}, R, S) where R is the following set of rules S →0S S →0 Is this a well-formed defjnition? A. No: there's more than one rule B. No: the same LHS gets sent to two difgerent strings. C. No: one of the string in the RHS has both variables and literals D. Yes. E. I don't know.

  7. Context-free language Sipser p. 104 The language of a CFG (V, Σ, R, S) is { w in Σ * | Starting with the Start variable and applying zero or more rules, can get to w} What is the language of the CFG ({S}, {0}, R, S) with R = {S→0S, S→0} ? A. {0} B. {0, 0S} C. {0, 00, 000, …} D. {ε, 0, 00, 000, …} E. I don't know.

  8. Context-free language Sipser p. 104 What is the language of the CFG ({S}, {0,1}, R, S) with R = the set of rules S →0S S → 0S | 1S | ε S → 0S | 1S | ε S→1S S→ε A. L(0 * 1 * ) B. L(0 * U 1 * ) C. L( (0 U 1) * ) D. L ( (0 * 1 * ) ) * E. I don't know.

  9. Context-free language Sipser p. 104 What is the language of the CFG ({S}, {0,1}, R, S) with R = the set of rules S → 0S | T T → 1T | ε A. L(0 * 1 * ) B. L(0 * U 1 * ) C. L( (0 U 1) * ) D. L ( (0 * 1 * ) ) * E. I don't know.

  10. Context-free language Sipser p. 104 What is the language of the CFG ({S}, {0,1}, R, S) with R = the set of rules S → 0S | T T → 1T | S A. L(0 * 1 * ) B. L(0 * U 1 * ) C. L( (0 U 1) * ) D. L ( (0 * 1 * ) ) * E. {}

  11. Questions about CFG ● What languages can be described by a CFG? ● Do CFG correspond to some “executable” computational model? ● Is the class of languages described by CFGs closed under union, concatenation, star, intersection, complement? ● Are there languages that cannot be described by a CFG?

  12. Arithmetic Expressions ● (3+12)*(5+7+(5*63^2)) ● Grammar rules: S → S+S | S*S | S^N | N N → D | DN D → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ● Derivations S → DN → 1N → 1DN→ 12N→ 12D → 123 – S → S+S → 1+S → 1+S*S →* 1+2*S →* 1+2*3 –

  13. Parse trees and Leftmost derivations ● 1+2*3 ● S → S+S → S+S*S →* S+2*3 →* 1+2*3 ● S → S+S →* 1+S → 1+S*S →* 1+2*3 (leftmost) S S S + S S * 1 2 3

  14. Parse trees and Leftmost derivations ● 1+2*3 ● S → S+S → S+S*S →* S+2*3 →* 1+2*3 ● S → S+S →* 1+S → 1+S*S →* 1+2*3 (leftmost) S S S + ● For each derivation, there is a unique parse three S S ● For each parse tree, there is a unique leftmost derivation * 1 2 3

  15. Ambiguity ● S → S+S → S+S+S → 1+2+3 ● S → S+S → 1+S → 1+S+S → 1+2+3 S S S 1 + 3 + S 1 2 + 2 + 3

  16. Ambiguity ● S → S+S → S+S+S → 1+2+3 ● S → S+S → 1+S → 1+S+S → 1+2+3 1+(2+3) S S S 1 + 3 + S 1 2 + 2 + 3 (1+2)+3

  17. Ambiguity ● S → S+S → S+S+S → 1+2+3 ● S → S+S → 1+S → 1+S+S → 1+2+3 1+(2*3) = 7 S S S 1 * 3 + S 1 2 + 2 * 3 (1+2)*3 = 9

  18. Unambiguous Grammars ● A CFG G is unambiguous if every w in L(G) has a unique parse tree (or, equivalently, a unique leftmost derivation) ● A CFG is ambiguous if there exists a w in L(G) such that w has two difgerent parse trees (or, equivalently, two difgerent leftmost derivations)

  19. Test time ● Is the following grammar ambiguous? S → S+S | S*S | 0 | 1 A) Yes B) No C) It depends on the input string D) There is no answer because the question is ambiguous E) I don’t know

  20. For next time ● Can you give an unambiguous grammar for the language of arithmetic expressions? ● Closure properties of CFG ● Push Down Automata

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