150 likes | 397 Views
Chapter 3 Grammar and Formal Language. Samuel College of Computer Science & Technology Harbin Engineering University. The type of grammar. In 1956, Noam Chomsky divides grammar into four types according to different limitations . 1.Grammar 0
E N D
Chapter 3 Grammar and Formal Language Samuel College of Computer Science & Technology Harbin Engineering University
The type of grammar • In 1956, Noam Chomsky divides grammar into four types according to different limitations. • 1.Grammar 0 Grammar 0 is a kind of unrestricted grammar—Natural Language(Language 0), it looks like . u::=v (u∈V+, v∈V*) zhangjing@hrbeu.edu.cn
Example2.10 Grammar G[S]=(VN, VT, P, S) VN ={S} VT={0, 1} P: S::=0S1 0S::=1S S1::=0S The language of it is: L(G)={},we can see that the grammar G[S] can not deduce any language. zhangjing@hrbeu.edu.cn
Example2.11 Grammar G[A]=(VN, VT, P, A) P: A ::=a A b | B c B. aBc ::= d. dB ::= bb. The derivation of it is A ⇒ aAb ⇒ aBcBb ⇒ dBb ⇒ bbb This means “bbb” is a legal sentence in this language. • Note: Grammar 0 can be recognized by Turing machines. . zhangjing@hrbeu.edu.cn
2.Grammar 1 If the definition of grammar 1 can be written like xUy::=xuy (U∈VN, u∈V+, x, y∈V*) The correspond language of grammar 1 is language1. . The feature of grammar 1 is that U is related with x and y while the U is replaced by u, as a result,the grammar 1 is also called context-sensitive grammar. In addition, the |xUy|≤|xuy|means that the length of xUy is smaller than the length of xuy. zhangjing@hrbeu.edu.cn
Example2.12 Grammar G[S]=(VN, VT, P, S) VN ={S} VT={0, 1} P: S::=0S1 0S::=0 S1::=1 L(G[S])={0n1 n |n≥1} • Note: Grammar 1 can be recognized by linear bounded automata. zhangjing@hrbeu.edu.cn
3. Grammar 2 The definition of grammar 2 is, U::=u (U∈VN, u∈V*) The grammar has no relation with others, so it is called context–free grammar, and it’s language is context-free language or language 2 . . zhangjing@hrbeu.edu.cn
Example2.13 Grammar G[S]=(VN, VT, P, S) VN ={S} VT={0, 1} P: S::=0S1 S::=a L(G[S])={0na1 n |n≥1} zhangjing@hrbeu.edu.cn
Example2.14 Grammar G[S]=(VN, VT, P, S) P: S::=ab|aSb The derivation of “aaabbb”: S⇒ a S b ⇒a a S bb ⇒a a a b b b So sentence: “aaabbb” is a legal sentence in this language L(G[S])={a nbn |n≥1} • Note:Grammar 2 can be recognized by push-down automata . . zhangjing@hrbeu.edu.cn
4. Grammar 3 The rules in Grammar 3 are shown as follows, U::=a U::= Wa or U::=aW (U、W∈VN, a∈VT) • Grammar 3 is also named regular expressions or regular grammar. Especially, the regular expressions is often used by the lexical analyzer. Its correspond language is language3 . . zhangjing@hrbeu.edu.cn
Example2.15 Grammar G[S]=(VN, VT, P, S) VN ={S} VT={0, 1} P: S::=0S S::=a “0a” is a legal sentence in this language. zhangjing@hrbeu.edu.cn
Example2.16 Grammar G[S]: P: S::=a | b | a S| bS The derivation of it is S ⇒a S ⇒a b S ⇒a b b This means “abb” is a legal sentence in this language. L={w|w consists of arbitrary number of ‘a’s and ‘b’s} • Note: Grammar 3 can be recognized by finite automata. . zhangjing@hrbeu.edu.cn
5.The relation and difference among grammars The relation among grammar is that the grammar is divided by the limitations. The less flexible it has, the higher type of grammar it is. Such as language 3 has more limitations than language 2, and so on. . zhangjing@hrbeu.edu.cn
The difference among grammars are: (1) Grammar 1 does not include grammar: A::=ε, but grammar 2 and 3 can consist of it. So grammar 2 and 3 that include grammar A::=εdo not belong to Grammar 1. . (2) The left side of Grammar 0 and 1 can have the terminal symbols or more than two non-terminal symbols. But the left side of grammar 2 and 3 can only has one non-terminal symbol . . (3) Only grammar 2 and 3 are relevant in compiler construction. Regular grammar can recognize words. Context free grammar can recognize blocks, and pairsof parenthesis, such as ((a+b) *c)/2. . zhangjing@hrbeu.edu.cn