COSC 3340: Introduction to Theory of Computation

Slides:



Advertisements
Similar presentations
Lecture 23UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 23.
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.
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).
Lecture 3UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 3.
Lecture 15UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 15.
CS5371 Theory of Computation
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
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.
Languages and Grammars MSU CSE 260. Outline Introduction: E xample Phrase-Structure Grammars: Terminology, Definition, Derivation, Language of a Grammar,
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.
Introduction Syntax: form of a sentence (is it valid) Semantics: meaning of a sentence Valid: the frog writes neatly Invalid: swims quickly mathematics.
Lecture 2UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 2.
1 Section 3.3 Grammars A grammar is a finite set of rules, called productions, that are used to describe the strings of a language. Notational Example.
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.
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:
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.
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.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Context-free.
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.
Lecture 11UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 11.
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.
Lecture 14UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 14.
Lecture 15 Ambiguous Grammars Topics: Context Free Grammars Language generated by a grammar Proofs with L(G) Ambiguous grammars October 20, 2008 CSCE 355.
Formal grammars A formal grammar is a system for defining the syntax of a language by specifying sequences of symbols or sentences that are considered.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
Grammars.
PROGRAMMING LANGUAGES
CONTEXT-FREE LANGUAGES
Context-Free Grammars: an overview
Formal Language & Automata Theory
CS510 Compiler Lecture 4.
Context free grammar.
CSE 105 theory of computation
PARSE TREES.
COSC 3340: Introduction to Theory of Computation
Context-Free Languages
COSC 3340: Introduction to Theory of Computation
COSC 3340: Introduction to Theory of Computation
Chapter 2: A Simple One Pass Compiler
CS21 Decidability and Tractability
CHAPTER 2 Context-Free Languages
CSE 311: Foundations of Computing
CSC 4181Compiler Construction Context-Free Grammars
COSC 3340: Introduction to Theory of Computation
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
COSC 3340: Introduction to Theory of Computation
COSC 3340: Introduction to Theory of Computation
CSC 4181 Compiler Construction Context-Free Grammars
Theory of Computation Lecture #
CSE 105 theory of computation
COSC 3340: Introduction to Theory of Computation
Formal Languages Context free languages provide a convenient notation for recursive description of languages. The original goal of formalizing the structure.
Context-Free Grammars
Context Free Grammars-II
Programming Languages 2nd edition Tucker and Noonan
Answer Questions about Exam2 problems
COSC 3340: Introduction to Theory of Computation
COSC 3340: Introduction to Theory of Computation
COSC 3340: Introduction to Theory of Computation
CSE 105 theory of computation
Presentation transcript:

COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9 Lecture 9 UofH - COSC 3340 - Dr. Verma

Context Free Languages Strictly bigger class than regular languages {anbn | n >= 0} context-free languages  regular languages   {wwR |w in {a,b}*} {arithmetic expressions} Lecture 9 UofH - COSC 3340 - Dr. Verma

A simple grammar for some sentences <Sentence>  <noun> <verb> <object> <noun>  Alice | John <verb>  eats | eat | ate <object>  apple | orange | mango The goal is to generate sentences in English over the English alphabet. Example: <Sentence>  <noun><verb><object>  Alice <verb><object>  Alice eats<object>  Alice eats apple Lecture 9 UofH - COSC 3340 - Dr. Verma

Context-free grammars (CFGs) Informally, a CFG is a finite set of rules. Each rule is of the form: <nonterminal symbol>  string over terminals and nonterminals. Terminals:- symbols that the desired strings should contain. Example: {a...z, ' ',...} Nonterminals:- symbols to which rules can be applied. Example: {<noun>, <verb>, ...} A special nonterminal is called start symbol. Example: <Sentence> Lecture 9 UofH - COSC 3340 - Dr. Verma

A grammar for arithmetic expressions E  E + E | E * E | (E) | x | y Start symbol - E. Terminals? Ans: {x, y, *, +, (, ), ' '} Nonterminals? Ans: {E} A derivation: E  E + E  E + E * E  x + E * E  x + x * E  x + x * y Lecture 9 UofH - COSC 3340 - Dr. Verma

Another grammar for arithmetic expr’s E  E + T | T T  T * F | F F  (E) | x | y A derivation for x + x * y ? E E + T T + T F + T x + T  x + T * F x + F * F x + x * F x + x * y Why two different grammars for arithmetic expressions? Lecture 9 UofH - COSC 3340 - Dr. Verma

Context Free Grammar Definition A CFG G = (V, T, P, S) where V  T = , V -- A finite set of symbols called nonterminals T -- A finite set of symbols called terminals P is a finite subset of V X (V  T)* called productions or rules We write A  w whenever (A, w)  P. S  V -- start symbol Lecture 9 UofH - COSC 3340 - Dr. Verma

Derivations and L(G) One step derivation: 0 or more steps derivation: u  v if u = xAy, v = xwy and A  w in P 0 or more steps derivation: u * v if u = u0  u1  ....  un = v (n  0) L(G) = { w in T* | S * w }. A language L is context-free if there is a CFG G with L(G) = L Lecture 9 UofH - COSC 3340 - Dr. Verma

Example: S  aSb |  Derivation: S  aSb  aaSbb  aabb. L(G) = ? Ans: {anbn | n  0 } Lecture 9 UofH - COSC 3340 - Dr. Verma

Parse trees All derivations can be shown in the form of trees. Order of rule application is lost. S a S b a S b  Lecture 9 UofH - COSC 3340 - Dr. Verma

Parse trees [contd.] In general, if we apply rule A  w0w1...wn, then we add nodes for wi as children of node labeled A A w0 w1 w2 wn    Lecture 9 UofH - COSC 3340 - Dr. Verma

E  E + E  E + E * E  x + E * E  x + x * E  x + x * y Lecture 9 UofH - COSC 3340 - Dr. Verma

E E + T T + T F + T x + T x + T * F x + F * F x + x * F x + x * y Lecture 9 UofH - COSC 3340 - Dr. Verma

Leftmost and Rightmost Derivations Derivation is leftmost if the nonterminal replaced in every step is the leftmost nonterminal. Consider E  E + E  E + x. Is it leftmost derivation? Derivation is rightmost if the nonterminal replaced in every step is the rightmost nonterminal. Consider E  E + E  x + E. Is it rightmost derivation? Lecture 9 UofH - COSC 3340 - Dr. Verma

Ambiguity A CFG is ambiguous if there is a string with at least two leftmost derivations Example: E  E + E | E * E | (E) | x | y is ambiguous A CFL is inherently ambiguous if every CFG that generates it is ambiguous. {anbncm | n, m  0}  {ambncn | n, m  0} Lecture 9 UofH - COSC 3340 - Dr. Verma