Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]

Slides:



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

1 Lecture 32 Closure Properties for CFL’s –Kleene Closure construction examples proof of correctness –Others covered less thoroughly in lecture union,
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.
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.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
CFGs and PDAs Sipser 2 (pages ). Last time…
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
CS5371 Theory of Computation
1 CSC 3130: Automata theory and formal languages Tutorial 4 KN Hung Office: SHB 1026 Department of Computer Science & Engineering.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
1 Module 28 Context Free Grammars –Definition of a grammar G –Deriving strings and defining L(G) Context-Free Language definition.
Languages, grammars, and regular expressions
104 Closure Properties of Regular Languages Regular languages are closed under many set operations. Let L 1 and L 2 be regular languages. (1) L 1  L 2.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
1 Module 31 Closure Properties for CFL’s –Kleene Closure construction examples proof of correctness –Others covered less thoroughly in lecture union, concatenation.
Normal forms for Context-Free Grammars
Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages.
Chapter 3: Formal Translation Models
How to Convert a Context-Free Grammar to Greibach Normal Form
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.
Context-free Grammars
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
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.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CMSC 330: Organization of Programming Languages
CSE 3813 Introduction to Formal Languages and Automata Chapter 8 Properties of Context-free Languages These class notes are based on material from our.
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
TM Design Universal TM MA/CSSE 474 Theory of Computation.
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:
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Chapter 5 Context-Free Grammars
Grammars CPSC 5135.
PART I: overview material
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.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory V Context-Free Grammars andLanguages.
CS 3240: Languages and Computation Context-Free Languages.
Midterm 1 Breakdown >79 3 >29 7 >69 5 >19 5 >59 7 >49 9 >39 7.
1 Module 31 Closure Properties for CFL’s –Kleene Closure –Union –Concatenation CFL’s versus regular languages –regular languages subset of CFL.
CMSC 330: Organization of Programming Languages
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Context-Free and Noncontext-Free Languages Chapter 13 1.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
Re-enter Chomsky More about grammars. 2 Parse trees S  A B A  aA | a B  bB | b Consider L = { a m b n | m, n > 0 } (one/more a ’s followed by one/more.
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.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Chapter 5 Context-free Languages
Context-Free Languages
Chapter 8 Properties of Context-free Languages These class notes are based on material from our textbook, An Introduction to Formal Languages and Automata,
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.
1 Language Recognition (11.4) Longin Jan Latecki Temple University Based on slides by Costas Busch from the courseCostas Busch
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
Dept. of Computer Science & IT, FUUAST Theory of Computation 2 Regular Expressions Regular Expression.
Lecture 6: Context-Free Languages
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Formal Language & Automata Theory
Theory of Computation Lecture #
Presentation transcript:

Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]

Context-free Grammars Def: A context-free grammar (CFG) is a 4-tuple G = (V, ,S,P) where - V is a finite set of variables (nonterminal symbols) -  is a finite set of terminal symbols (terminals) - S 2 V is the start symbol - P is the finite set of grammar rules (productions) of the form A  , where A 2 V and  2 (V [  ) * (V and  are disjoint.) For example from previous slide : [Section 6.1]

Context-free Grammars A derivation of the string “abaaba” : Def : Let G=(V, ,S,P) be a CFG. For ,  2 (V [  ) * we write  ) G  if there is a production C   in P such that  =  1 C  2 and  =  1  2. We say that  is derived from . If  1, …,  n 2 (V [  ) * and  k )  k+1 for k=1,…,n-1, we write  1 ) G *  n. Language generated by G, denoted L(G), is { x 2  * | ______ } [Section 6.1]

Context-free Grammars Examples : { a k b k | k ¸ 0 } { x 2 {a,b} * | x has at least 2 b’s } { x 2 {a,b} * | |x| is even } [Section 6.1]

Context-free Grammars Example : S   | abS | baS | aSb | bSa | Sab | Sba What is the language generated by this grammar ? (We’ve seen this before…) [Section 6.1]

Context-free Grammars Our G : S   | abS | baS | aSb | bSa | Sab | Sba | SS Recall that we proved (using structural induction) that every string generated by G contains the same number of a’s and b’s. Moreover, we can generate every string with equal number of a’s and b’s by the rules of G. [Section 6.1]

Closure Properties of CFL’s Given are two CFG’s G 1 = (V 1, ,S 1,P 1 ) and G 2 = (V 2, ,S 2,P 2 ). - Give a CFG G such that L(G) = L(G 1 ) [ L(G 2 ). - Give a CFG G such that L(G) = L(G 1 )L(G 2 ). - Give a CFG G such that L(G) = L(G 1 ) *. Thus, context-free languages (CFL’s) are closed under union, concatenation, and Kleene’s star. [Section 6.2]

Derivation Trees & Ambiguity Consider G:S  S + S | S * S | (S) | x Describe the language generated by G: Give all derivations of x + x * x : [Section 6.4]

Derivation Trees We will draw derivation trees of x + x * x : A leftmost derivation is a derivation in which each derivation step uses a rule for the leftmost nonterminal symbol. Derivation trees are in 1-1 correspondence with leftmost derivations. [Section 6.4]

Ambiguity A CFG G is ambiguous iff there exists a string x 2 L(G) such that x has more than one distinct derivation trees (or, equivalently, more than one leftmost derivation). Is S  S + S | S * S | (S) | x ambiguous ? Can you give an unambiguous grammar for mathematical expressions ? [Section 6.4]

Ambiguity in Programming Languages Consider the following production rule :  if ( ) | if ( ) else | What are the terminals and nonterminals in the above ? How to fix the problem ? [Section 6.4]

CFL’s vs. Regular Languages Is every context-free language regular ? Is every regular language context-free ? (Recall the definition of regular languages.) [Section 6.5]

Parsing & Normal Forms How to determine if a string is in a language generated by a CFG ? (Algorithms that do this are called parsers.) It helps to have the grammar in a normal form (more restrictions than the original definition of CFG’s). Chomsky normal form : Every rule is of the form A   or A  A 1 A 2 where A,A 1,A 2 2 V and  2 . Example : S  S + S | S * S | (S) | x Convert this grammar into Chomsky normal form. [Section 6.6]

Parsing & Normal Forms How to determine if a string is in a language generated by a CFG ? (Algorithms that do this are called parsers.) It helps to have the grammar in a normal form (more restrictions than the original definition of CFG’s). Chomsky normal form : Every rule is of the form A   or A  A 1 A 2 where A,A 1,A 2 2 V and  2 . Example : S  S + S | S * S | (S) | x Convert this grammar into Chomsky normal form. [Section 6.6]

Parsing & Normal Forms Greibach normal form : Every rule is of the form A   where  2  and  2 V *. Example : S  S + S | S * S | (S) | x Convert this grammar into Greibach normal form. [Section 6.6]

Machines for CFG ? Deterministic or nondeterministic ? Should be able to recognize { a k b k | k ¸ 0 }, { w 2 {a,b}  | w is a palindrome }, but not recognize { ww | w 2 {a,b} * }.