Languages and Grammars MSU CSE 260. Outline Introduction: E xample Phrase-Structure Grammars: Terminology, Definition, Derivation, Language of a Grammar,

Slides:



Advertisements
Similar presentations
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
Advertisements

Grammars, Languages and Parse Trees. Language Let V be an alphabet or vocabulary V* is set of all strings over V A language L is a subset of V*, i.e.,
COGN1001: Introduction to Cognitive Science Topics in Computer Science Formal Languages and Models of Computation Qiang HUO Department of Computer.
Chapter Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
Context-Free Grammars Sipser 2.1 (pages 99 – 109).
CS5371 Theory of Computation
Context-Free Grammars Lecture 7
Discussion #31/20 Discussion #3 Grammar Formalization & Parse-Tree Construction.
Normal forms for Context-Free Grammars
Chapter 3: Formal Translation Models
Lecture 9UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 9.
Syntactic Pattern Recognition Statistical PR:Find a feature vector x Train a system using a set of labeled patterns Classify unknown patterns Ignores relational.
Language Translation Principles Part 1: Language Specification.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Syntactic Analysis Dr. Hyunyoung Lee.
Chapter 13. Chapter Summary Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output Language Recognition Turing.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
1 INFO 2950 Prof. Carla Gomes Module Modeling Computation: Languages and Grammars Rosen, Chapter 12.1.
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.
Languages & Strings String Operations Language Definitions.
Modeling Computation Rosen, ch. 12.
Introduction Syntax: form of a sentence (is it valid) Semantics: meaning of a sentence Valid: the frog writes neatly Invalid: swims quickly mathematics.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
Winter 2007SEG2101 Chapter 71 Chapter 7 Introduction to Languages and Compiler.
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.
Languages, Grammars, and Regular Expressions Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth.
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.
Grammars CPSC 5135.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
CSNB143 – Discrete Structure Topic 11 – Language.
Phrase-structure grammar A phrase-structure grammar is a quadruple G = (V, T, P, S) where V is a finite set of symbols called nonterminals, T is a set.
Regular Grammars Chapter 7. Regular Grammars A regular grammar G is a quadruple (V, , R, S), where: ● V is the rule alphabet, which contains nonterminals.
CMSC 330: Organization of Programming Languages Context-Free Grammars.
Chapter 3 Describing Syntax and Semantics
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
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.
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
September1999 CMSC 203 / 0201 Fall 2002 Week #14 – 25/27 November 2002 Prof. Marie desJardins clip art courtesy of
Programming Languages and Design Lecture 2 Syntax Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Lecture 16: Modeling Computation Languages and Grammars Finite-State Machines with Output Finite-State Machines with No Output.
Formal Languages and Grammars
Discrete Structures ICS252 Chapter 5 Lecture 2. Languages and Grammars prepared By sabiha begum.
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
Chapter 4: Syntax analysis Syntax analysis is done by the parser. –Detects whether the program is written following the grammar rules and reports syntax.
Models of Computation by Dr. Michael P. Frank, University of Florida Modified and extended by Longin Jan Latecki, Temple University Rosen 7 th ed., Ch.
1 A well-parenthesized string is a string with the same number of (‘s as )’s which has the property that every prefix of the string has at least as many.
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.
7.2 Programming Languages - An Introduction to Informatics WMN Lab. Hye-Jin Lee.
Lecture 17: Theory of Automata:2014 Context Free Grammars.
Chapter 2. Formal Languages Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Formal Languages and Automata FORMAL LANGUAGES FINITE STATE AUTOMATA.
20 G M aaba acba aaba.. What is it about? Models of Language Generation Models of Language Recognition.
Modeling Arithmetic, Computation, and Languages Mathematical Structures for Computer Science Chapter 8 Copyright © 2006 W.H. Freeman & Co.MSCS SlidesAlgebraic.
PROGRAMMING LANGUAGES
PROGRAMMING LANGUAGES
BCT 2083 DISCRETE STRUCTURE AND APPLICATIONS
Discrete Mathematics and its Applications Rosen 7th ed., Ch. 13.1
Chapter 7 Regular Grammars
Discrete Mathematics and its Applications Rosen 6th ed., Ch. 12.1
Models of Computation by Dr. Michael P
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Discrete Mathematics and its Applications Rosen 7th ed., Ch. 13.1
Discrete Mathematics and its Applications Rosen 7th ed., Ch. 13.1
Models of Computation by Dr. Michael P
Discrete Mathematics and its Applications Rosen 8th ed., Ch. 13.1
COSC 3340: Introduction to Theory of Computation
Models of Computation by Dr. Michael P
Languages and Grammer In TCS
Presentation transcript:

Languages and Grammars MSU CSE 260

Outline Introduction: E xample Phrase-Structure Grammars: Terminology, Definition, Derivation, Language of a Grammar, Examples –Exercise 10.1 (1) Types of Phrase-Structure Grammars Derivation Trees: Example, Parsing –Exercise 10.1 (2, 3) Backus-Naur Form

Introduction In the English language, the grammar determines whether a combination of words is a valid sentence. Are the following valid sentences? –The large rabbit hops quickly.Yes –The frog writes neatly.Yes –Swims quickly mathematician.No Grammars are concerned with the syntax (form) of a sentence, and NOT its semantics (or meaning.)

English Grammar Sentence: noun phrase followed by verb phrase; Noun phrase: article adjective noun, or article noun; Verb phrase: verb adverb, or verb; Article: a, or the; Adjective: large, or hungry; Noun: rabbit, or mathematician, or frog; Verb: eats, or hops, or writes, or swims; Adverb: quickly, or wildly, or neatly;

Example Sentence Noun phrase verb phrase Article adjective noun verb phrase Article adjective noun verb adverb the adjective noun verb adverb the large noun verb adverb the large rabbit verb adverb the large rabbit hops adverb the large rabbit hops quickly

Grammars and Computation Grammars are used as a model of computation. Grammars are used to: –generate the words of a language, and –determine whether a word is in a language.

Phrase-Structure Grammars Terminology Definitions. A vocabulary (or alphabet) V is a finite, nonempty set of elements called symbols. A word (or sentence) over V is a string of finite length of elements of V. The empty string (or null string,) denoted by, is the string containing no symbols. The set of all words over V is denoted by V*. A language over V is a subset of V*.

Phrase-Structure Grammars A language can be specified by: –listing all the words in the language, or –giving a set of criteria satisfied by its words, or –using a grammar. A grammar provides: –a set of symbols, and –a set of rules, called productions, for producing words by replacing strings by other strings: w 0  w 1.

Phrase-Structure Grammar Definition A phrase-structure grammar G = (V, T, S, P) consists of: –a vocabulary V, –a subset T of V consisting of terminal elements, –a start symbol S from V, and –a set P of productions. The set N = V-T consists of nonterminal symbols. Every production in P must contain at least one nonterminal on its left side.

Phrase-structure Grammar Example G = {V, T, S, P}, where –V = {a, b, A, B, S}, –T = {a, b}, –S is the start symbol, and –P = {S  Aba, A  BB, B  ab, AB  b}.

Phrase-Structure Grammars Derivation Definition. Let G = (V, T, S,P) be a phrase-structure grammar. Let w 0 = lz 0 r and w 1 = lz 1 r be strings over V. –If z 0  z 1 is a production of G, we say that: w 1 is directly derivable from w 0 (denoted: w 0  w 1.) –If w 0, w 1, …, w n are strings over V such that: w 0  w 1, w 1  w 2, …, w n-1  w n, we say that: w n is derivable from w 0 (denoted: w 0  * w n.) Note. * should be on top of . –The sequence of all steps used to obtain w n from w 0 is called a derivation.

Example In the previous example grammar, the production: B  ab makes the string Aaba directly derivable from string ABa. –ABa  Aaba Also Aaba  BBaba  Bababa  abababa –using: A  BB, B  ab, and B  ab. So: ABa  * abababa  abababa is derivable from ABa.

Language of a Grammar Definition. Let G = (V, T, S, P) be a phrase-structure grammar. The language generated by G (or the language of G), denoted by L(G), is the set of all strings of terminals that are derivable from the start symbol S. L(G) = {w  T* | S  * w}.

Example Let G = {V, T, S, P} be the grammar where: –V = {S, 0, 1}, –T = {0, 1}, –P = {S  11S, S  0}. What is L(G)? –At any stage of the derivation we can either: add two 1s at the end of the string, or terminate the derivation by adding a 0 at the end of the string. –L(G)={0, 110, 11110, , …} = Set of all strings that begin with an even number of 1s and end with 0.

Exercise 10.1 (1)

Types of Grammars A type 0 (phrase-structure) grammar has no restrictions on its productions. A type 1 (or context-sensitive) grammar has productions only of forms: –w 1  w 2 with length of w 2  length of w 1, or –w 1 . A type 2 (or context-free) grammar has productions only of the form A  w 2, where A is a single nonterminal symbol.

Types of Grammars – cont. A type 3 (or regular) grammar has productions only of the form: –A  aB, or A  a, where A and B are nonterminal symbols, and a is a terminal symbol, or –S . Note. –Every type 3 grammar is a type 2 grammar –Every type 2 grammar is a type 1 grammar –Every type 1 grammar is a type 0 grammar

Types of Grammars - Summary Type Restrictions on productions w 1  w 2 0No restrictions 1l(w 1 )  l(w 2 ), or w 2 = 2w 1 =A where A  N 3w 1 =A, and w 2 =aB or w 2 =a, where A  N, B  N, a  T, or w 1 =S and w 2 =

Derivation Trees For type 2 (context-free) grammars: A derivation (or parse) tree, is an ordered rooted tree that represents a derivation in the language generated by a context-free grammar, where: –the root represents the starting symbol; –the internal vertices represent nonterminal symbols; –the leaves represent the terminal symbols; –for a production A  w, the vertex representing A will have children vertices that represent each symbol in w.

Example Derivation tree for: the hungry rabbit eats quickly sentence noun phrase verb phrase article adjective noun verb adverb the hungry rabbit eats quickly

Exercise 10.1 (2, 3)

Parsing To determine whether a string is in the language generated by a grammar, use: –Top-down parsing: Begin with S and attempt to derive the word by successively applying productions, or –Bottom-up parsing: Work backward: Begin by inspecting the word and apply productions backward.

Example Let G = {V, T, S, P} be the grammar where: –V = {a, b, c, A, B, C, S}, T = {a, b, c}, –Productions: Determine whether cbab is in L(G)? S  AB Top-down parsing: A  Ca S  AB B  Ba S  AB  CaB B  Cb S  AB  CaB  cbaB B  b S  AB  CaB  cbaB  cbab C  cb Bottom-up parsing: C  b Cab  cbab Ab  Cab  cbab AB  Ab  Cab  cbab S  AB  Ab  Cab  cbab

Backus-Naur Form Used with type 2 (context-free) grammars; like for specification of programming languages: –Use ::= instead of  –Enclose nonterminal symbols within –Group productions with same left side with symbol | Example. – ::= – ::= + | - – ::= | – ::= 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9