1/34 Context-free Grammars CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Chinese University of Hong Kong Fall 2015
2/34 Precedence in Arithmetic Expressions 5 3 * 2 + or 5 3 2 + * bash$ python Python 2.7.9 (default, Apr 2 2015, 15:33:21) >>> 2+3*5 17 = 25 = 17
3/34 NUM 5 NUM * 3 NUM TERM TERM + 2 TERM Grammars describe meaning EXPR EXPR arithmetic expressions rules for valid (simple) Rules always yield the correct meaning EXPR → EXPR + TERM EXPR → TERM TERM → TERM * NUM TERM → NUM NUM → 0-9
4/34 VERB-PHRASE flower a with A-NOUN girl a A-NOUN girl a Grammar of English PREP-PHRASE a boy the likes NOUN-PHRASE girl SENTENCE → NOUN-PHRASE VERB-PHRASE � �� � � �� � NOUN-PHRASE → A-NOUN or → A-NOUN PREP-PHRASE � �� � � �� � � �� �
5/34 with NOUN-PHRASE flower a PREP with PREP-PHRASE Grammar of English flower a Recursive structure A-NOUN girl girl a A-NOUN a NOUN-PHRASE → A-NOUN or → A-NOUN PREP-PHRASE � �� � � �� � � �� � PREP-PHRASE → PREP NOUN-PHRASE ���� � �� �
5/34 with NOUN-PHRASE flower a PREP with PREP-PHRASE Grammar of English flower a Recursive structure A-NOUN girl girl a A-NOUN a NOUN-PHRASE → A-NOUN or → A-NOUN PREP-PHRASE � �� � � �� � � �� � PREP-PHRASE → PREP NOUN-PHRASE ���� � �� �
Grammar of (parts of) English 6/34 SENTENCE → NOUN-PHRASE VERB-PHRASE ARTICLE → a NOUN-PHRASE → A-NOUN ARTICLE → the NOUN-PHRASE → A-NOUN PREP-PHRASE NOUN → boy VERB-PHRASE → CMPLX-VERB NOUN → girl VERB-PHRASE → CMPLX-VERB PREP-PHRASE NOUN → flower PREP-PHRASE → PREP A-NOUN VERB → likes A-NOUN → ARTICLE NOUN VERB → touches CMPLX-VERB → VERB NOUN-PHRASE VERB → sees CMPLX-VERB → VERB PREP → with
7/34 VERB VERB-PHRASE NOUN-PHRASE CMPLX-VERB NOUN-PHRASE PREP-PHRASE A-NOUN A-NOUN A-NOUN ARTICLE NOUN PREP ARTICLE NOUN The meaning of sentences ARTICLENOUN boy the likes flower a with girl a SENTENCE
7/34 VERB VERB-PHRASE NOUN-PHRASE CMPLX-VERB NOUN-PHRASE PREP-PHRASE A-NOUN A-NOUN A-NOUN ARTICLE NOUN PREP ARTICLE NOUN The meaning of sentences ARTICLENOUN boy the likes flower a with girl a SENTENCE
7/34 VERB VERB-PHRASE NOUN-PHRASE CMPLX-VERB NOUN-PHRASE PREP-PHRASE A-NOUN A-NOUN A-NOUN ARTICLE NOUN PREP ARTICLE NOUN The meaning of sentences ARTICLENOUN boy the likes flower a with girl a SENTENCE
8/34 Context-free grammar 0, 1 are terminals A is the start variable A 0 A 1 00 A 11 000 A 111 000 B 111 000#111 derivation A → 0 A 1 A → B B → # A , B are variables A → 0 A 1 is a production
8/34 Context-free grammar 0, 1 are terminals A is the start variable derivation A → 0 A 1 A → B B → # A , B are variables A → 0 A 1 is a production A ⇒ 0 A 1 ⇒ 00 A 11 ⇒ 000 A 111 ⇒ 000 B 111 ⇒ 000#111
9/34 Context-free grammar A context-free grammar is given by ( V , Σ , R , S ) where ◮ V is a finite set of variables or non-terminals ◮ Σ is a finite set of terminals ◮ R is a set of productions or substitution rules of the form A → α A is a variable and α is a string of variables and terminals ◮ S ∈ V is a variable called the start variable
10/34 Notation and conventions variables in UPPERCASE conventions: shorthand: Start variable: E Terminals: +, (, ), 0, 1 Variables: E , N start variable comes first E → E + E N → 0 N E → ( E ) N → 1 N E → N N → 0 N → 1 E → E + E | ( E ) | N N → 0 N | 1 N | 0 | 1
11/34 Derivation (1+10)+1 E production application of one derivation derivation derivation: a sequential application of productions E ⇒ E + E ⇒ ( E )+ E ⇒ ( E )+ N E → E + E | ( E ) | N ⇒ ( E )+1 N → 0 N | 1 N | 0 | 1 ⇒ ( E + E )+1 ⇒ ( N + E )+1 ⇒ ( N + N )+1 α ⇒ β ⇒ ( N +1 N )+1 ⇒ ( N +10)+1 ⇒ (1+10)+1
11/34 Derivation E production application of one derivation derivation derivation: a sequential application of productions E ⇒ E + E ⇒ ( E )+ E ⇒ ( E )+ N E → E + E | ( E ) | N ⇒ ( E )+1 N → 0 N | 1 N | 0 | 1 ⇒ ( E + E )+1 ⇒ ( N + E )+1 ⇒ ( N + N )+1 α ⇒ β ⇒ ( N +1 N )+1 ⇒ ( N +10)+1 ⇒ (1+10)+1 ∗ ∗ ⇒ (1+10)+1 α ⇒ β
12/34 Context-free languages The language of a CFG is the set of all strings at the end of a derivation Questions we will ask: I give you a CFG, what is the language? I give you a language, write a CFG for it L ( G ) = { w ∈ Σ ∗ | S ∗ ⇒ w }
No: uneven number of 0s and 1s 13/34 00#11 00##11 00#111 # B A # 00 B 11 Analysis example 1 00 A 11 0 A 1 A 00#11 Can you derive: No: too many # A → 0 A 1 | B L ( G ) = { 0 n #1 n | n � 0 } B → #
No: uneven number of 0s and 1s 13/34 Analysis example 1 Can you derive: 00#11 # A B # 00#111 00##11 No: too many # A → 0 A 1 | B L ( G ) = { 0 n #1 n | n � 0 } B → # A ⇒ 0 A 1 ⇒ 00 A 11 ⇒ 00 B 11 ⇒ 00#11
No: uneven number of 0s and 1s 13/34 Analysis example 1 Can you derive: 00#11 # 00#111 00##11 No: too many # A → 0 A 1 | B L ( G ) = { 0 n #1 n | n � 0 } B → # A ⇒ 0 A 1 ⇒ 00 A 11 ⇒ 00 B 11 ⇒ 00#11 A ⇒ B ⇒ #
13/34 Analysis example 1 Can you derive: 00#11 # 00#111 00##11 No: too many # A → 0 A 1 | B L ( G ) = { 0 n #1 n | n � 0 } B → # A ⇒ 0 A 1 ⇒ 00 A 11 ⇒ 00 B 11 ⇒ 00#11 A ⇒ B ⇒ # No: uneven number of 0s and 1s
14/34 Analysis example 2 Can you derive () (()()) S ( S ) ( SS ) (( S ) S ) (( S )( S )) (()( S )) (()()) S → SS | ( S ) | ε S ⇒ ( S ) ⇒ ()
14/34 Analysis example 2 Can you derive () (()()) S → SS | ( S ) | ε S ⇒ ( S ) S ⇒ ( S ) ⇒ () ⇒ ( SS ) ⇒ (( S ) S ) ⇒ (( S )( S )) ⇒ (()( S )) ⇒ (()())
15/34 Parse trees ) S ( S ) S ( S S ( S ) S → SS | ( S ) | ε A parse tree gives a more compact representation S ⇒ ( S ) ⇒ ( SS ) ⇒ (( S ) S ) ⇒ (( S )( S )) ⇒ (()( S )) ε ε ⇒ (()())
16/34 ( ) ) S ( S ) Parse trees S ( S S One parse tree can represent many derivations S S ⇒ ( S ) S ⇒ ( S ) ⇒ ( SS ) ⇒ ( SS ) ⇒ (( S ) S ) ⇒ ( S ( S )) ⇒ (( S )( S )) ⇒ (( S )( S )) ⇒ (()( S )) ⇒ (()( S )) ⇒ (()()) ⇒ (()()) S ⇒ ( S ) S ⇒ ( S ) ⇒ ( SS ) ⇒ ( SS ) ⇒ (( S ) S ) ε ε ⇒ ( S ( S )) ⇒ (() S ) ⇒ ( S ()) ⇒ (()( S )) ⇒ (( S )()) ⇒ (()()) ⇒ (()())
No: uneven number of ( and ) No: some prefix has too many ) 17/34 Analysis example 2 Can you derive (()() ())(() S → SS | ( S ) | ε
No: some prefix has too many ) 17/34 Analysis example 2 Can you derive (()() No: uneven number of ( and ) ())(() S → SS | ( S ) | ε
17/34 Analysis example 2 Can you derive (()() No: uneven number of ( and ) ())(() No: some prefix has too many ) S → SS | ( S ) | ε
Divide w into blocks with 18/34 S Each block is in L G same number of ( and ) Parsing rules: ) ( ) ) ( ) ( ( S Analysis example 2 S S S S S S S no prefix of w has more ) than ( w has the same number of ( and ) w L G Parse each block recursively S → SS | ( S ) | ε
18/34 Analysis example 2 same number of ( and ) Divide w into blocks with Parsing rules: ) ( ) ) ( ) ( ( S S S S S S S S S Parse each block recursively S → SS | ( S ) | ε L ( G ) = { w | w has the same number of ( and ) no prefix of w has more ) than ( } ε Each block is in L ( G ) ε ε
19/34 Design example 1 00001111 000111 0011 01 S 0 S 1 L = { 0 n 1 n | n � 0 } These strings have recursive structure ε
19/34 Design example 1 00001111 000111 0011 01 L = { 0 n 1 n | n � 0 } These strings have recursive structure ε S → 0 S 1 | ε
These strings have two parts: 20/34 L S S S S L is the same as L 0 S 1 S rules for L m m m n Design example 2 n n L L L L 000111 00110011 010011 Examples: 0 S 1 L = { 0 n 1 n 0 m 1 m | n � 0 , m � 0 }
20/34 Design example 2 Examples: 010011 00110011 000111 L = { 0 n 1 n 0 m 1 m | n � 0 , m � 0 } These strings have two parts: L = L 1 L 2 L 1 = { 0 n 1 n | n � 0 } S → S 1 S 1 L 2 = { 0 m 1 m | m � 0 } S 1 → 0 S 1 1 | ε rules for L 1 : S 1 → 0 S 1 1 | ε L 2 is the same as L 1
21/34 Design example 3 Examples: 011001 0011 1100 00110011 These strings have a nested structure: outer part: 0 n 1 n inner part: 1 m 0 m S 0 S 1 I I 1 I 0 L = { 0 n 1 m 0 m 1 n | n � 0 , m � 0 }
21/34 Design example 3 Examples: 011001 0011 1100 00110011 These strings have a nested structure: outer part: 0 n 1 n inner part: 1 m 0 m L = { 0 n 1 m 0 m 1 n | n � 0 , m � 0 } S → 0 S 1 | I I → 1 I 0 | ε
22/34 middle part C : cannot end in 0 A : C final part 1 0 1 1 0 B 0 0 1 1 0 1 0 0 Design example 4 A initial part 1 0 0 1 not allowed 11001000, 01111 allowed 01011, 001011001, 10010101000 cannot begin with 0 L = { x | x has two 0-blocks with the same number 0s }
22/34 Design example 4 C : cannot end in 0 A : C final part 1 0 1 1 0 B middle part cannot begin with 0 0 0 1 1 0 1 0 0 A initial part 1 0 0 1 not allowed 11001000, 01111 allowed 01011, 001011001, 10010101000 L = { x | x has two 0-blocks with the same number 0s } � �� � � �� � � �� �
Recommend
More recommend