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 given context-free grammar • CFG Design
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
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
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 }
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.
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.
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.
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.
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. {}
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?
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 –
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
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
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
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
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
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)
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
For next time ● Can you give an unambiguous grammar for the language of arithmetic expressions? ● Closure properties of CFG ● Push Down Automata
Recommend
More recommend