Context-Free Languages

Slides:



Advertisements
Similar presentations
Context-Free Languages
Advertisements

1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Introduction to Computability Theory Lecture5: Context Free Languages Prof. Amos Israeli.
CS5371 Theory of Computation
Applied Computer Science II Chapter 2 : Context-free languages Prof. Dr. Luc De Raedt Institut für Informatik Albert-Ludwigs Universität Freiburg Germany.
1 Grammars. 2 Grammars express languages Example: the English language.
Costas Busch - RPI1 Context-Free Languages. Costas Busch - RPI2 Regular Languages.
January 14, 2015CS21 Lecture 51 CS21 Decidability and Tractability Lecture 5 January 14, 2015.
Courtesy Costas Busch - RPI1 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2004COMP 3351 Context-Free Languages. Fall 2004COMP 3352 Regular Languages.
Prof. Busch - LSU1 Context-Free Languages. Prof. Busch - LSU2 Regular Languages Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
Fall 2005Costas Busch - RPI1 Context-Free Languages.
Context-Free Languages Hinrich Schütze CIS, LMU, Slides based on RPI CSCI 2400 Thanks to Costas Busch.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 7 Mälardalen University 2010.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2005.
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 Mälardalen University 2010.
Grammars CPSC 5135.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
1 Context-Free Languages. 2 Regular Languages 3 Context-Free Languages.
CS 3240: Languages and Computation Context-Free Languages.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
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.
Context-Free Languages. Regular Languages 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.
Grammar Set of variables Set of terminal symbols Start variable Set of Production rules.
Theory of Languages and Automata By: Mojtaba Khezrian.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
Introduction to Parsing
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
5. Context-Free Grammars and Languages
Chapter 3 – Describing Syntax
Describing Syntax and Semantics
Context-Free Languages & Grammars (CFLs & CFGs)
Deterministic Finite Automata And Regular Languages.
Context-Free Grammars: an overview
Formal Language & Automata Theory
G. Pullaiah College of Engineering and Technology
CS510 Compiler Lecture 4.
Fall Compiler Principles Context-free Grammars Refresher
Models of Computation.
Syntax Specification and Analysis
More NP-complete Problems
Reductions.
CSE 105 theory of computation
PDAs Accept Context-Free Languages
Syntax versus Semantics
CSE322 LEFT & RIGHT LINEAR REGULAR GRAMMAR
Context-free Languages
5. Context-Free Grammars and Languages
CS21 Decidability and Tractability
CHAPTER 2 Context-Free Languages
فصل دوم Context-Free Languages
Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 2 Context-Free Language - 01
Theory of Computation Lecture #
Fall Compiler Principles Context-free Grammars Refresher
… NPDAs continued.
CSE 105 theory of computation
COMPILER CONSTRUCTION
Faculty of Computer Science and Information System
CSE 105 theory of computation
Presentation transcript:

Context-Free Languages

Context-Free Languages Regular Languages

Context-Free Languages Grammars Pushdown Automata stack automaton

Context-Free Grammars

Grammars Grammars express languages Example: the English language grammar

Derivation of string “the dog walks”:

Derivation of string “a cat runs”:

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” }

Productions Sequence of Terminals (symbols) Variables Sequence of Variables

Another Example Grammar: Sequence of terminals and variables Variable The right side may be

Grammar: Derivation of string :

Grammar: Derivation of string :

Grammar: Other derivations:

Grammar: Language of the grammar:

A Convenient Notation We write: Instead of: for zero or more derivation steps

In general we write: If: in zero or more derivation steps Trivially:

Example Grammar Possible Derivations

Another convenient notation:

Formal Definitions Grammar: Set of variables Set of terminal symbols Start variable Set of productions

Context-Free Grammar: All productions in are of the form Variable String of variables and terminals

Example of Context-Free Grammar productions start variable variables terminals

Language of a Grammar: For a grammar with start variable String of terminals or

Example: context-free grammar : Since, there is derivation for any

Context-Free Language: A language is context-free if there is a context-free grammar with

Example: is a context-free language since context-free grammar : generates

Another Example Context-free grammar : Example derivations: Palindromes of even length

() ((( ))) (( )) Another Example Context-free grammar : Example derivations: Describes matched parentheses: () ((( ))) (( ))

Derivation Order and Derivation Trees

Derivation Order Consider the following example grammar with 5 productions:

Leftmost derivation order of string : At each step, we substitute the leftmost variable

Rightmost derivation order of string : At each step, we substitute the rightmost variable

Leftmost derivation of : Rightmost derivation of :

Derivation Trees Consider the same example grammar: And a derivation of :

yield

yield

yield

yield

Derivation Tree (parse tree) yield

Sometimes, derivation order doesn’t matter Leftmost derivation: Rightmost derivation: Give same derivation tree

Ambiguity

Grammar for mathematical expressions Example strings: Denotes any number

A leftmost derivation for

Another leftmost derivation for

Two derivation trees for

take

Good Tree Bad Tree Compute expression result using the tree

Two different derivation trees may cause problems in applications which use the derivation trees: Evaluating expressions In general, in compilers for programming languages

Ambiguous Grammar: A context-free grammar is ambiguous if there is a string which has: two different derivation trees or two leftmost derivations (Two different derivation trees give two different leftmost derivations and vice-versa)

Example: this grammar is ambiguous since string has two derivation trees

this grammar is ambiguous also because string has two leftmost derivations

Another ambiguous grammar: IF_STMT if EXPR then STMT if EXPR then STMT else STMT Variables Terminals Very common piece of grammar in programming languages

If expr1 then if expr2 then stmt1 else stmt2 IF_STMT if expr1 then STMT if expr2 then stmt1 else stmt2 Two derivation trees IF_STMT if expr1 then STMT else stmt2 if expr2 then stmt1

In general, ambiguity is bad and we want to remove it Sometimes it is possible to find a non-ambiguous grammar for a language But, in general we cannot do so

A successful example: Equivalent Ambiguous Grammar Non-Ambiguous Grammar generates the same language

Unique derivation tree for

An un-successful example: is inherently ambiguous: every grammar that generates this language is ambiguous

Example (ambiguous) grammar for :

The string has always two different derivation trees (for any grammar) For example

This Summary is an Online Content from this Book: Michael Sipser, Introduction to the Theory of Computation, 2ndEdition It is edited for Computation Theory Course 6803415-3 by: T.Mariah Sami Khayat Teacher Assistant @ Adam University College For Contacting: mskhayat@uqu.edu.sa Kingdom of Saudi Arabia Ministry of Education Umm AlQura University Adam University College Computer Science Department المملكة العربية السعودية وزارة التعليم جامعة أم القرى الكلية الجامعية أضم قسم الحاسب الآلي