CS 212 LECTURE 03 PROGRAMMING LANGUAGES GRAMMAR It

  • Slides: 23
Download presentation
CS 212 LECTURE 03 PROGRAMMING LANGUAGES

CS 212 LECTURE 03 PROGRAMMING LANGUAGES

GRAMMAR • It is often convenient to specify languages in terms of grammars. The

GRAMMAR • It is often convenient to specify languages in terms of grammars. The advantage in doing so arises mainly from the usage of a small number of rules for describing a language with a large number of sentences. For instance, the possibility that an English sentence consists of a subject phrase followed by a predicate phrase can be expressed by a grammatical rule of the form 2

GRAMMAR <sentence> <noun-phrase><verb-phrase> <noun-phrase> <article><adjective><noun> <noun-phrase> <article><noun> <verb-phrase> <verb><adverb> | <verb> <article> a |

GRAMMAR <sentence> <noun-phrase><verb-phrase> <noun-phrase> <article><adjective><noun> <noun-phrase> <article><noun> <verb-phrase> <verb><adverb> | <verb> <article> a | the <adjective> large | hungry <noun> rabbit | mathematician <verb> eat | hops <adverb> quickly | wildly 3

GRAMMAR • From these rules we can form valid sentences using a series of

GRAMMAR • From these rules we can form valid sentences using a series of replacements until no more rules can be used. • Example, the sentence is “the large rabbit hops quickly” we start with the rule : <sentence> <noun-phrase><verb-phrase> <article><adjective><noun><verb-phrase> 4

GRAMMAR àthe large<noun> <verb-phrase> àthe large rabbit <verb><adverb> àthe large rabbit hops quickly Let

GRAMMAR àthe large<noun> <verb-phrase> àthe large rabbit <verb><adverb> àthe large rabbit hops quickly Let try this sentence : “a hungry mathematician eats wildly” 5

GRAMMAR Let move to the formal definition of grammar Definition : A grammar <

GRAMMAR Let move to the formal definition of grammar Definition : A grammar < , N, P, S> consists of four parts: 1. A finite set of terminal symbols , the alphabet of the language, that are assembled to make up the sentences in the language. 2. A finite set N of nonterminal symbols , each of which represents some collection of subphrases of 6 sentences.

GRAMMAR Let move to the formal definition of grammar Definition : A grammar <

GRAMMAR Let move to the formal definition of grammar Definition : A grammar < , N, P, S> consists of four parts: 7

CLASSIFICATION OF GRAMMAR • Chomsky classified grammars according to the structure of their productions,

CLASSIFICATION OF GRAMMAR • Chomsky classified grammars according to the structure of their productions, suggesting four forms of particular usefulness, calling them type 0 through type 3 • Type 0 : The most general grammars, the unrestricted grammars , require only that at least one nonterminal occur on the left side of a rule where α and β are strings of symbols in and α is not the empty string, and 8

TYPE 0 : EXAMPLE Let = { a, b }, N = { A,

TYPE 0 : EXAMPLE Let = { a, b }, N = { A, B, S } , S = { S } P = S ABa , A BB , B ab , AB b 9

CLASSIFICATION OF GRAMMAR • Type 1 : When we add the restriction that the

CLASSIFICATION OF GRAMMAR • Type 1 : When we add the restriction that the right side contains no fewer symbols than the left side, we get the context-sensitive grammars—for example, a rule of the form αβ → αγβ where α, β ∈ (N U Σ)+ (i. e. , α and β are strings of nonterminals and terminals) and γ ∈ (N U Σ)+ (i. e. , γ is a nonempty string of 10 nonterminals and terminals).

TYPE 1 : EXAMPLE = { a, b, c } , N ={ S,

TYPE 1 : EXAMPLE = { a, b, c } , N ={ S, B, C } P= S a. Sbc S a. BC a. B ab b. C bc cb bc This type 1 grammar is generate string anbncn where n 1 11

CLASSIFICATION OF GRAMMAR • Type 2 : The context-free grammars prescribe that the left

CLASSIFICATION OF GRAMMAR • Type 2 : The context-free grammars prescribe that the left side be a single nonterminal producing rules of the form A → w where A ∈ N (i. e. , A is a single nonterminal), w ∈ (N U Σ)* (i. e. , w are strings of nonterminals and terminals) 12

TYPE 2 : EXAMPLE = { a, b } , N ={ S, A}

TYPE 2 : EXAMPLE = { a, b } , N ={ S, A} P = S b. Sbb S A A a. A A This type 2 grammar is generate string 13

CLASSIFICATION OF GRAMMAR • Type 3 : The most restrictive grammars, the regular grammars

CLASSIFICATION OF GRAMMAR • Type 3 : The most restrictive grammars, the regular grammars , allow only a terminal or a terminal followed by one nonterminal on the right side—that is, rules of the form • A → a - where A is a non-terminal in N and a is a terminal in Σ • A → a. B - where A and B are in N and a is in Σ • A → - where A is in N and denotes the empty string, i. e. the string of length 0. 14

TYPE 3 : EXAMPLE = { a, b, c } , N ={ S,

TYPE 3 : EXAMPLE = { a, b, c } , N ={ S, A } P= S → a. S S → b. A A→ A → c. A This type 3 grammar is generate string a*bc * 15

DERIVATIONS • The cfg G that generates the language consisting of strings over ={a,

DERIVATIONS • The cfg G that generates the language consisting of strings over ={a, b} with a even number of a G = ( , N, P, S) , N={S, A} , ={a, b} P : S AA A AAA | b. A | Ab | a we want to generate string ababaa, we will write its derivation. So, we have two types of derivation which are leftmost and rightmost derivation. 16

DERIVATIONS S=> AA => a. AAA => aba. AA => ababaa Leftmost S=> AAAA

DERIVATIONS S=> AA => a. AAA => aba. AA => ababaa Leftmost S=> AAAA => aba. AA => ababaa Left most S=> AA => Aa => AAbaa => Ab. Abaa => Ababaa => ababaa Rightmost 17

CFG EXAMPLE �The cfg G that generates the language consisting of strings over ={0,

CFG EXAMPLE �The cfg G that generates the language consisting of strings over ={0, 1} G = ( , N, P, S) , N={A, B} , ={0, 1} , S={A} P: A B | B 0 A 1 we want to generate string 000111, write its leftmost or rightmost derivation. 18

DERIVATIONS Leftmost derivation A => B => 0 A 1 => 0 B 1

DERIVATIONS Leftmost derivation A => B => 0 A 1 => 0 B 1 => 00 A 11 => 00 B 11 => 000 A 111 => 000111 19

Some Pascal production rules <expression> <simple expression> <term> | <sign><term> | <simple expression><adding operator><term>

Some Pascal production rules <expression> <simple expression> <term> | <sign><term> | <simple expression><adding operator><term> <adding operator> + | <multiplying operator> * | / | div | mod <term> <factor> | <term><multiplying operator><factor> <identifier> | <unsigned constant> | (<expression>) 20

<unsigned constant> <unsigned number> <unsigned integer> | <unsigned real> <unsigned integer> <digit><unsigned integer> |

<unsigned constant> <unsigned number> <unsigned integer> | <unsigned real> <unsigned integer> <digit><unsigned integer> | <digit > <identifier> <letter><identifier tail>| <digit><identifier tail> | <sign> + | <digit> 0 | 1 | 2 |…| 9 <letter> a | b | c | … | z 21

Example : Uses Pascal production rules to find the leftmost derivation that given the

Example : Uses Pascal production rules to find the leftmost derivation that given the constant 65. Derivation is <expression> <simple expression> <term> <factor> <unsigned constant> <unsigned number> <unsigned integer> <digit><unsigned integer> 6<digit> 65 22

TRY THIS • Uses Pascal production rules to find the leftmost derivation that given

TRY THIS • Uses Pascal production rules to find the leftmost derivation that given -250. • Uses Pascal production rules to find the leftmost derivation that given 5 * ( x + 15 ) • Uses Pascal production rules to find the leftmost derivation that given ( (x – 10) div 2 ) • Uses Pascal production rules to find the leftmost derivation that given – 125 /( x 1 div 2 ) 23