plan for today and beginning next week lexical analysis
play

Plan for Today and Beginning Next week (Lexical Analysis) Regular - PowerPoint PPT Presentation

Plan for Today and Beginning Next week (Lexical Analysis) Regular Expressions Finite State Machines DFAs: Deterministic Finite Automata Complications NFAs: Non Deterministic Finite State Automata From Regular Expressions to NFAs From NFAs to


  1. Plan for Today and Beginning Next week (Lexical Analysis) Regular Expressions Finite State Machines DFAs: Deterministic Finite Automata Complications NFAs: Non Deterministic Finite State Automata From Regular Expressions to NFAs From NFAs to DFAs CS453 Lecture Regular Expressions and Transition Diagrams 1

  2. Structure of a Typical Compiler Analysis Synthesis character stream lexical analysis IR code generation tokens “ words ” IR syntactic analysis optimization AST “ sentences ” IR semantic analysis code generation annotated AST target language interpreter CS453 Lecture Regular Expressions and Transition Diagrams 2

  3. Tokens for Example MeggyJava program import meggy.Meggy; import meggy.Meggy; class PA3Flower { class PA3Flower { public static void main(String[] whatever){ public static void main(String[] whatever){ { // Upper left petal, clockwise // Upper left petal, clockwise Meggy.setPixel( (byte)2, (byte)4, Meggy.Color.VIOLET ); Meggy.setPixel( (byte)2, (byte)4, Meggy.Color.VIOLET ); Meggy.setPixel( (byte)2, (byte)1, Meggy.Color.VIOLET); Meggy.setPixel( (byte)2, (byte)1, Meggy.Color.VIOLET); … … } } Tokens: Tokens: Symbol(IMPORT,null), Symbol(MEGGY,null), Symbol(SEMI,null), Symbol(CLASS,null), Symbol(ID, ” PA3Flower ” ), Symbol(LBRACE,null), … CS453 Lecture Regular Expressions and Transition Diagrams 3

  4. About The Slides on Languages and Finite Automata Slides Originally Developed by Prof. Costas Busch (2004) – Many thanks to Prof. Busch for developing the original slide set. Adapted with permission by Prof. Dan Massey (Spring 2007) – Subsequent modifications, many thanks to Prof. Massey for CS 301 slides Adapted with permission by Prof. Michelle Strout (Spring 2011) – Adapted for use in CS 453 – Adapted by Wim Bohm( added regular expr à à NFA à à DFA, Spr2012)

  5. Languages A language is a set of strings (sometimes called sentences) String: A finite sequence of letters Examples: “ cat ” , “ dog ” , “ house ” , … Defined over a fixed alphabet: a , b , c , … , z { } Σ =

  6. Empty String A string with no letters: ε (sometimes λ is used) ε = 0 Observations: ε w = w ε = w ε abba = abba ε = abba

  7. Regular Expressions Regular expressions describe regular languages You have probably seen them in OSs / editors Example: ( a |( b )( c ))* describes the language { } L (( a |( b )( c ))*) = ε , a , bc , aa , abc , bca ,...

  8. Recursive Definition for Specifying Regular Expressions Primitive regular expressions: ∅ , ε , α where α ∈ Σ , somealphabet r r Given regular expressions and 1 2 r 1 | r 2 r 1 r 2 Are regular expressions r 1 * ( ) r 1

  9. Regular operators choice: A | B a string from L(A) or from L(B) concatenation: A B a string from L(A) followed by a string from L(B) repetition: A* 0 or more concatenations of strings from L(A) A + 1 or more grouping: ( A ) Concatenation has precedence over choice: A|B C vs. (A|B)C More syntactic sugar, used in scanner generators: [abc] means a or b or c [\t\n ] means tab, newline, or space [a-z] means a,b,c, …, or z CS453 Lecture Regular Expressions and Transition Diagrams 9

  10. Example Regular Expressions and Regular Definitions Regular definition: name : regular expression name can then be used in other regular expressions Keywords “ print ” , “ while ” Operations: “ + ” , “ - ” , “ * ” Identifiers: let : [a-zA-Z] // chose from a to z or A to Z dig : [0-9] id : let (let | dig)* Numbers: dig + = dig dig* CS453 Lecture Regular Expressions and Transition Diagrams 10

  11. Finite Automaton Input String Output Finite String Automaton

  12. Finite Accepter Input String Output “ Accept ” Finite or Automaton “ Reject ”

  13. State Transition Graph a , b abba -Finite Accepter q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4 initial final state state transition “ accept ” state

  14. Initial Configuration Input String a b b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  15. Reading the Input a b b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  16. a b b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  17. a b b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  18. a b b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  19. Input finished a b b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4 Output: “ accept ”

  20. String Rejection a b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  21. a b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  22. a b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  23. a b a a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  24. Input finished a b a a , b Output: q “ reject ” 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  25. The Empty String ε a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  26. ε a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4 Output: Would it be possible to accept the empty string? “ reject ”

  27. Another Example a a b a , b a a , b b q q q 0 1 2

  28. a a b a , b a a , b b q q q 0 1 2

  29. a a b a , b a a , b b q q q 0 1 2

  30. a a b a , b a a , b b q q q 0 1 2

  31. Input finished a a b a , b a Output: “ accept ” a , b b q q q 0 1 2

  32. Rejection a b b a , b a a , b b q q q 0 1 2

  33. a b b a , b a a , b b q q q 0 1 2

  34. a b b a , b a a , b b q q q 0 1 2

  35. a b b a , b a a , b b q q q 0 1 2

  36. Input finished a b b Which strings are accepted? a , b a a , b b q q q 0 1 2 Output: “ reject ”

  37. Formalities Deterministic Finite Automaton (DFA) M Q , , , q , F ( ) = Σ δ 0 Q : set of states : input alphabet Σ : transition function δ q : initial state 0 : set of final (accepting) states F

  38. Σ Input Alphabet a , b { } Σ = a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  39. Q Set of States Q = q , q , q , q , q , q { } 0 1 2 3 4 5 a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  40. q Initial State 0 a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  41. F Set of Final States F = q { } 4 a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  42. δ Transition Function : Q Q δ × Σ → a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  43. q 0 , a q ( ) δ = 1 a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  44. q 0 , b q ( ) δ = 5 a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  45. q 2 , b q ( ) δ = 3 a , b q 5 a , b a b a b a a b b q q q q q 0 1 2 3 4

  46. δ Transition Function / table a b δ q q q 0 1 5 q q q 1 5 2 q q a , b q 2 3 5 q q q 3 5 4 q q q q 5 5 5 4 a , b q q q a b a 5 5 5 b a a b b q q q q q 0 1 2 3 4

  47. Complications 1. "1234" is an NUMBER but what about the “ 123 ” in “ 1234 ” or the “ 23 ” , etc. Also, the scanner must recognize many tokens, not one, only stopping at end of file. 3. "if" is a keyword or reserved word IF, but "if" is also defined by the reg. exp. for identifier ID. We want to recognize IF. 4. We want to discard white space and comments. 5. "123" is a NUMBER but so is "235" and so is "0", just as "a" is an ID and so is "bcd ” , we want to recognize a token, but add attributes to it. CS453 Lecture Regular Expressions and Transition Diagrams 47

  48. Complications 1 1. "1234" is an NUMBER but what about the “ 123 ” in “ 1234 ” or the “ 23 ” , etc. Also, the scanner must recognize many tokens, not one, only stopping at end of file. So: recognize the largest string defined by some regular expression, only stop getting more input if there is no more match. This introduces the need to reconsider a character, as it is the first of the next token e.g. fname(a,bcd ); would be scanned as ID OPEN ID COMMA ID CLOSE SEMI EOF scanning fname would consume (, which would be put back and then recognized as OPEN CS453 Lecture Regular Expressions and Transition Diagrams 48

  49. Complication 2 2. "if" is a keyword or reserved word IF, but "if" is also defined by the reg. exp. for identifier ID, we want to recognize IF, so Have some way of determining which token ( IF or ID ) is recognized. This can be done using priority , e.g. in scanner generators an earlier definition has a higher priority than a later one. By putting the definition for IF before the definition for ID in the input for the scanner generator, we get the desired result. What about the string “ ifyouleavemenow ” ? CS453 Lecture Regular Expressions and Transition Diagrams 49

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