Context-Free Grammars

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.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
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
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
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.
Specifying Languages CS 480/680 – Comparative Languages.
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 PROGRAMS CSci 4011.
Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.
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.
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Pushdown Automata CS 130: Theory of Computation HMU textbook, Chap 6.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Grammars CPSC 5135.
Context-free Languages
CS 3240: Languages and Computation Context-Free Languages.
CONTEXT FREE GRAMMAR presented by Mahender reddy.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein.
Finite Automata – Definition and Examples Lecture 6 Section 1.1 Mon, Sep 3, 2007.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
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.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Theory of Languages and Automata By: Mojtaba Khezrian.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Context-Free Grammars: an overview
Formal Language & Automata Theory
Context-free grammars, derivation trees, and ambiguity
Context free grammar.
CSE 105 theory of computation
CS314 – Section 5 Recitation 3
Formal Language Theory
PARSE TREES.
Context Free Languages
CSC 4170 Theory of Computation Nondeterminism Section 1.2.
Context-free Languages
Chapter 2: A Simple One Pass Compiler
CS21 Decidability and Tractability
CHAPTER 2 Context-Free Languages
Context-Free Grammars
Context-Free Languages
فصل دوم Context-Free Languages
CS21 Decidability and Tractability
Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 2 Context-Free Language - 01
Theory of Computation Lecture #
Teori Bahasa dan Automata Lecture 9: Contex-Free Grammars
Decidability and Tractability
CSC 4170 Theory of Computation Nondeterminism Section 1.2.
CSE 105 theory of computation
COSC 3340: Introduction to Theory of Computation
Context Free Grammars-II
Faculty of Computer Science and Information System
CSE 105 theory of computation
Presentation transcript:

Context-Free Grammars CSC 4170 Theory of Computation Context-Free Grammars Section 2.1

A  B A   B  0A1 Terminals: 0,1 Variables: A,B Productions: What is a CFG 2.1.a A  B A   B  0A1 Terminals: 0,1 Variables: A,B Productions: Start variable: A Derivation: A  B  0A1  0B1  00A11  0011 A B Parse tree: 0 1 A B 0 1 A 

Our grammar simplified 2.1.b A  0A1 A   A  0A1  00A11  000A111  0000A1111  00001111 What language does this grammar produce?

N  men | women | children V  like | hate | respect A more complex CFG 2.1.c1 S  N’_V_N’ N’  N | N_who_V_N’ N  men | women | children V  like | hate | respect SN’_V_N’N’_like_N’N_like_N’N_like_Nwomen_like_Nwomen_like_children S N’ _ V _ N’ N like N women children

N  men | women | children V  like | hate | respect A more complex CFG 2.1.c2 S  N’_V_N’ N’  N | N_who_V_N’ N  men | women | children V  like | hate | respect S N’ _ V _ N’ N respect N _ who _ V _ N’ children men hate N women

A context-free grammar is a 4-tuple (V,,R,S), where Formal definitions 2.1.d 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 pair of a variable and a string of variables and terminals; 4. S is an element of V called the start variable. If u,v, and w are strings of variables and terminals and A w is a rule, we say that uAv yields uwv, written uAv  uwv. x * y means that x=y, or x y, or there are z1,…,zn such that x z1 … zn y. The language produced (defined, described) by the grammar is {w | S * w and w is a string of (only) terminals}. A context-free language is a language produced by some CFG.

Ambiguity: An informal example the girl touches the boy with the flower Does this mean the girl touches (the boy with the flower) or the girl touches the boy with the flower (the girl touches the boy) with the flower ? with the flower the girl touches the boy

An example of an ambiguous CFG <EXPR>  <EXPR> + <EXPR> | <EXPR>  <EXPR> | a a + a  a <EXPR> <EXPR> <EXPR> + <EXPR> <EXPR>  <EXPR> a <EXPR>  <EXPR> <EXPR> + <EXPR> a a a a a A grammar is ambiguous iff it has two different parse trees for the same string

An equivalent but unambiguous grammar <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM>  a | a <EXPR> <EXPR> + <TERM> <TERM> <TERM>  a a a a + a  a

A more complex unambiguous grammar 2.1.h <EXPR>  <EXPR> + <TERM> | <TERM> <TERM>  <TERM>  <FACTOR> | <FACTOR> <FACTOR>  (<EXPR>) | a <EXPR> <TERM> <EXPR> <TERM>  <FACTOR> <EXPR> + <TERM> <FACTOR> a <TERM> <TERM>  <FACTOR> ( <EXPR> ) <FACTOR> <FACTOR> a <EXPR> + <TERM> <TERM> <FACTOR> a a <FACTOR> a a + a  a a (a + a)  a

Designing context-free grammars Design a CFG that produces all regular expressions over the alphabet {0,1}: <RE>  Design a CFG G that produces the union of the languages produced by two given CFGs G1 and G2. G1: A1  w1 … An  wn G2: B1  u1 … Bm  um

Converting a DFA into a CFG 2.1.j Variables: The states of the DFA 1 Q1 Q2 1 Start variable: The start state of the DFA Productions: 1. Qi  a Qj, whenever there is an a-arrow from Qi to Qj; 2. Qi  , whenever Qi is an accept state.

011001 Testing in work Q1  0 Q1 Q1  1 Q2 Q2  0 Q2 Q2  1 Q1 Q2   2.1.j Q1  0 Q1 Q1  1 Q2 Q2  0 Q2 Q2  1 Q1 Q2   1 Q1 Q2 1 Q1 0Q1 01Q2 011Q1 0110Q1 01100Q1 011001Q2 011001 011001