CS 212 LECTURE 04 PROGRAMMING LANGUAGES EXAMPLE Build
CS 212 LECTURE 04 PROGRAMMING LANGUAGES
EXAMPLE • Build a DFA for regular expression (1) R = (001)* + (110)* Step (1) Start with 0 0 1 Step (2) Create loop 0 0 1 (2) 2
(3) Step (3) Start with 1 1 0 Step (4) Create loop 1 1 0 (4) 3
(5) Step (5) In each state, make the transitions that make string aren’t in R go to q 7. (Dead state) 4
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 5
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 6
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> the<adjective><noun> ><verb-phrase> 7
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” 8
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 sentences. 3. A finite set P of productions or rules that describe how each nonterminal is defined in terms of terminal symbols and nonterminals. The choice of nonterminals determines the phrases of the language to which we ascribe meaning. 4. A distinguished nonterminal S, the start symbol , that specifies the principal category being defined—for example, sentence or program. 9
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 designated start symbol. is a specially 10
TYPE 0 : EXAMPLE Let = { a, b }, N = { A, B, S } , S = { S } P = S ABa , A BB , B ab , AB b 11
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 nonterminals and terminals). 12
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 13
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) 14
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 15
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. 16
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 * 17
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. 18
DERIVATIONS S=> AA => a. A => AAAA => Aa => a. AAA => AAAa => ab. AAA => AAb. Aa => aba. AA => AAbaa => abab. AA => Ab. Abaa => ababa. A => Ababaa => ababaa 19 Leftmost Left most Rightmost
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. 20
DERIVATIONS Leftmost derivation A => B => 0 A 1 => 0 B 1 => 00 A 11 => 00 B 11 => 000 A 111 => 000111 21
• 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>) 22
<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 23
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 24
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 ) 25
- Slides: 25