Lecture 21: Languages and Grammars. Natural Language vs. Formal Language.

Slides:



Advertisements
Similar presentations
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Advertisements

Closure Properties of CFL's
CFGs and PDAs Sipser 2 (pages ). Long long ago…
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
CFGs and PDAs Sipser 2 (pages ). Last time…
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
CS5371 Theory of Computation
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
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.
Normal forms for Context-Free Grammars
How to Convert a Context-Free Grammar to Greibach Normal Form
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.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
Grammars This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy of this license, visit.
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.
Pushdown Automata (PDA) Intro
1 Homework #7 (Models of Computation, Spring, 2001) Due: Section 1; April 16 (Monday) Section 2; April 17 (Tuesday) 2. Covert the following context-free.
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 Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Pushdown Automata (PDAs)
A sentence (S) is composed of a noun phrase (NP) and a verb phrase (VP). A noun phrase may be composed of a determiner (D/DET) and a noun (N). A noun phrase.
Context-free Grammars [Section 2.1] - more powerful than regular languages - originally developed by linguists - important for compilation of programming.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Grammars CPSC 5135.
Context-free Languages
Copyright © Curt Hill Languages and Grammars This is not English Class. But there is a resemblance.
Complexity and Computability Theory I Lecture #9 Instructor: Rina Zviel-Girshin Lea Epstein.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Lecture 11 Theory of AUTOMATA
Pushdown Automata Chapters Generators vs. Recognizers For Regular Languages: –regular expressions are generators –FAs are recognizers For Context-free.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
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 A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
Lecture 16: Modeling Computation Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output.
Discrete Structures ICS252 Chapter 5 Lecture 2. Languages and Grammars prepared By sabiha begum.
LECTURE 4 Syntax. SPECIFYING SYNTAX Programming languages must be very well defined – there’s no room for ambiguity. Language designers must use formal.
Formal Languages, Automata and Models of Computation
CSCI 2670 Introduction to Theory of Computing September 14, 2005.
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen Department of Computer Science University of Texas-Pan American.
Exercises on Chomsky Normal Form and CYK parsing
1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 8 Mälardalen University 2011.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Costas Busch - LSU1 PDAs Accept Context-Free Languages.
 2005 SDU Lecture11 Decidability.  2005 SDU 2 Topics Discuss the power of algorithms to solve problems. Demonstrate that some problems can be solved.
Theory of Computation Automata Theory Dr. Ayman Srour.
CSCI 2670 Introduction to Theory of Computing September 16, 2004.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Modeling Arithmetic, Computation, and Languages Mathematical Structures for Computer Science Chapter 8 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesAlgebraic.
Computability Joke. Context-free grammars Parsing. Chomsky
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Context-Free Grammars: an overview
Context-free Languages
CHAPTER 2 Context-Free Languages
Chapter 2 Context-Free Language - 01
Theory of Computation Lecture #
Context-Free Grammars
Presentation transcript:

Lecture 21: Languages and Grammars

Natural Language vs. Formal Language

Rules of English Grammar verifying a sentence

Phrase Structure Grammar

An Example

Another Example

Example

Types of Phrase Structure Grammars

A grammar is is used to generate or evaluate members of a formal language. the context-free grammar G 1 below, generates the language {0 n 1 n for n>=0}. A -> 0A1 A ->  (the empty string) A grammar is a set of production rules as shown above. A production rule is comprised of a variable, a right-arrow, and a string of variables and terminals. The variables are used to control the arrangement of possible substitutions and the terminals are symbols from the alphabet of the language being generated or recognized. By convention, variables are represented by uppercase letters. One of the variables is designated as the start variable, usually the left-hand symbol of the first rule in the grammar. Introduction to Grammars

There are usually choices for applying production rules, which means that in order to create a particular member of a language we need to specify an order of application of the rules of the grammar, called a derivation. For example, the following is a derivation to show that is a member of the language defined by G1. start A rule 1 0A1 rule 1 00A11 rule 1 000A111 rule A1111 rule Applying Production Rules

All the member strings that can be generated through derivations using G 1 are collectively referred to as the language of the grammar G 1 or L(G 1 ). We say that a context-free grammar is one in which candidate strings are accepted or rejected as members of L(G 1 ) based strictly on their structures and not based on the context in which they appear. The syntax of a programming language is (mostly) definable with a context-free grammar. For example, the reserved words of a programming language are parsed and recognized as reserved words regardless of their context. (Can you think of an example in a programming language in which context is important?) The Language of the Grammar

Formal Definition of a Context-Free Grammar A context-free grammar is a 4-tuple (V, ,R,S), where 1. V is a finite set of 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 variable and a string of variables and terminals, and 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 of the grammar, we say that uAv yields uwv, written uAv -> uwv. Write u v if u=v or if a sequence u 1,u 2,..., u k exists for k>=0 and, u -> u 1 -> u 2 ->... -> u k -> v. -> *

A Practical Example - The following context-free grammar can be used to derive (or recognize) in- fix arithmetic expressions in x,y and z. The start variable is S and the termials are +, -, *, /, (, ), x, y, and z. S -> T+S | T-S | T T -> T*T | T/T | (S) | x | y | z Use the grammar above to show that (x+y)/(x-y)*z is a valid in-fix expression. S -> T -> T/T -> T/T*T ->... -> (S)/(S)*T ->... -> (T+S)/(T-S)*T ->... -> (x+y)/(x-y)*z An Example

How do we go about demonstrating that a particular string is not derivable with a given context- free grammar? For example how would we use the grammar above to show that (x+y)*/z is not a valid in-fix expression? The problem we are dealing with is that we have no means to choose which rules to apply or in which order to apply them. If the same grammar generates the same string in more than one way, we say that the grammar is ambiguous. Using a grammar to generate arbitrary parts of a candidate string in a random order can lead to an exponential number of alternate paths to test. What we need is a systematic method for applying the rules to show that a particular string is or is not derivable. The Problem with Grammars

Chomsky Normal Form A context-free grammar is said to be in Chomsky normal form (CNF) when every rule has the form A -> BC A -> a where a is any terminal and A, B, and C are any variables except B and C are not the start variable. A rule that replaces the start variable with nothing (i.e. S -> e ) is permitted. We can show that "Any context-free language is generated by a context-free grammar in Chomsky normal form." Perhaps more important to us than the proof itself is the proof idea which we can use to convert a context-free grammar into a CNF grammar.

CNF grammars are very useful since they simplify derivations making them more easily implemented as computer algorithms. The method of conversion of a context-free grammar into CNF is detailed below: 1. Create a new start variable. 2. Eliminate all  rules of the form A -> . 3. Eliminate all unit rules of the form A -> B. 4. Convert the remaining rules into the form A -> BC or A -> a Converting a Grammar into CNF

Example: Lets work through an example of this conversion process. In the following, rules being eliminated will be shown in blue, rules being added will be shown in red. Given the grammar, we apply Step 1, creating a new start variable S 0, Now we need to eliminate rules of the form A-> . In order to do this without changing the grammar, we must also create replacement rules to permit the same derivations that were possible when the epsilon rules were present. For example, removing B ->  requires that we add a rule S -> a to cover the derivation S-> aB -> a that would have been possible with the rule B -> . Similarly we need to include the rule A ->  to account for the A -> B ->  derivation. An Example

We still need to remove the A ->  rule. Next we remove all unit rules. Removing S 0 -> S we must add S 0 -> SA | AS | ASA | aB | a. We can remove S -> S without adding any other rules.

When we remove A -> B we must add the rule A -> b. Removing A -> S we must add A -> SA | AS | ASA | aB | a. This gives us, Finally we must make sure that each rule is in one of the two forms allowed in CNF. Specifically we can have only rules that replace a single variable with a pair of variables or rules that replace a single variable with a single terminal.

Therefore we must modify the rules, S0 -> ASA, S -> ASA, S -> aB, A -> ASA, and A -> aB. We create new variables and new rules to ensure that the grammar derivation is not changed. The original grammar is shown on the left and the CNF of our grammar is on the right.

The CNF grammar is larger and less readable than our original grammar, so what is the advantage of the CNF grammar? We may learn more if we attempt to derive candidate strings using each form. Derivation using the Original Grammar

The value of the CNF grammar begins to be revealed in these sample derivations. Although the CNF grammar has a larger rule set, its derivations are significantly shorter. Derivation using the CNF Grammar