Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”

Slides:



Advertisements
Similar presentations
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.
Advertisements

1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Context-Free Grammars
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.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
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 Grammars Formalism Derivations Backus-Naur Form Left- and Rightmost Derivations.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Courtesy Costas Busch - RPI1 Context-Free Languages.
Normal forms for Context-Free Grammars
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
COP4020 Programming Languages
Fall 2006Costas Busch - RPI1 PDAs Accept Context-Free Languages.
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.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
EECS 6083 Intro to Parsing Context Free Grammars
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
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.
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of CFL was to formalize.
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 CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Chapter 5 Context-Free Grammars
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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.
Context Free Grammars. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
CMSC 330: Organization of Programming Languages Context-Free Grammars.
作者 : 陳鍾誠 單位 : 金門技術學院資管系 URL : 日期 : 2016/6/4 程式語言的語法 Grammar.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
CS 3240 – Chapter 5. LanguageMachineGrammar RegularFinite AutomatonRegular Expression, Regular Grammar Context-FreePushdown AutomatonContext-Free Grammar.
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.
11 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 7 School of Innovation, Design and Engineering Mälardalen University 2012.
Chapter 3 Context-Free Grammars Dr. Frank Lee. 3.1 CFG Definition The next phase of compilation after lexical analysis is syntax analysis. This phase.
Syntax Analysis – Part I EECS 483 – Lecture 4 University of Michigan Monday, September 17, 2006.
Context-Free Languages
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
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.
Lecture 6: Context-Free Languages
Compiler Construction Lecture Five: Parsing - Part Two CSC 2103: Compiler Construction Lecture Five: Parsing - Part Two Joyce Nakatumba-Nabende 1.
CS 461 – Sept. 23 Context-free grammars Derivations Ambiguity Proving correctness.
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
5. Context-Free Grammars and Languages
Context-Free Languages & Grammars (CFLs & CFGs)
Context-Free Grammars: an overview
Context-Free Languages
CS 404 Introduction to Compiler Design
G. Pullaiah College of Engineering and Technology
Context free grammar.
PARSE TREES.
Context-Free Languages
Context-Free Grammars
CHAPTER 2 Context-Free Languages
Context-Free Grammars 1
Finite Automata and Formal Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Theory of Computation Lecture #
Presentation transcript:

Context Free Grammars 1

Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger” than that of regular languages –One of these classes are called “Context Free” languages Described by Context-Free Grammars (CFG) –Why named context-free? –Property that we can substitute strings for variables regardless of context (implies context sensitive languages exist) CFG’s are useful in many applications –Describing syntax of programming languages –Parsing –Structure of documents, e.g.XML Analogy of the day: –DFA:Regular Expression as Pushdown Automata : CFG 2

3 Regular Languages Context-Free Languages

4 Example: CFG for { 0 n 1 n | n > 1} Productions: S -> 01 S -> 0S1 Basis: 01 is in the language. Induction: if w is in the language, then so is 0w1.

CFG Example Language of palindromes –We can easily show using the pumping lemma that the language L = { w | w = w R } is not regular. –However, we can describe this language by the following context-free grammar over the alphabet {0,1}: P   P  0 P  1 P  0P0 P  1P1 Inductive definition More compactly: P   | 0 | 1 | 0P0 | 1P1 5

6 Grammars Grammars express languages Example: the English language grammar

Fall 2006Costas Busch - RPI7

Fall 2006Costas Busch - RPI8 Derivation of string “the dog walks”:

9 Derivation of string “a cat runs”:

10 Language of the grammar: L = { “a cat runs”, “a cat sleeps”, “the cat runs”, “the cat sleeps”, “a dog runs”, “a dog sleeps”, “the dog runs”, “the dog sleeps” }

11 Variables Sequence of Terminals (symbols) Productions Sequence of Variables

12 CFG Formalism Terminals = symbols of the alphabet of the language being defined. Variables = nonterminals = a finite set of other symbols, each of which represents a language. Start symbol = the variable whose language is the one being defined.

13 Productions A production has the form variable (head) -> string of variables and terminals (body). Convention: –A, B, C,… and also S are variables. –a, b, c,… are terminals. –…, X, Y, Z are either terminals or variables. –…, w, x, y, z are strings of terminals only. – , , ,… are strings of terminals and/or variables.

CFG Notation A CFG G may then be represented by these four components, denoted G=(V,T,R,S) –V is the set of variables –T is the set of terminals –R is the set of production rules –S is the start symbol. 14

15 Derivations – Intuition We derive strings in the language of a CFG by starting with the start symbol, and repeatedly replacing some variable A by the body of one of its productions. –That is, the “productions for A” are those that have head A.

16 Derivations – Formalism We say  A  =>  if A ->  is a production. Example: S -> 01; S -> 0S1. S => 0S1 => 00S11 =>

17 Iterated Derivation =>* means “zero or more derivation steps.” Basis:  =>*  for any string . Induction: if  =>*  and  => , then  =>* .

18 Example: Iterated Derivation S -> 01; S -> 0S1. S => 0S1 => 00S11 => Thus S =>* S; S =>* 0S1; S =>* 00S11; S =>*

Sample CFG 1.E  I// Expression is an identifier 2.E  E+E// Add two expressions 3.E  E*E// Multiply two expressions 4.E  (E)// Add parenthesis 5.I  L// Identifier is a Letter 6.I  ID// Identifier + Digit 7.I  IL// Identifier + Letter 8.D  0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9// Digits 9.L  a | b | c | … A | B | … Z// Letters Note Identifiers are regular; could describe as (letter)(letter + digit)* 19

Derivation Expand start symbol first and work way down in such a way that it matches the input string For example, given a*(a+b1) we can derive this by: –E  E*E  I*E  L*E  a*E  a*(E)  a*(E+E)  a*(I+E)  a*(L+E)  a*(a+E)  a*(a+I)  a*(a+ID)  a*(a+LD)  a*(a+bD)  a*(a+b1) Note that at each step of the productions we could have chosen any one of the variables to replace with a more specific rule. 20

Multiple Derivation We saw an example of  in deriving a*(a+b1) We could have used  * to condense the derivation. –E.g. we could just go straight to E  * E*(E+E) or even straight to the final step E  * a*(a+b1) Going straight to the end is not recommended on a homework or exam problem if you are supposed to show the derivation 21

22 Leftmost and Rightmost Derivations Derivations allow us to replace any of the variables in a string. –Leads to many different derivations of the same string. By forcing the leftmost variable (or alternatively, the rightmost variable) to be replaced, we avoid these “distinctions without a difference.”

Leftmost Derivation In the previous example we used a derivation called a leftmost derivation. We can specifically denote a leftmost derivation using the subscript “lm”, as in:  lm or  * lm A leftmost derivation is simply one in which we replace the leftmost variable in a production body by one of its production bodies first, and then work our way from left to right. 23

24 Example: Leftmost Derivations Balanced-parentheses grammar: S -> SS | (S) | () S => lm SS => lm (S)S => lm (())S => lm (())() Thus, S =>* lm (())() S => SS => S() => (S)() => (())() is a derivation, but not a leftmost derivation.

Rightmost Derivation Not surprisingly, we also have a rightmost derivation which we can specifically denote via:  rm or  * rm A rightmost derivation is one in which we replace the rightmost variable by one of its production bodies first, and then work our way from right to left. 25

Rightmost Derivation Example a*(a+b1) was already shown previously using a leftmost derivation. We can also come up with a rightmost derivation, but we must make replacements in different order: –E  rm E*E  rm E * (E)  rm E*(E+E)  rm E*(E+I)  rm E*(E+ID)  rm E*(E+I1)  rm E*(E+L1)  rm E*(E+b1)  rm E*(I+b1)  rm E*(L+b1)  rm E*(a+b1)  rm I*(a+b1)  rm L*(a+b1)  rm a*(a+b1) 26

27 Example: Rightmost Derivations Balanced-parentheses grammar: S -> SS | (S) | () S => rm SS => rm S() => rm (S)() => rm (())() Thus, S =>* rm (())() S => SS => SSS => S()S => ()()S => ()()() is neither a rightmost nor a leftmost derivation.

Left or Right? Does it matter which method you use? Answer: No Any derivation has an equivalent leftmost and rightmost derivation. That is, A  * . iff A  * lm  and A  * rm . 28

29 Sentential Forms Any string of variables and/or terminals derived from the start symbol is called a sentential form. Formally,  is a sentential form iff S =>* .

30 Language of a Grammar If G is a CFG, then L(G), the language of G, is {w | S =>* w}. Example: G has productions S -> ε and S -> 0S1. L(G) = {0 n 1 n | n > 0}.

31 Context-Free Languages A language that is defined by some CFG is called a context-free language. There are CFL’s that are not regular languages, such as the example just given. But not all languages are CFL’s. Intuitively: CFL’s can count two things, not three.

CFG Exercise {a i b j c k | i ≠ j or j ≠ k} 32