CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.

Slides:



Advertisements
Similar presentations
4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
CFGs and PDAs Sipser 2 (pages ). Long long ago…
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CFGs and PDAs Sipser 2 (pages ). Last time…
Decidable languages Sipser 4.1 (pages ). CS 311 Mount Holyoke College 2 Hierarchy of languages All languages Turing-recognizable Turing-decidable.
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Context-Free Grammars Lecture 7
1 Context Free Languages October 2, Announcement HW 3 due now.
Balanced Parentheses G = (V, , S, P) V = {S}  = {(,)} Start variable is S P = { S --> (S) | SS | /\}
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Normal forms for Context-Free Grammars
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
CMSC 330: Organization of Programming Languages
CS 461 – Sept. 26 CFG correctness Section 2.2 – Pushdown Automata.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Syntax Analysis The recognition problem: given a grammar G and a string w, is w  L(G)? The parsing problem: if G is a grammar and w  L(G), how can w.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
Pushdown Automata (PDAs)
1 Context-Free Languages Not all languages are regular. L 1 = {a n b n | n  0} is not regular. L 2 = {(), (()), ((())),...} is not regular.  some properties.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Context-Free Grammars
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
PART I: overview material
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
CS 3240: Languages and Computation Context-Free Languages.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
Syntax Context-Free Grammars, Syntax Trees. CFG for Arithmetic Expressions E::= E op E| (E) | num op ::= + | * num ::= 0 | 1 | 2 | … Backus-Naur Form.
CFG1 CSC 4181Compiler Construction Context-Free Grammars Using grammars in parsers.
CMSC 330: Organization of Programming Languages
Regular Expressions and Languages A regular expression is a notation to represent languages, i.e. a set of strings, where the set is either finite or contains.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Context-Free Languages
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Spring
CSE 311 Foundations of Computing I Lecture 19 Recursive Definitions: Context-Free Grammars and Languages Autumn 2012 CSE
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CSE 311 Foundations of Computing I Lecture 18 Recursive Definitions: Context-Free Grammars and Languages Autumn 2011 CSE 3111.
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
Introduction to Parsing
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
Context-Free Grammars: an overview
CS 404 Introduction to Compiler Design
Theorem 29 Given any PDA, there is another PDA that accepts exactly the same language with the additional property that whenever a path leads to ACCEPT,
Context-Free Languages
CHAPTER 2 Context-Free Languages
فصل دوم Context-Free Languages
Theory of Computation Lecture #
CFGs: Formal Definition
Presentation transcript:

CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness

Derivations A sequence of steps showing how a word is generated using grammar rules. Drawing a diagram or tree can help. Example: derivation of 1110 from this grammar: S  ABS A  ε | 1AAB B  ε | 1B0 1A1B0 11A1ε0 11ε10 11

Derivations, con’d There are several ways to express a derivation –Tree –Leftmost derivation = as you create string, replace leftmost variable. Example: S  AB  1AB  11AB  11B  111B0  1110 –Rightmost derivation Often, grammars are ambiguous –For some string,  2+ derivation trees. Or, equivalently:  2+ leftmost derivations. –Example?

More examples Even number of 0’s Words of form { 0 i 1 j } where j is constrained. i  j  2i (not to be confused with { 0 n 1 2n } ) i  j  2i + 3 i  j  1.5i Next topic: how do we show that our grammar is correct?

Correctness Given a language L and a grammar G, how do we know if L = L(G)? Must show: 1.L  L(G). All words in L can be derived by grammar. Tell how to derive the words. 2.L(G)  L. All words derived by grammar are in L. Use induction.

First example Show that S  ε | 0S1 is a CFG for the language { 0 n 1 n } Step #1: Any word in 0 n 1 n can be generated by the grammar. We explain the derivation. –Apply the rule 0S1 n times –Finally, apply the ε rule. Step #2: All words generated by the grammar are of the form 0 n 1 n. –Induction on the number of times we use a rule. –S  ε for the basis, and S  0S1 for the inductive step.

Second example L is { even number of 0s } and G is S  ε | S1 | S0S0 Step #1. Suppose w  L. How can G generate w? If w = ε, we’re done. Do the following until w is ε: If w ends with 1s, take them off by virtue of “S1”. Now w ends with 0, so take 2 0’s away by virtue of “S0S0”. We know there must be 2 0’s in w because w had an even number to start with.

Illustration S  ε | S1 | S0S0 Let’s see how we can derive “010100”: The last 2 0s: S  S0S0  ε0ε0 = is is S  S0S0  ε0S10  ε0ε10 = is S  S1  ε1 = 1 We can reconstruct derivation from beginning in form of a tree.

L(G) is in L S  ε | S1 | S0S0 Step #2: Need to show all generated words are in L. Base case: ε is in L. It has even number of 0s. Recursive case. Let w be a word generated by the grammar. Assume it has even # 0s. We can create a larger word by applying rule “S1” or “S0S0”. S1 adds no more 0s. S0S0 adds two 0s. Either way the # of 0s stays even.

Extra example Let’s show that this grammar G S  ε | 0S1 | 00S111 generates the language L = { 0 i 1 j | i  j  1.5i } Step #1: any word in L can be generated –This is the hard part, so let’s motivate the proof with an example or two. Step #2: all words generated are in L –Just induction again.

L is in L(G) S  ε | 0S1 | 00S111 How could we generate ? –We have 5 extra 1s. So we use rule 00S111 five times. –Undoing these steps, # 0s = 11-5*2 = 1 and # 1s = 16-5*3 = 1. Then we use 0S1 once and we are left with ε. Okay, how about ? –We have 4 extra 1s, so use rule 00S111 four times. –Undoing these steps, # 0s = 12-4*2 = 4 and # 1s = 16-4*3 = 4. They match! So use “0S1” 4 times.

Thinking out loud… S  ε | 0S1 | 00S111 Let w = 0 i 1 j  L. In other words, i  j  1.5i Consider the number j – i. This is the number of times to apply rule #3 (00S111). Note that using rule #3 (j – i) times will account for 2(j – i) zeros. Then apply rule #2 (0S1) the “appropriate number” of times. How many? Well, we want i 0’s and we’ve already got 2(j – i), so we need the difference: i – 2(j – i) = 3i – 2j.

Finishing step 1 S  ε | 0S1 | 00S111 Let w = 0 i 1 j  L. In other words, i  j  1.5i Use rule #1 once to start with empty word. Use rule #3 (j – i) times. Use rule #2 (3i – 2j) times. Total # 0s = 3i – 2j + 2(j – i) = i Total # 1s = 3i – 2j + 3(j – i) = j Thus, the word can be generated.

L(G) is in L S  ε | 0S1 | 00S111 Now the easy part. Need to show that when we apply any rule, we preserve: i  j  1.5i Base case: ε has no 0s or 1s. 0  0  1.5*0 √ Recursive case. Let w be generated by the grammar with i 0s and j 1s satisfying i  j  1.5i. If we apply either rule 0S1 or 00S111, we can show (i + 1)  (j + 1)  1.5(i + 1) (i + 2)  (j + 3)  1.5(i + 2) (Need to work out arithmetic.)

Think about… Can you write a CFG for { 0 i 1 j | 0.5i  j  1.5i } ? in other words, the ratio of 1s to 0s must be between ½ and 3/2.