chapter twelve context free languages
play

Chapter Twelve: Context-Free Languages Formal Language, chapter 12, - PowerPoint PPT Presentation

Chapter Twelve: Context-Free Languages Formal Language, chapter 12, slide 1 1 We defined the right-linear grammars by giving a simple restriction on the form of each production. By relaxing that restriction a bit, we get a broader class of


  1. Chapter Twelve: 
 Context-Free Languages Formal Language, chapter 12, slide 1 1

  2. We defined the right-linear grammars by giving a simple restriction on the form of each production. By relaxing that restriction a bit, we get a broader class of grammars: the context-free grammars. These grammars generate the context-free languages, which include all the regular languages along with many that are not regular. Formal Language, chapter 12, slide 2 2

  3. Outline • 12.1 Context-Free Grammars and Languages • 12.2 Writing CFGs • 12.3 CFG Applications: BNF • 12.4 Parse Trees • 12.5 Ambiguity • 12.6 EBNF Formal Language, chapter 12, slide 3 3

  4. Examples • We've proved that these languages are not regular, yet they have grammars – { a n b n } S → aSb | ε – { xx R | x ∈ { a , b }*} S → aSa | bSb | ε � S → aSa | R – { a n b j a n | n ≥ 0, j ≥ 1} 
 R → bR | b � • Although not right-linear, these grammars still follow a rather restricted form … Formal Language, chapter 12, slide 4 4

  5. Context-Free Grammars • A context-free grammar (CFG) is one in which every production has a single nonterminal symbol on the left-hand side • A production like R → y is permitted – It says that R can be replaced with y , regardless of the context of symbols around R in the string • One like uRz → uyz is not permitted – That would be context-sensitive: it says that R can be replaced with y only in a specific context Formal Language, chapter 12, slide 5 5

  6. Context-Free Languages • A context-free language (CFL) is one that is L ( G ) for some CFG G • Every regular language is a CFL – Every regular language has a right-linear grammar – Every right-linear grammar is a CFG • But not every CFL is regular – { a n b n } – { xx R | x ∈ { a , b }*} – { a n b j a n | n ≥ 0, j ≥ 1} Formal Language, chapter 12, slide 6 6

  7. Language Classes So Far L ( a * b *) regular languages CFLs { a n b n } Formal Language, chapter 12, slide 7 7

  8. Outline • 12.1 Context-Free Grammars and Languages • 12.2 Writing CFGs • 12.3 CFG Applications: BNF • 12.4 Parse Trees • 12.5 Ambiguity • 12.6 EBNF Formal Language, chapter 12, slide 8 8

  9. Writing CFGs • Programming: – A program is a finite, structured, mechanical thing that specifies a potentially infinite collection of runtime behaviors – You have to imagine how the code you are crafting will unfold when it executes • Writing grammars: – A grammar is a finite, structured, mechanical thing that specifies a potentially infinite language – You have to imagine how the productions you are crafting will unfold in the derivations of terminal strings • Programming and grammar-writing use some of the same mental muscles • Here follow some techniques and examples … Formal Language, chapter 12, slide 9 9

  10. Regular Languages • If the language is regular, we already have a technique for constructing a CFG – Start with an NFA – Convert to a right-linear grammar using the construction from chapter 10 Formal Language, chapter 12, slide 10 10

  11. Example L = { x ∈ {0,1}* | the number of 0s in x is divisible by 3} 1 1 1 0 0 T U S 0 S → 1 S | 0 T | ε 
 T → 1 T | 0 U 
 U → 1 U | 0 S Formal Language, chapter 12, slide 11 11

  12. Example L = { x ∈ {0,1}* | the number of 0s in x is divisible by 3} • The conversion from NFA to grammar always works • But it does not always produce a pretty grammar • It may be possible to design a smaller or otherwise more readable CFG manually: S → 1 S | 0 T | ε 
 S → T 0 T 0 T 0 S | T T → 1 T | 0 U 
 T → 1 T | ε U → 1 U | 0 S Formal Language, chapter 12, slide 12 12

  13. Balanced Pairs • CFLs often seem to involve balanced pairs – { a n b n }: every a paired with b on the other side – { xx R | x ∈ { a , b }*}: each symbol in x paired with its mirror image in x R – { a n b j a n | n ≥ 0, j ≥ 1}: each a on the left paired with one on the right • To get matching pairs, use a recursive production of the form R → xRy • This generates any number of x s, each of which is matched with a y on the other side Formal Language, chapter 12, slide 13 13

  14. Examples • We've seen these before: – { a n b n } S → aSb | ε – { xx R | x ∈ { a , b }*} S → aSa | bSb | ε – { a n b j a n | n ≥ 0, j ≥ 1} S → aSa | R R → bR | b • Notice that they all use the R → xRy trick Formal Language, chapter 12, slide 14 14

  15. Examples • { a n b 3 n } – Each a on the left can be paired with three b s on the right – That gives S → aSbbb | ε { xy | x ∈ { a , b }*, y ∈ { c , d }*, and | x | = | y |} • – Each symbol on the left (either a or b ) can be paired with one on the right (either c or d ) – That gives S → XSY | ε X → a | b Y → c | d Formal Language, chapter 12, slide 15 15

  16. Concatenations • A divide-and-conquer approach is often helpful • For example, L = { a n b n c m d m } – We can make grammars for { a n b n } and { c m d m }: S 1 → aS 1 b | ε S 2 → cS 2 d | ε – Now every string in L consists of a string from the first followed by a string from the second – So combine the two grammars and add a new start symbol: S → S 1 S 2 
 S 1 → aS 1 b | ε S 2 → cS 2 d | ε Formal Language, chapter 12, slide 16 16

  17. Concatenations, In General • Sometimes a CFL L can be thought of as the concatenation of two languages L 1 and L 2 – That is, L = L 1 L 2 = { xy | x ∈ L 1 and y ∈ L 2 } • Then you can write a CFG for L by combining separate CFGs for L 1 and L 2 – Be careful to keep the two sets of nonterminals separate, so no nonterminal is used in both – In particular, use two separate start symbols S 1 and S 2 • The grammar for L consists of all the productions from the two sub-grammars, plus a new start symbol S with the production S → S 1 S 2 Formal Language, chapter 12, slide 17 17

  18. Unions, In General • Sometimes a CFL L can be thought of as the union of two languages L = L 1 ∪ L 2 • Then you can write a CFG for L by combining separate CFGs for L 1 and L 2 – Be careful to keep the two sets of nonterminals separate, so no nonterminal is used in both – In particular, use two separate start symbols S 1 and S 2 • The grammar for L consists of all the productions from the two sub-grammars, plus a new start symbol S with the production S → S 1 | S 2 Formal Language, chapter 12, slide 18 18

  19. Example L = { z ∈ {a,b}* | z = xx R for some x , or | z | is odd} • This can be thought of as a union: L = L 1 ∪ L 2 S 1 → aS 1 a | bS 1 b | ε – L 1 = { xx R | x ∈ {a,b}*} S 2 → XXS 2 | X 
 – L 2 = { z ∈ {a,b}* | | z | is odd} X → a | b S → S 1 | S 2 
 • So a grammar for L is S 1 → aS 1 a | bS 1 b | ε 
 S 2 → XXS 2 | X 
 X → a | b Formal Language, chapter 12, slide 19 19

  20. Example L = { a n b m | n ≠ m } • This can be thought of as a union: – L = { a n b m | n < m } ∪ { a n b m | n > m } • Each of those two parts can be thought of as a concatenation: – L 1 = { a n b n } – L 2 = { b i | i > 0} – L 3 = { a i | i > 0} S → S 1 S 2 | S 3 S 1 
 – L = L 1 L 2 ∪ L 3 L 1 S 1 → aS 1 b | ε 
 S 2 → bS 2 | b 
 • The resulting grammar: S 3 → aS 3 | a Formal Language, chapter 12, slide 20 20

  21. Outline • 12.1 Context-Free Grammars and Languages • 12.2 Writing CFGs • 12.3 CFG Applications: BNF • 12.4 Parse Trees • 12.5 Ambiguity • 12.6 EBNF Formal Language, chapter 12, slide 21 21

  22. BNF • John Backus and Peter Naur • A way to use grammars to define the syntax of programming languages (Algol), 1959-1963 • BNF: Backus-Naur Form • A BNF grammar is a CFG, with notational changes: – Nonterminals are written as words enclosed in angle brackets: < exp > instead of E – Productions use ::= instead of → – The empty string is < empty > instead of ε • CFGs (due to Chomsky) came a few years earlier, but BNF was developed independently Formal Language, chapter 12, slide 22 22

  23. Example < exp > ::= < exp > - < exp > | < exp > * < exp > | < exp > = < exp > 
 | < exp > < < exp > | ( < exp > ) | a | b | c • This BNF generates a little language of expressions: – a<b – (a-(b*c)) Formal Language, chapter 12, slide 23 23

  24. Example < stmt > ::= < exp-stmt > | < while-stmt > | < compound-stmt > |... 
 < exp-stmt > ::= < exp > ; 
 < while-stmt > ::= while ( < exp > ) < stmt > 
 < compound-stmt > ::= { < stmt-list > } 
 < stmt-list > ::= < stmt > < stmt-list > | < empty > • This BNF generates C-like statements, like – while (a<b) { 
 c = c * a; 
 a = a + a; 
 } • This is just a toy example; the BNF grammar for a full language may include hundreds of productions Formal Language, chapter 12, slide 24 24

  25. Outline • 12.1 Context-Free Grammars and Languages • 12.2 Writing CFGs • 12.3 CFG Applications: BNF • 12.4 Parse Trees • 12.5 Ambiguity • 12.6 EBNF Formal Language, chapter 12, slide 25 25

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