context free grammars
play

Context-free Grammars CSCI 3130 Formal Languages and Automata Theory - PowerPoint PPT Presentation

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,


  1. 1/34 Context-free Grammars CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Chinese University of Hong Kong Fall 2015

  2. 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. 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. 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. 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 ���� � �� �

  6. 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 ���� � �� �

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

  14. 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

  15. 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

  16. 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 α ⇒ β

  17. 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 }

  18. 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 → #

  19. 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

  20. 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 ⇒ #

  21. 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

  22. 14/34 Analysis example 2 Can you derive () (()()) S ( S ) ( SS ) (( S ) S ) (( S )( S )) (()( S )) (()()) S → SS | ( S ) | ε S ⇒ ( S ) ⇒ ()

  23. 14/34 Analysis example 2 Can you derive () (()()) S → SS | ( S ) | ε S ⇒ ( S ) S ⇒ ( S ) ⇒ () ⇒ ( SS ) ⇒ (( S ) S ) ⇒ (( S )( S )) ⇒ (()( S )) ⇒ (()())

  24. 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 )) ε ε ⇒ (()())

  25. 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 )()) ⇒ (()()) ⇒ (()())

  26. No: uneven number of ( and ) No: some prefix has too many ) 17/34 Analysis example 2 Can you derive (()() ())(() S → SS | ( S ) | ε

  27. No: some prefix has too many ) 17/34 Analysis example 2 Can you derive (()() No: uneven number of ( and ) ())(() S → SS | ( S ) | ε

  28. 17/34 Analysis example 2 Can you derive (()() No: uneven number of ( and ) ())(() No: some prefix has too many ) S → SS | ( S ) | ε

  29. 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 ) | ε

  30. 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 ) ε ε

  31. 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 ε

  32. 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 | ε

  33. 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 }

  34. 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

  35. 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 }

  36. 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 | ε

  37. 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 }

  38. 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 } � �� � � �� � � �� �

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend