TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Similar documents
Theory of Computation - Module 3

Automata Theory Final Exam Solution 08:10-10:00 am Friday, June 13, 2008

Properties of Context-free Languages. Reading: Chapter 7

Parsing. Context-Free Grammars (CFG) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 26

Finite Automata Theory and Formal Languages TMV027/DIT321 LP4 2018

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Simplification of CFG and Normal Forms. Wen-Guey Tzeng Computer Science Department National Chiao Tung University

Context Free Languages and Grammars

Theory Of Computation UNIT-II

Lecture 12 Simplification of Context-Free Grammars and Normal Forms

Context Free Languages (CFL) Language Recognizer A device that accepts valid strings. The FA are formalized types of language recognizer.

Homework 4 Solutions. 2. Find context-free grammars for the language L = {a n b m c k : k n + m}. (with n 0,

1. (a) Explain the procedure to convert Context Free Grammar to Push Down Automata.

Concordia University Department of Computer Science & Software Engineering

Notes for Comp 497 (Comp 454) Week 10 4/5/05

Notes for Comp 497 (454) Week 10

Automata Theory CS F-08 Context-Free Grammars

60-354, Theory of Computation Fall Asish Mukhopadhyay School of Computer Science University of Windsor

(b) If G=({S}, {a}, {S SS}, S) find the language generated by G. [8+8] 2. Convert the following grammar to Greibach Normal Form G = ({A1, A2, A3},

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Context-Free Grammars (and Languages) Lecture 7

Context-Free Grammar

SYLLABUS. Introduction to Finite Automata, Central Concepts of Automata Theory. CHAPTER - 3 : REGULAR EXPRESSIONS AND LANGUAGES

CS375: Logic and Theory of Computing

Properties of Context-Free Languages

CPS 220 Theory of Computation

CS5371 Theory of Computation. Lecture 7: Automata Theory V (CFG, CFL, CNF)

Before We Start. The Pumping Lemma. Languages. Context Free Languages. Plan for today. Now our picture looks like. Any questions?

Section 1 (closed-book) Total points 30

Lecture 11 Context-Free Languages

EXAMPLE CFG. L = {a 2n : n 1 } L = {a 2n : n 0 } S asa aa. L = {a n b : n 0 } L = {a n b : n 1 } S asb ab S 1S00 S 1S00 100

Non-context-Free Languages. CS215, Lecture 5 c

INSTITUTE OF AERONAUTICAL ENGINEERING

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Properties of context-free Languages

Foundations of Informatics: a Bridging Course

Recitation 4: Converting Grammars to Chomsky Normal Form, Simulation of Context Free Languages with Push-Down Automata, Semirings

This lecture covers Chapter 7 of HMU: Properties of CFLs

Context-Free Grammars and Languages. Reading: Chapter 5

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Einführung in die Computerlinguistik

Einführung in die Computerlinguistik

Context-free Grammars and Languages

Fundamentele Informatica II

Theory of Computation

Properties of Context-Free Languages. Closure Properties Decision Properties

Context Free Grammars

Sheet 1-8 Dr. Mostafa Aref Format By : Mostafa Sayed

FLAC Context-Free Grammars

Properties of Context Free Languages

Chapter 5: Context-Free Languages

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Theory of Computer Science

Fundamentele Informatica 3 Antwoorden op geselecteerde opgaven uit Hoofdstuk 7 en Hoofdstuk 8

An automaton with a finite number of states is called a Finite Automaton (FA) or Finite State Machine (FSM).

6.1 The Pumping Lemma for CFLs 6.2 Intersections and Complements of CFLs

Chapter 1. Formal Definition and View. Lecture Formal Pushdown Automata on the 28th April 2009

COMP-330 Theory of Computation. Fall Prof. Claude Crépeau. Lec. 10 : Context-Free Grammars

MA/CSSE 474 Theory of Computation

Author: Vivek Kulkarni ( )

TAFL 1 (ECS-403) Unit- II. 2.1 Regular Expression: The Operators of Regular Expressions: Building Regular Expressions

Context-Free Grammars and Languages

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Question Bank UNIT I

Chapter 16: Non-Context-Free Languages

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

Context-Free Grammars: Normal Forms

Solutions to Problem Set 3

AC68 FINITE AUTOMATA & FORMULA LANGUAGES DEC 2013

MA/CSSE 474 Theory of Computation

CS5371 Theory of Computation. Lecture 9: Automata Theory VII (Pumping Lemma, Non-CFL)

Einführung in die Computerlinguistik Kontextfreie Grammatiken - Formale Eigenschaften

Computational Models - Lecture 4

Context-Free Grammars. 2IT70 Finite Automata and Process Theory

The View Over The Horizon

Finite Automata and Formal Languages TMV026/DIT321 LP Useful, Useless, Generating and Reachable Symbols

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

Theory of Computation (Classroom Practice Booklet Solutions)

Introduction to Theory of Computing

Computational Models - Lecture 4 1

NPDA, CFG equivalence

Context-Free Languages (Pre Lecture)

VTU QUESTION BANK. Unit 1. Introduction to Finite Automata. 1. Obtain DFAs to accept strings of a s and b s having exactly one a.

UNIT II REGULAR LANGUAGES

Theory of Computation Turing Machine and Pushdown Automata

CMSC 330: Organization of Programming Languages

C1.1 Introduction. Theory of Computer Science. Theory of Computer Science. C1.1 Introduction. C1.2 Alphabets and Formal Languages. C1.

Context Free Languages. Automata Theory and Formal Grammars: Lecture 6. Languages That Are Not Regular. Non-Regular Languages

CS 373: Theory of Computation. Fall 2010

Languages. Languages. An Example Grammar. Grammars. Suppose we have an alphabet V. Then we can write:

The Pumping Lemma for Context Free Grammars

Computational Models - Lecture 4 1

Pushdown Automata. Reading: Chapter 6

Chap. 7 Properties of Context-free Languages

CS20a: summary (Oct 24, 2002)

Pushdown Automata. Notes on Automata and Theory of Computation. Chia-Ping Chen

CFG Simplification. (simplify) 1. Eliminate useless symbols 2. Eliminate -productions 3. Eliminate unit productions

Functions on languages:

CS A Term 2009: Foundations of Computer Science. Homework 2. By Li Feng, Shweta Srivastava, and Carolina Ruiz.

Transcription:

TAFL 1 (ECS-403) Unit- III 3.1 Definition of CFG (Context Free Grammar) and problems 3.2 Derivation 3.3 Ambiguity in Grammar 3.3.1 Inherent Ambiguity 3.3.2 Ambiguous to Unambiguous CFG 3.4 Simplification of CFGs 3.4.1 Removal of useless symbols 3.4.2 Elimination of Ԑ production 3.4.3 Removal of unit production 3.5 Normal Forms 3.5.1 Chomsky s Normal Forms (CNF) 3.5.2 Greibach Normal Form (GNF) 3.6 Closure Properties of CFL 3.7 Decision Properties of CGL 3.8 Undecidable Problems 3.9 Applications of CFG 3.10 Pumping Lemma for CFL 3.11 CFG to finite Automata

TAFL 2 (ECS-403) 3.1 Definition of CFG (Context Free Grammar): The CFG can be formally defined by G = {V,T,P,S} where V = set of non terminals or variables T = set of terminals P = set of production S = start symbol Problems based on CFG 1. Write CFG for the following languages: a) Having any number of a s over the set {a}. S as S Ԑ b) Having regular expression (0+1)*. S 0S 1S S Ԑ c) Containing strings of at least two a s. S Aa Aa A A aa ba Ԑ d) L = wcw T where w Ԑ (a,b)* S asa S bsb S C e) Which has all the strings which are all palindromes over {a,b}. S asa S bsb S a b Ԑ

TAFL 3 (ECS-403) f) Which consists of all the strings having at least one occurrence of 000. S ATA A 0A 1A Ԑ T 000 g) There are no consecutive b s, the string may or may not have consecutive a s. S as ba a b Ԑ A as a Ԑ h) At least one occurrence of double a. S BAB A aa B ab bb Ԑ i) All the string of different first and last symbols over {0,1}. S 0A1 1A0 A 0A 1A Ԑ j) L = a n b 2n where n 1. S asbb abb k) L = a x b y where x y S asb A B A aa a B bb b l) For the regular expression (110+11)*(10)* S AB A 110A 11A Ԑ B 10B Ԑ m) For generating the integers. S GI G + - I DI D D 0 1 2. 9

TAFL 4 (ECS-403) n) L = {0 i 1 j 2 k j > i+k}. S ABC A 0A1 Ԑ B 1B 1 C 1C2 Ԑ o) L = {0 i 1 j 2 k i = j}. S AB A 0A1 Ԑ B 2B Ԑ p) L = {0 i 1 j 2 k j k}. S AB A 0A Ԑ B 1B2 C C 2C Ԑ

TAFL 5 (ECS-403) 3.2 Derivation: a. Left Most Derivation b. Right Most Derivation Example: Derive the string 1000111 for leftmost and rightmost derivation. S T00T T 0T 1T Ԑ Left Most Derivation S T00T S T00T 1T00T T 1T 10T00T T 0T 10Ԑ00T T Ԑ 1000T 10001T T 1T 100011T T 1T 1000111T T 1T 1000111Ԑ T Ԑ 1000111 Right Most Derivation S T00T S T00T T001T T 1T T0011T T 1T T00111T T 1T T00111Ԑ T Ԑ T00111 1T00111 T 1T 10T00111 T 0T 10Ԑ00111 T Ԑ 1000111 3.2.1 Derivation Tree (Parse Tree): Derivation Tree is a graphical representation for the derivation of the given production rules for a given CFG. Following are properties of any derivation tree: a. The root node is always a node starting symbol. b. The derivation is read from left to right. c. The leaf nodes are always terminal nodes. d. The interior nodes are always the non terminal nodes.

TAFL 6 (ECS-403) Example: Construct the derivation tree for the string aabbabba from the CFG given below: S ab ba A a as baa B b bs abb Solution: 3.3 Ambiguity in Grammar: The grammar can be derived in either leftmost or rightmost derivation but if there exists more than one left parse tree or more than one right parse tree than the grammar is said to be ambiguous grammar.

TAFL 7 (ECS-403) Example: Example: Remove ambiguity from the following example: E E+E E*E id Solution: E E + T E T T T * F T F F id

TAFL 8 (ECS-403) 3.3.1 Inherent Ambiguity: A context free grammar is called inherently ambiguous if all the productions rules in the grammar are ambiguous. Example: S XYZ aaybb X aay aa Y baz ba Z bzb bb 3.3.2 Ambiguous to Unambiguous CFG: 3.4 Simplification of CFGs: There are three steps for simplification of CFG (reduced grammar): a) Removal of useless symbols. b) Elimination of Ԑ production. c) Removal of unit production. 3.4.1 Removal of useless symbols: A symbol P is useful if there exists some derivation in the following form: * S αpβ * αpβ w Then p is said to be useful symbol.

TAFL 9 (ECS-403) Example: S aa a Bb cc A ab B a Aa C ccd D ddd Solution: S aa a Bb A ab B a Aa 3.4.2 Elimination of Ԑ production Example: S XYX X 0X Ԑ Y 1Y Ԑ Solution: S XYX XY YX Y X X 0X 0 Y 1Y 1 3.4.3 Removal of unit production. Example: S 0A 1B C A 0S 00 B 1 A C 01 Solution S 0A 1B 01 A 0S 00 B 1 0S 00 C 01 Exercise: Simplify the following CFGs S A 0C1 A B 01 10 C Ԑ CD Solution S 01 10

TAFL 10 (ECS-403) 3.5 Normal Forms: 3.5.1 Chomsky s Normal Forms (CNF): The CNF can be defined in the following form: Non terminal Non Terminal. Non Terminal Non terminal Terminal Example: Convert the following CFG into CNF: 1. S aaaas S aaaa 2. S asa bsb a b Solution: S C a C 1 C 1 C a C 2 C 2 C a C 3 C 3 C a C 4 S C 5 C 5 C 5 C a C a Ca a Solution: S C a C 1 C 1 SC a Ca a S C b C 2 C 2 SC b C b b S a S b 3.5.2 Greibach Normal Form (GNF): The GNF can be defined in the following form: Non terminal One Terminal. Any number of non-terminals Non terminal One terminal

TAFL 11 (ECS-403) To convert given CFG into GNF we can use two lemmas based on which it is easy to convert given CFG to GNF: A. Lemma 1: G = (V, T, P, S) be a given CFG and if there is a production A Ba and B β 1 β 2. β n Then we can convert A rule to GNF as A β 1 a β 2 a. β n a B. Lemma 2: G = (V, T, P, S) be a given CFG and if there is a production A Aa 1 Aa 2. Aa n β 1 β 2. β n Such that β i do not start with A then equivalent grammar in GNF can be: A β 1 β 2. β n A β 1 Z β 2 Z. β n Z Z a 1 a 2. a n Z a 1 Z a 2 Z. a n Z Example: Convert the following grammar to GNF: Example 1. S absb S aa Solution: S absb S aa B b A a Example 2. S ABA A aa Ԑ B bb Ԑ

TAFL 12 (ECS-403) Remove Ԑ in the CFG: S ABA AB BA AA AA A B A aa a B bb b Remove unit production: S ABA AB BA AA AA aa a bb b A aa a B bb b In GNF we can write S aaba aab aba ab aaa aa a bba ba bb b A aa a B bb b Example 3. S AA 0 A SS 1 Step 1: (for A) A SS 1 Put the valve of S in left most A AAS 0S 1 From lemma 2: a 1 = AS, β 1 = 0S, β 2 = 1

TAFL 13 (ECS-403) A 0S 1 A 0SZ 1Z Z AS Z ASZ Step 2: S AA 0 Put the above value of A in left most: S 0SA 1A 0SZA 1ZA 0 Put the value of A for Z: Z 0SS 1S 0SZS 1ZS Z 0SSZ 1SZ 0SZSZ 1ZSZ The final solution is S 0SA 1A 0SZA 1ZA 0 A 0S 1 0SZ 1Z Z 0SS 1S 0SZS 1ZS 0SSZ 1SZ 0SZSZ 1ZSZ Exercise: convert the following CFG into GNF S AB A BS b B SA a

TAFL 14 (ECS-403) 3.6 Closure Properties of CFL: a) The context free languages are closed under union. b) The context free languages are closed concatenation. c) The context free languages are closed under kleen closure. d) The context free languages are not closed under intersection. e) The context free languages are not closed under complement. 3.7 Decision Properties of CGL: a) Emptiness: The given context free grammar cannot generate any string at all. b) Finiteness: The given context free grammar generates a finite language. c) Membership: whether given string belongs to given grammar. 3.8 Undecidable Problems: There are no algorithms to answer these questions. Hence these problems are known as Undecidable Problems: a) Whether or not two different context free languages define the same language? b) Whether given CFL is ambiguous or not? c) Whether complement of given CFL is context free language? d) Whether the intersection of two context free languages is context free? 3.9 Applications of CFG: When any high level program like C or PASCAL is compiled, the compiler checks the syntax of every programming statement by constructing syntax tree, And for building the syntax tree, it is necessary to write context free grammar for each statement in the program.

TAFL 15 (ECS-403) There are various Parsing Techniques: Types of Parser Top Down Parser Bottom Up Parser Backtracking Predictive Parser Shift Reduce LR Parser Parser SLR Parser LALR Parser LR Parser 3.10 Pumping Lemma for CFL: Let L be any context free language, then there is a constant n, which depends only upon L, such that there exist a string w Ԑ L and w n where w = pqrst such that a) qs 1 b) prs n and c) For all i 0 pq i rs i t is in L. Example: Proof whether the given language L = {SS T S Ԑ (a,b)*} is context free or not?

TAFL 16 (ECS-403) 3.11 CFG to Finite Automata: S 0A 1B 0 1 A 0S 1B 1 B 0A 1S