Languages and Grammars MSU CSE 260 4703 MSU

  • Slides: 24
Download presentation
Languages and Grammars MSU CSE 260 4/7/03 MSU CSE 260 -001 SS 03 -

Languages and Grammars MSU CSE 260 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 1

Outline • Introduction: Example • Phrase-Structure Grammars: Terminology, Definition, Derivation, Language of a Grammar,

Outline • Introduction: Example • 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 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 2

Introduction • In the English language, the grammar determines whether a combination of words

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. – The frog writes neatly. – Swims quickly mathematician. Yes No • Grammars are concerned with the syntax (form) of a sentence, and NOT its semantics (or meaning. ) 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 3

English Grammar • Sentence: noun phrase followed by verb phrase; • Noun phrase: article

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; 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 4

Example • • • Sentence Noun phrase verb phrase Article adjective noun verb adverb

Example • • • Sentence Noun phrase verb phrase Article adjective noun verb adverb the large rabbit verb adverb the large rabbit hops quickly 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 5

Grammars and Computation • Grammars are used as a model of computation. • Grammars

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. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 6

Phrase-Structure Grammars Terminology • Definitions. A vocabulary (or alphabet) V is a finite, nonempty

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*. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 7

Phrase-Structure Grammars • A language can be specified by: – listing all the words

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. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 8

Phrase-Structure Grammar Definition A phrase-structure grammar G = (V, T, S, P) consists of:

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. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 9

Phrase-structure Grammar Example • G = {V, T, S, P}, where – V =

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}. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 10

Phrase-Structure Grammars Derivation • Definition. Let G = (V, T, S, P) be a

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, …, wn are strings over V such that: w 0 w 1, w 1 w 2, …, wn-1 wn, we say that: wn is derivable from w 0 (denoted: w 0 * wn. ) Note. * should be on top of . – The sequence of all steps used to obtain wn from w 0 is called a derivation. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 11

Example • In the previous example grammar, the production: B ab makes the string

Example • In the previous example grammar, the production: B ab makes the string Aaba directly derivable from string ABa. – ABa Aaba • Also Aaba BBaba Babababa – using: A BB, B ab, and B ab. • So: ABa * abababa Øabababa is derivable from ABa. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 12

Language of a Grammar • Definition. Let G = (V, T, S, P) be

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}. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 13

Example • Let G = {V, T, S, P} be the grammar where: –

Example • Let G = {V, T, S, P} be the grammar where: – V = {S, 0, 1}, – T = {0, 1}, – P = { S 11 S, S 0}. • What is L(G)? – At any stage of the derivation we can either: • add two 1 s at the end of the string, or • terminate the derivation by adding a 0 at the end of the string. – L(G)={0, 11110, 1111110, …} = Set of all strings that begin with an even number of 1 s and end with 0. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 14

Exercise 10. 1 (1) 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem

Exercise 10. 1 (1) 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 15

Types of Grammars • A type 0 (phrase-structure) grammar has no restrictions on its

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. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 16

Types of Grammars – cont. • A type 3 (or regular) grammar has productions

Types of Grammars – cont. • A type 3 (or regular) grammar has productions only of the form: – A a. B, 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 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 17

Types of Grammars - Summary Type 0 1 2 3 4/7/03 Restrictions on productions

Types of Grammars - Summary Type 0 1 2 3 4/7/03 Restrictions on productions w 1 w 2 No restrictions l(w 1) l(w 2), or w 2= w 1=A where A N w 1=A, and w 2=a. B or w 2=a, where A N, B N, a T, or w 1=S and w 2= MSU CSE 260 -001 SS 03 - N. Assem 18

Derivation Trees • For type 2 (context-free) grammars: A derivation (or parse) tree, is

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: – – 4/7/03 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. MSU CSE 260 -001 SS 03 - N. Assem 19

Example • Derivation tree for: the hungry rabbit eats quickly sentence noun phrase verb

Example • Derivation tree for: the hungry rabbit eats quickly sentence noun phrase verb phrase article adjective noun verb adverb the hungry rabbit eats quickly 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 20

Exercise 10. 1 (2, 3) 4/7/03 MSU CSE 260 -001 SS 03 - N.

Exercise 10. 1 (2, 3) 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 21

Parsing • To determine whether a string is in the language generated by a

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. 4/7/03 MSU CSE 260 -001 SS 03 - N. Assem 22

Example • Let G = {V, T, S, P} be the grammar where: 4/7/03

Example • Let G = {V, T, S, P} be the grammar where: 4/7/03 – 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 Ca. B B Cb S AB Ca. B cba. B B b S AB Ca. B cbab C cb Bottom-up parsing: C b Cab cbab Ab Cab cbab AB Ab Cab cbab S AB Ab Cab cbab MSU CSE 260 -001 SS 03 - N. Assem 23

Backus-Naur Form • Used with type 2 (context-free) grammars; like for specification of programming

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. – – 4/7/03 <signed integer> : : = <sign><integer> <sign> : : = + | <integer> : : = <digit> | <digit><integer> <digit> : : = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 MSU CSE 260 -001 SS 03 - N. Assem 24