CSCI 2670 Introduction to Theory of Computing September 15, 2005.

Slides:



Advertisements
Similar presentations
Erik Jonsson School of Engineering and Computer Science FEARLESS Engineering CS 4384 – 001 Automata Theory Thursday: Context-Free.
Advertisements

Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
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.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
CSCI 2670 Introduction to Theory of Computing September 15, 2004.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
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.
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
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 20, 2005.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
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:
Grammars CPSC 5135.
Context-free Languages
Context-Free Grammars – Derivations Lecture 15 Section 2.1 Mon, Sep 24, 2007.
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”
Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein.
Midterm 1 Breakdown >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7.
CSCI 2670 Introduction to Theory of Computing September 22, 2005.
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 3: Introduction to Syntactic Analysis.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
CSCI 2670 Introduction to Theory of Computing September 13.
CSCI 2670 Introduction to Theory of Computing September 23, 2004.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
CSCI 2670 Introduction to Theory of Computing October 13, 2005.
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
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
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.
Theory of Languages and Automata By: Mojtaba Khezrian.
CSE 311 Foundations of Computing I Lecture 20 Context-Free Grammars and Languages Autumn 2012 CSE
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Context-Free Grammars: an overview
Formal Language & Automata Theory
CSE 105 theory of computation
Context-free Languages
CHAPTER 2 Context-Free Languages
Context-Free Grammars
Context-Free Languages
Chapter 2 Context-Free Language - 01
CSE 105 theory of computation
Context-Free Grammars
CSE 105 theory of computation
Presentation transcript:

CSCI 2670 Introduction to Theory of Computing September 15, 2005

Agenda Yesterday –Introduce context-free grammars Today –No quiz! Quiz postponed until Monday –Regular expressions, pumping lemma, CFG’s –Build CFG’s

Context-free grammar definition A context-free grammar is a 4-tuple (V, ,R,S), where 1.V is a finite set called the variables, 2.  is a finite set, disjoint from V, called the terminals, 3.R is a finite set of rules, with each rule being a variable and a string of variables and terminals, and 4.S  V is the start variable.

More definitions If u, v, and w are strings of variables and terminals, and A  w is a rule of the grammar, we say uAv yields uwv –Denoted uAv  uwv If a sequence of rules leads from u to v – i.e., u  u 1  u 2  …  v, we denote this u  * v (I can’t do the actual notation in powerpoint – the * should be over the double bars) The language of the grammar is {w   * | s  * w}

Example A  Ab | Bb B  aBb | ab V = {A,B}  = {a,b} R is the set of rules listed above S = A The language of this grammar is {w  {a,b} * | w = a n b m, m > n > 0}

Designing CFG’s Requires creativity There are some guidelines to help –Union of two CFG’s –Converting a DFA to a CFG –Linked terminals –Recursive behavior

Designing the union of two CFG’s For the union of k CFG’s, design each CFG separately with starting variables S 1, S 2, …, S k and combine using the rule S  S 1 | S 2 | … | S k What is the CFG for the following language {a i b j c k | i, j, k ≥ 0 and i = j or j = k} {a i b j c k | i, j, k ≥ 0 and i = j}  {a i b j c k | i, j, k ≥ 0 and j = k}

Example First design {a i b j c k | i, j, k ≥ 0 and i=j} S 1  S 1 c | A A  aAb |  Then design {a i b j c k | i, j, k ≥ 0 and j=k} –(use different variables) S 2  aS 2 | B B  bBc |  Finally, add the “unifying” rule S  S 1 | S 2

Converting DFA’s into CFG’s For each state q i in the DFA, make a variable R i for your CFG. For each transition rule  (q i,a)=q k in your DFA, add the rule R i  aR k to your CFG For each accept state q a in your DFA, add the rule R a  ε If q 0 is the start state in your DFA, then R 0 is the starting variable in your CFG

Example q1q1 0, 1 q2q q3q3 1 V = {R 1, R 2, R 3 } R 1  0R 3 | 1R 2 R 2  0R 1 | 1R 3 R 3  1R 3 | 1R 3 R 2  ε R 1 is the start state

Linked terminals Terminals may be “linked” to one another in that they have the same number of occurrences (or a related number) –{0 n 1 n | n ≥ 0}, {x n y 2n | n > 0} Add terminals simultaneously –S  0S1 | ε –S  xSyy | xyy

Recursive behavior Some languages may be built of pieces that are within the language –Legal pairing of parenthesis For these languages, you will want a recursive rule –S  SS Not all recursive rules will be that easy!

Example Construct a CFG accepting all strings in {0,1} * that have equal numbers of 0’s and 1’s S  S0S1S | S1S0S | ε

Ambiguity Consider the CFG ({S},{0,1},R,S), where the rules of R are S  0 | 1 | S + S | S * S Derive the string 0 * 1 + 1

Ambiguity S  0 | 1 | S + S | S * S 0 * S SS* S+S 0 11 S SS+ S*S 1 01 Different parse trees!

Definition of ambiguity A context-free grammar G generates a string w if there are two different parse trees that generate w –Different derivations that differ only in order do not indicate ambiguity

Derivation & ambiguity A derivation of a string w in a grammar G is a leftmost derivation if every step of the derivation replaced the leftmost variable A string is derived ambiguously in CFG G if it has two or more different leftmost derivations The grammar G is ambiguous if it generates some string ambiguously –Some grammars are inherently ambiguous