csci 3136 principles of programming languages
play

CSCI 3136 Principles of Programming Languages Lexical Analysis and - PowerPoint PPT Presentation

CSCI 3136 Principles of Programming Languages Lexical Analysis and Automata Theory - 1 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 78 Phases of Compilation Character Stream Scanner (lexical analysis) Token Stream


  1. CSCI 3136 Principles of Programming Languages Lexical Analysis and Automata Theory - 1 Summer 2013 Faculty of Computer Science Dalhousie University 1 / 78

  2. Phases of Compilation Character Stream Scanner (lexical analysis) Token Stream Parser (syntactic analysis) Parse Tree Symbol Table Semantic Analysis and Inter- mediate Code Generation Abstract Syntax Tree or Other Intermediate Form Machine-independent Code Improvement (Optional) Modified Intermediate Form Target Code Generation Target Language (e.g., assembly) Machine-specific Code Im- provement (Optional) Modified Target Language 2 / 78

  3. Parser (PDA) recognizes Context-free Grammar are gen- erated by Context-free Language

  4. Scanner Parser (DFA) (PDA) recognizes recognizes Regular Expression Context-free Grammar are gen- are gen- erated erated by by Regular Language Context-free Language

  5. Scanner Parser (DFA) (PDA) recognizes recognizes Regular Expression Context-free Grammar are gen- are gen- erated erated by by Regular Language Context-free Language

  6. Scanner Parser (DFA) (PDA) recognizes recognizes Regular Expression Context-free Grammar are gen- are gen- erated erated by by Regular Language Context-free Language Formal Language (a set of strings)

  7. Scanner Parser (DFA) (PDA) recognizes recognizes Regular Expression Context-free Grammar are gen- are gen- erated erated by by Regular Language Context-free Language

  8. Scanner Parser (DFA) (PDA) recognizes recognizes Regular Expression Context-free Grammar are gen- are gen- erated erated by by Regular Language Context-free Language

  9. Scanner Parser (DFA) (PDA) recognizes recognizes Rules: 1. Concatenation Regular Expression Context-free Grammar 2. Alternation 3. Kleene closure are gen- are gen- erated erated by by Regular Language Context-free Language

  10. Scanner Parser (DFA) (PDA) recognizes recognizes Regular Expression Context-free Grammar are gen- are gen- erated erated by by Regular Language Context-free Language

  11. Scanner Parser (DFA) (PDA) recognizes recognizes Regular Expression Context-free Grammar are gen- are gen- erated erated by by Regular Language Context-free Language

  12. Scanner Parser (DFA) (PDA) recognizes recognizes Rules: 1. Concatenation Regular Expression Context-free Grammar 2. Alternation 3. Kleene closure are gen- are gen- erated erated by by Regular Language Context-free Language

  13. Scanner Parser (DFA) (PDA) recognizes recognizes Rules: 1. Concatenation Regular Expression 2. Alternation Context-free Grammar 3. Kleene closure 4. Recursion are gen- are gen- erated erated by by Regular Language Context-free Language 14 / 78

  14. Formal Languages A formal language L is a set of strings over an alphabet Σ. 15 / 78

  15. Formal Languages A formal language L is a set of strings over an alphabet Σ. • Alphabet Σ: set of characters that can be used to form strings (letters, digits, punctuation, . . . ) 16 / 78

  16. Formal Languages A formal language L is a set of strings over an alphabet Σ. • Alphabet Σ: set of characters that can be used to form strings (letters, digits, punctuation, . . . ) • String : finite sequence of characters 17 / 78

  17. Formal Languages A formal language L is a set of strings over an alphabet Σ. • Alphabet Σ: set of characters that can be used to form strings (letters, digits, punctuation, . . . ) • String : finite sequence of characters • ǫ denotes the empty string (string with no letters: “ ′′ ) 18 / 78

  18. Formal Languages A formal language L is a set of strings over an alphabet Σ. • Alphabet Σ: set of characters that can be used to form strings (letters, digits, punctuation, . . . ) • String : finite sequence of characters • ǫ denotes the empty string (string with no letters: “ ′′ ) • Length | s | of a string s = number of characters in s | ǫ | = 0, | a | = 1, | abc | = 3, . . . 19 / 78

  19. Formal Languages A formal language L is a set of strings over an alphabet Σ. • Alphabet Σ: set of characters that can be used to form strings (letters, digits, punctuation, . . . ) • String : finite sequence of characters • ǫ denotes the empty string (string with no letters: “ ′′ ) • Length | s | of a string s = number of characters in s | ǫ | = 0, | a | = 1, | abc | = 3, . . . • Σ 0 = set of strings of length 0: Σ 0 = { ǫ } Σ 1 = set of strings of length 1: Σ 1 = { a, b, c, ... } Σ 2 = set of strings of length 2: Σ 2 = { aa, ab,..., ca,... } . . . 20 / 78

  20. Formal Languages A formal language L is a set of strings over an alphabet Σ. • Alphabet Σ: set of characters that can be used to form strings (letters, digits, punctuation, . . . ) • String : finite sequence of characters • ǫ denotes the empty string (string with no letters: “ ′′ ) • Length | s | of a string s = number of characters in s | ǫ | = 0, | a | = 1, | abc | = 3, . . . • Σ 0 = set of strings of length 0: Σ 0 = { ǫ } Σ 1 = set of strings of length 1: Σ 1 = { a, b, c, ... } Σ 2 = set of strings of length 2: Σ 2 = { aa, ab,..., ca,... } . . . • Kleene star : Σ ∗ = Σ 0 ∪ Σ 1 ∪ Σ 2 ∪ . . . (set of all strings over alphabet Σ) 21 / 78

  21. Formal Languages A formal language L is a set of strings over an alphabet Σ. • Alphabet Σ: set of characters that can be used to form strings (letters, digits, punctuation, . . . ) • String : finite sequence of characters • ǫ denotes the empty string (string with no letters: “ ′′ ) • Length | s | of a string s = number of characters in s | ǫ | = 0, | a | = 1, | abc | = 3, . . . • Σ 0 = set of strings of length 0: Σ 0 = { ǫ } Σ 1 = set of strings of length 1: Σ 1 = { a, b, c, ... } Σ 2 = set of strings of length 2: Σ 2 = { aa, ab,..., ca,... } . . . • Kleene star : Σ ∗ = Σ 0 ∪ Σ 1 ∪ Σ 2 ∪ . . . (set of all strings over alphabet Σ) • A formal language L over alphabet Σ is a subset L ⊆ Σ ∗ 22 / 78

  22. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } 23 / 78

  23. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation 24 / 78

  24. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation ab 25 / 78

  25. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation • Alternation ( | ) ab 26 / 78

  26. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation • Alternation ( | ) ab|c 27 / 78

  27. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation • Alternation ( | ) • Kleene star (*) ab|c 28 / 78

  28. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation • Alternation ( | ) • Kleene star (*) a ∗ b|c 29 / 78

  29. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation • Alternation ( | ) • Kleene star (*) • Parentheses a ∗ b|c 30 / 78

  30. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation • Alternation ( | ) • Kleene star (*) • Parentheses a ∗ (b|c) 31 / 78

  31. Regular Expressions (RegEx) Alphabet Σ = { a, b, c } • Concatenation • Alternation ( | ) • Kleene star (*) • Parentheses a ∗ (b|c) 32 / 78

  32. RegEx Operator Precedence 33 / 78

  33. RegEx Operator Precedence • Parentheses 34 / 78

  34. RegEx Operator Precedence • Parentheses a ∗ (b | c)a ∗ | a 35 / 78

  35. RegEx Operator Precedence • Parentheses a ∗ (b | c)a ∗ | a 36 / 78

  36. RegEx Operator Precedence • Parentheses • Kleene star (*) a ∗ (b | c)a ∗ | a 37 / 78

  37. RegEx Operator Precedence • Parentheses • Kleene star (*) a ∗ (b | c)a ∗ | a 38 / 78

  38. RegEx Operator Precedence • Parentheses • Kleene star (*) • Concatenation a ∗ (b | c)a ∗ | a 39 / 78

  39. RegEx Operator Precedence • Parentheses • Kleene star (*) • Concatenation a ∗ (b | c)a ∗ | a 40 / 78

  40. RegEx Operator Precedence • Parentheses • Kleene star (*) • Concatenation • Alternation ( | ) a ∗ (b | c)a ∗ | a 41 / 78

  41. RegEx Operator Precedence • Parentheses • Kleene star (*) • Concatenation • Alternation ( | ) a ∗ (b | c)a ∗ | a 42 / 78

  42. Examples of Regular Expressions-1 Σ = { 0, 1 } 43 / 78

  43. Examples of Regular Expressions-1 Σ = { 0, 1 } • 01 ∗ 44 / 78

  44. Examples of Regular Expressions-1 Σ = { 0, 1 } • 01 ∗ 0 followed by any number of 1s (i.e., { 0, 01, 011, 0111, . . . } ) 45 / 78

  45. Examples of Regular Expressions-1 Σ = { 0, 1 } • 01 ∗ 0 followed by any number of 1s (i.e., { 0, 01, 011, 0111, . . . } ) • (01 ∗ )(01) 46 / 78

  46. Examples of Regular Expressions-1 Σ = { 0, 1 } • 01 ∗ 0 followed by any number of 1s (i.e., { 0, 01, 011, 0111, . . . } ) • (01 ∗ )(01) 0 followed by any number of 1s then 01 (i.e., { 001, 0101, 01101, 011101, . . . } ) 47 / 78

  47. Examples of Regular Expressions-1 Σ = { 0, 1 } • 01 ∗ 0 followed by any number of 1s (i.e., { 0, 01, 011, 0111, . . . } ) • (01 ∗ )(01) 0 followed by any number of 1s then 01 (i.e., { 001, 0101, 01101, 011101, . . . } ) • (0|1) ∗ 0 48 / 78

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