Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev

Size: px
Start display at page:

Download "Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev"

Transcription

1 Fll Compiler Principles Lecture 1: Lexicl Anlysis Romn Mnevich Ben-Gurion University of the Negev

2 Agend Understnd role of lexicl nlysis in compiler Regulr lnguges reminder Lexicl nlysis lgorithms Scnner genertion 2

3 Jvscript exmple Cn you some identify sic units in this code? vr curroption = 0; // Choose content to disply in lower pne. function choose ( id ) { vr menu = ["out-me", "pulictions", "teching", "softwre", "ctivities"]; } for (i = 0; i < menu.length; i++) { curroption = menu[i]; vr elt = document.getelementbyid(curroption); if (curroption == id && elt.style.disply == "none") { elt.style.disply = "lock"; } else { elt.style.disply = "none"; } } 3

4 Jvscript exmple Cn you some identify sic units in this code? keyword????? vr curroption = 0; // Choose content to disply in lower pne. function choose ( id ) { vr menu = ["out-me", "pulictions", "teching", "softwre", "ctivities"]; } for (i = 0; i < menu.length; i++) { curroption = menu[i]; vr elt = document.getelementbyid(curroption); if (curroption == id && elt.style.disply == "none") { elt.style.disply = "lock"; } else { elt.style.disply = "none"; } }?? 4

5 Jvscript exmple Cn you some identify sic units in this code? keyword whitespce identifier opertor numeric literl punctution vr curroption = 0; // Choose content to disply in lower pne. function choose ( id ) { vr menu = ["out-me", "pulictions", "teching", "softwre", "ctivities"]; } comment string literl for (i = 0; i < menu.length; i++) { curroption = menu[i]; vr elt = document.getelementbyid(curroption); if (curroption == id && elt.style.disply == "none") { elt.style.disply = "lock"; } else { elt.style.disply = "none"; } } 5

6 Role of lexicl nlysis First prt of compiler front-end High-level Lnguge Lexicl Anlysis Syntx Anlysis Prsing AST Symol Tle etc. Inter. Rep. (IR) Code Genertion Executle Code (scheme) Convert strem of chrcters into strem of tokens Split text into most sic meningful strings Simplify input for syntx nlysis 6

7 From scnning to prsing progrm text 59 + (1257 * xposition) Lexicl Anlyzer Lexicl error vlid token strem num + ( num * id ) Grmmr: E id E num E E + E E E * E E ( E ) syntx error + Prser vlid Astrct Syntx Tree num * num x 7

8 Scnner output where is the white spce? where is the white spce? vr curroption = 0; // Choose content to disply in lower pne. function choose ( id ) { vr menu = ["out-me", "pulictions, "teching", "softwre", "ctivities"]; } for (i = 0; i < menu.length; i++) { curroption = menu[i]; vr elt = document.getelementbyid(curroption); if (curroption == id && elt.style.disply == "none") { elt.style.disply = "lock"; } else { elt.style.disply = "none"; } } Strem of Tokens LINE: ID(vlue) 1: VAR 1: ID(currOption) 1: EQ 1: INT_LITERAL(0) 1: SEMI 3: FUNCTION 3: ID(choose) 3: LP 3: ID(id) 3: EP 3: LCB... 8

9 Tokens 9

10 Wht is token? Lexeme sustring of originl text constituting n identifile unit Identifiers, vlues, reserved words, Record type storing: Kind Vlue (when pplicle) Strt-position/end-position Any informtion tht is useful for the prser Different for different lnguges 10

11 Exmple tokens Type Identifier Exmples x, y, z, foo, r NUM 42 FLOATNUM STRING so long, nd thnks for ll the fish LPAREN ( RPAREN ) IF if 11

12 C++ exmple 1 Splitting text into tokens cn e tricky How should the code elow e split? vector<vector<int>> myvector >> opertor >, > or two tokens? 12

13 C++ exmple 2 Splitting text into tokens cn e tricky How should the code elow e split? vector<vector<int> > myvector >, > two tokens 13

14 Seprting tokens Type Exmples Comments /* ignore code */ // ignore until end of line White spces \t \n Lexemes tht re recognized ut get consumed rther thn trnsmitted to prser if i f i/*comment*/f 14

15 Preprocessor directives in C Type Include directives Exmples #include<foo.h> Mcros #define THE_ANSWER 42 15

16 First step of designing scnner Define ech type of lexeme Reserved words: vr, if, for, while Opertors: < = ++ Identifiers: myfunction Literls: 123 hello How cn we define lexemes of unounded length? 16

17 First step of designing scnner Define ech type of lexeme Reserved words: vr, if, for, while Opertors: < = ++ Identifiers: myfunction Literls: 123 hello How cn we define lexemes of unounded length? Regulr expressions 17

18 Agend Understnd role of lexicl nlysis in compiler Convert text to strem of tokens Regulr lnguges reminder Lexicl nlysis lgorithms Scnner genertion 18

19 Regulr lnguges reminder 19

20 Bsic definitions nd fcts Forml lnguges Alphet = finite set of letters Word = sequence of letter Lnguge = set of words Regulr lnguges defined equivlently y Regulr expressions Finite-stte utomt 20

21 Regulr expressions Empty string: Є Letter: 1,, k Alphet Conctention: R 1 R 2 Union: R 1 R 2 Kleene-str: R* Shorthnd: R + stnds for R R* scope: (R) Exmple: (0* 1*) (1* 0*) Wht is this lnguge? 21

22 Exercise 1 - Question Lnguge of Jv identifiers Identifiers strt with either n underscore _ or letter Continue with either underscore, letter, or digit 22

23 Exercise 1 - Answer Lnguge of Jv identifiers Identifiers strt with either n underscore _ or letter Continue with either underscore, letter, or digit (_ z A Z)(_ z A Z 0 9)* 23

24 Exercise 1 Better nswer Lnguge of Jv identifiers Identifiers strt with either n underscore _ or letter Continue with either underscore, letter, or digit (_ z A Z)(_ z A Z 0 9)* Using shorthnd mcros First = _ z A Z Next = First 0 9 R = First Next* 24

25 Exercise 2 - Question Lnguge of rtionl numers in deciml representtion (no leding, ending zeros) Positive exmples: Negtive exmples:

26 Exercise 2 - Answer Lnguge of rtionl numers in deciml representtion (no leding, ending zeros) Digit = Digit0 = 0 Digit Num = Digit Digit0* Frc = Digit0* Digit Pos = Num.Frc 0.Frc Num.Frc PosOrNeg = (Є -)Pos R = 0 PosOrNeg 26

27 Exercise 3 - Question Equl numer of opening nd closing prenthesis: [ n ] n = [], [[]], [[[]]], 27

28 Exercise 3 - Answer Equl numer of opening nd closing prenthesis: [ n ] n = [], [[]], [[[]]], Not regulr Context-free Grmmr: S ::= [] [S] 28

29 Finite utomt 29

30 Finite utomt: known results Types of finite utomt: Deterministic (DFA) Non-deterministic (NFA) Non-deterministic + epsilon trnsitions Theorem: trnsltion of regulr expressions to NFA+epsilon (liner time) Theorem: trnsltion of NFA+epsilon to DFA Worst-cse exponentil time Theorem [Myhill-Nerode]: DFA cn e minimized 30

31 Finite utomt An utomton M = Q,,, q 0, F is defined y sttes nd trnsitions trnsition ccepting stte c strt strt stte 31

32 Exercise - Question Wht is the lnguge defined y the utomton elow? c strt 32

33 Exercise - Answer Wht is the lnguge defined y the utomton elow * c? Generlly: ll pths leding to ccepting sttes c strt 33

34 Non-deterministic utomt Allow multiple trnsitions from given stte leled y sme letter c strt c 34

35 NFA+Є utomt Є trnsitions cn fire without reding the input strt c Є 35

36 A little out me Joined Ben-Gurion University in 2012 Reserch interests Inductive progrmming nd synthesis Sttic nlysis nd verifiction Lnguge-supported prllelism 36

37 I m here for Teching you theory nd prctice of populr compiler lgorithms Hopefully mke you think out solving prolems y exmples from the compilers world Answering questions out mteril Contcting me e-mil: romnm@cs.gu.c.il Office hours: see course we-pge Announcements Forums (per ssignment) 37

38 Tenttive syllus Front End Intermedite Representtion Optimiztions Code Genertion Scnning Opertionl Semntics Dtflow Anlysis Register Alloction Top-down Prsing (LL) Lowering Loop Optimiztions Energy Optimiztion Bottom-up Prsing (LR) Instruction Selection mid-term exm 38

39 Reg-exp vs. utomt Regulr expressions re declrtive Offer compct wy to define regulr lnguge y humns Don t offer direct wy to check whether given word is in the lnguge Automt re opertive Define n lgorithm for deciding whether given word is in regulr lnguge Not nturl nottion for humns A high-level lnguge A mchine lnguge 39

40 From Regulr expressions to utomt 40

41 From reg. exp. to NFA+Є utomt Theorem: there is n lgorithm to uild n NFA+Є utomton for ny regulr expression Proof: y induction on the structure of the regulr expression strt 41

42 Inductive constructions R = strt R 1 R 2 R 1 R = strt strt R 2 R * strt R R 1 R 2 strt R 1 R 2 42

43 Running time of NFA+Є Construction requires O(k) sttes for reg-exp of length k Running n NFA+Є with k sttes on string of length n tkes O(n k 2 ) time Cn we reduce the k 2 fctor? Ech stte in configurtion of O(k) sttes my hve O(k) outgoing edges, so processing n input letter my tke O(k 2 ) time 43

44 From NFA+Є to DFA Construction requires O(k) sttes for reg-exp of length k Running n NFA+Є with k sttes on string of length n tkes O(n k 2 ) time Cn we reduce the k 2 fctor? Theorem: for ny NFA+Є utomton there exists n equivlent deterministic utomton Proof: determiniztion vi suset construction Numer of sttes in the worst-cse O(2 k ) Running time O(n) 44

45 Recp We know how to define ny single type of lexeme We know how to convert ny regulr expression into recognizing utomton But how do we use this for scnning? 45

46 The forml scnning prolem 46

47 Wht is scnner Lexicl Specifiction: List of regulr expressions (one per lexeme) vr curroption = 0; // Choose content function choose ( id ) {... R 1 R k Scnner Strem of Tokens LINE: ID(vlue) 1: VAR 1: ID(currOption) 1: EQ 1: INT_LITERAL(0) 1: SEMI... 47

48 Scnning prolem Input: Lexicl specifiction: R 1,, R k (regulr expressions, one per lexeme) input: string of n chrcters Output: sequence of tokens R 1 (lex 1 ) R n (lex n ) such tht The lexemes prtition the input lex 1 lex n = input R 1 R n mtch the lexeme type from the specifiction 48

49 Exmple 1: prtitioning ID = ( z) ( z)* ONE = 1 Input: 1 Wht should the output e? 1. ID() ID() ID() ONE 2. ID() ID() ONE 3. ID() ID() ONE 4. ID() ONE First mtch semntics Mximl munch semntics 49

50 Mximl munch semntics ID = ( z) ( z)* ONE = 1 Input: 1 How do we return ID() ONE? Solution: find longest mtching lexeme Automton my enter nd leve ccepting stte mny times efore longest mtch is found Intuition: some tokens, such s identifiers re prefix-closed 50

51 Exmple 2: hndling miguities ID = ( z) ( z)* IF = if Input: if Mtches oth tokens Wht should the scnner output e? -z strt q0 -z\i i ID -z\f -z DFA ID f ID, IF 51

52 Solution: precedence semntics ID = ( z) ( z)* IF = if Input: if Mtches oth tokens Wht should the scnner output e? Brek tie using order of definitions Output: ID(if) strt q0 -z\i i ID ID -z\f f -z -z ID, IF DFA 52

53 Solution: precedence semntics IF = if ID = ( z) ( z)* Input: if Mtches oth tokens Wht should the scnner output e? Brek tie using order of definitions Output: IF strt q0 -z\i i ID ID -z\f f Conclusion: list keyword token definitions efore identifier definition -z -z IF, ID DFA 53

54 Putting together n lgorithm 54

55 Overll lgorithm structure List of regulr expressions (one per lexeme) R 1 R k High-level intermedite representtion minimiztion Medium-level intermedite representtion Crucil: Assign semntics NFA+Є R 1 R k DFA for R 1 R k How do we implement mximl munch? Scnner implementtion (efficient dt structures) 55

56 A First mtch lgorithm 56

57 Suggestions? First mtch lgorithm Wht is the complexity? 57

58 A Mximl munch lgorithm 58

59 Mximl munch scnning lgorithm Input: input: string of n chrcters M: DFA for union of tokens Output: positions of in input tht re the finl chrcters of ech token Dt: Stck of stte, index of sttes nd their positions encountered since lst ccepting stte i: index of next chrcter in input q: current stte or Bottom (no stte) 59

60 Mximl munch pseudo-code Reset DFA to look for next token Used to indicte n error sitution (no token is found) 60

61 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 1 B,1 Output = 61

62 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 1 B,1 q1 2 B,1 q0,1 Output = 62

63 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 1 B,1 q1 2 B,1 q0,1 q3 3 q1,2 Output = 63

64 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 1 B,1 q1 2 B,1 q0,1 q3 3 q1,2 q3 4 q1,2 q3,3 Output = 64

65 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 1 B,1 q1 2 B,1 q0,1 q3 3 q1,2 q3 4 q1,2 q3,3 q3 3 q1,2 Output = 65

66 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 1 B,1 q1 2 B,1 q0,1 q3 3 q1,2 q3 4 q1,2 q3,3 q3 3 q1,2 q1 2 Output = 66

67 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 1 B,1 q1 2 B,1 q0,1 q3 3 q1,2 q3 4 q1,2 q3,3 q3 3 q1,2 q1 2 Output = 1 67

68 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 2 B,2 Output = 1 68

69 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 2 B,2 q1 3 B,2 q0,2 Output = 1 69

70 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 2 B,2 q1 3 B,2 q3 4 q1,3 Output = 1 70

71 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 2 B,2 q1 3 B,2 q3 4 q1,3 Output = 1 71

72 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 2 B,2 q1 3 B,2 q3 4 q1,3 q1 3 Output = 1 72

73 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 2 B,2 q1 3 B,2 q3 4 q1,3 q1 3 Output =

74 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 3 B,3 Output =

75 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 3 B,3 q1 4 B,3 q0,3 Output =

76 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 3 B,3 q1 4 B,3 q0,3 Output =

77 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 3 B,3 q1 4 B,3 q0,3 Output =

78 Mximl munch run exmple Assume R 1 = input = q0 q1 R 2 = * q3 q2 q i stck q0 3 B,3 q1 4 B,3 q0,3 Output =

79 Complexity of mximl munch Wht is the complexity of tokenizing text of n chrcters y mtching longest tokens? 79

80 Complexity of mximl munch Wht is the complexity of tokenizing text of n chrcters y mtching longest tokens? Assume the following token clsses R 1 = R 2 = * For input= n it is O(n 2 ) Cn we improve the worst-cse complexity? q q q q n n 80

81 Improved scnning lgorithm Ide: use work done on leftover stck to improve future decisions Rememer for ech index which sttes hve filed cnnot e extended to token Mximl-Munch Tokeniztion in Liner Time Tom Reps [TOPLAS 1998] 81

82 Improved lgorithm pseudo-code Wht is the running time? How mny times cn this test fil for given index? 82

83 Agend Understnd role of lexicl nlysis in compiler Convert text to strem of tokens Regulr lnguges reminder Lexicl nlysis lgorithms Precedence + First mtch Precedence + Mximl munch Scnner genertion 83

84 Implementing scnner 84

85 Implementing modern scnners Mnul construction of utomt + determiniztion + mximl munch + tie reking Very tedious Error-prone Non-incrementl Fortuntely there re tools tht utomticlly generte roust code from specifiction for most lnguges C: Lex, Flex Jv: JLex, JFlex 85

86 Using JFlex Define tokens (nd sttes) Run JFlex to generte Jv implementtion Usully MyScnner.nextToken() will e clled in loop y prser MyScnner.lex Strem of chrcters Lexicl Specifiction JFlex MyScnner.jv Tokens 86

87 Filtering illegl comintions Which tokens should the scnner return for 123foo? 87

88 Filtering illegl comintions Which tokens should the scnner return for 123foo? We sometimes wnt to rule out certin token conctentions prior to prsing How cn we do tht with wht we ve seen so fr? 88

89 Filtering illegl comintions Which tokens should the scnner return for 123foo? We sometimes wnt to rule out certin token conctentions prior to prsing How cn we do tht with wht we ve seen so fr? Define error lexemes 89

90 Ctching errors Wht if input doesn t mtch ny token definition? Wnt to grcefully signl n error Trick: dd ctch-ll rule tht mtches ny chrcter nd reports n error Add fter ll other rules 90

91 Next lecture: prsing

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University Fll 2014-2015 Compiler Principles Lecture 1: Lexicl Anlysis Romn Mnevich Ben-Gurion University Agend Understnd role of lexicl nlysis in compiler Lexicl nlysis theory Implementing professionl scnner vi

More information

Dr. D.M. Akbar Hussain

Dr. D.M. Akbar Hussain Dr. D.M. Akr Hussin Lexicl Anlysis. Bsic Ide: Red the source code nd generte tokens, it is similr wht humns will do to red in; just tking on the input nd reking it down in pieces. Ech token is sequence

More information

Compilation

Compilation Compiltion 0368-3133 Lecture 2: Lexicl Anlysis Nom Rinetzky 1 2 Lexicl Anlysis Modern Compiler Design: Chpter 2.1 3 Conceptul Structure of Compiler Compiler Source text txt Frontend Semntic Representtion

More information

Topic 2: Lexing and Flexing

Topic 2: Lexing and Flexing Topic 2: Lexing nd Flexing COS 320 Compiling Techniques Princeton University Spring 2016 Lennrt Beringer 1 2 The Compiler Lexicl Anlysis Gol: rek strem of ASCII chrcters (source/input) into sequence of

More information

Fig.25: the Role of LEX

Fig.25: the Role of LEX The Lnguge for Specifying Lexicl Anlyzer We shll now study how to uild lexicl nlyzer from specifiction of tokens in the form of list of regulr expressions The discussion centers round the design of n existing

More information

In the last lecture, we discussed how valid tokens may be specified by regular expressions.

In the last lecture, we discussed how valid tokens may be specified by regular expressions. LECTURE 5 Scnning SYNTAX ANALYSIS We know from our previous lectures tht the process of verifying the syntx of the progrm is performed in two stges: Scnning: Identifying nd verifying tokens in progrm.

More information

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5

CS321 Languages and Compiler Design I. Winter 2012 Lecture 5 CS321 Lnguges nd Compiler Design I Winter 2012 Lecture 5 1 FINITE AUTOMATA A non-deterministic finite utomton (NFA) consists of: An input lphet Σ, e.g. Σ =,. A set of sttes S, e.g. S = {1, 3, 5, 7, 11,

More information

Definition of Regular Expression

Definition of Regular Expression Definition of Regulr Expression After the definition of the string nd lnguges, we re redy to descrie regulr expressions, the nottion we shll use to define the clss of lnguges known s regulr sets. Recll

More information

Lexical Analysis: Constructing a Scanner from Regular Expressions

Lexical Analysis: Constructing a Scanner from Regular Expressions Lexicl Anlysis: Constructing Scnner from Regulr Expressions Gol Show how to construct FA to recognize ny RE This Lecture Convert RE to n nondeterministic finite utomton (NFA) Use Thompson s construction

More information

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata

CS 432 Fall Mike Lam, Professor a (bc)* Regular Expressions and Finite Automata CS 432 Fll 2017 Mike Lm, Professor (c)* Regulr Expressions nd Finite Automt Compiltion Current focus "Bck end" Source code Tokens Syntx tree Mchine code chr dt[20]; int min() { flot x = 42.0; return 7;

More information

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08

CS412/413. Introduction to Compilers Tim Teitelbaum. Lecture 4: Lexical Analyzers 28 Jan 08 CS412/413 Introduction to Compilers Tim Teitelum Lecture 4: Lexicl Anlyzers 28 Jn 08 Outline DFA stte minimiztion Lexicl nlyzers Automting lexicl nlysis Jlex lexicl nlyzer genertor CS 412/413 Spring 2008

More information

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis

CS143 Handout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexical Analysis CS143 Hndout 07 Summer 2011 June 24 th, 2011 Written Set 1: Lexicl Anlysis In this first written ssignment, you'll get the chnce to ply round with the vrious constructions tht come up when doing lexicl

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών ΕΠΛ323 - Θωρία και Πρακτική Μταγλωττιστών Lecture 3 Lexicl Anlysis Elis Athnsopoulos elisthn@cs.ucy.c.cy Recognition of Tokens if expressions nd reltionl opertors if è if then è then else è else relop

More information

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) *

Languages. L((a (b)(c))*) = { ε,a,bc,aa,abc,bca,... } εw = wε = w. εabba = abbaε = abba. (a (b)(c)) * Pln for Tody nd Beginning Next week Interpreter nd Compiler Structure, or Softwre Architecture Overview of Progrmming Assignments The MeggyJv compiler we will e uilding. Regulr Expressions Finite Stte

More information

CS 430 Spring Mike Lam, Professor. Parsing

CS 430 Spring Mike Lam, Professor. Parsing CS 430 Spring 2015 Mike Lm, Professor Prsing Syntx Anlysis We cn now formlly descrie lnguge's syntx Using regulr expressions nd BNF grmmrs How does tht help us? Syntx Anlysis We cn now formlly descrie

More information

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing

Some Thoughts on Grad School. Undergraduate Compilers Review and Intro to MJC. Structure of a Typical Compiler. Lexing and Parsing Undergrdute Compilers Review nd Intro to MJC Announcements Miling list is in full swing Tody Some thoughts on grd school Finish prsing Semntic nlysis Visitor pttern for bstrct syntx trees Some Thoughts

More information

Lexical analysis, scanners. Construction of a scanner

Lexical analysis, scanners. Construction of a scanner Lexicl nlysis scnners (NB. Pges 4-5 re for those who need to refresh their knowledge of DFAs nd NFAs. These re not presented during the lectures) Construction of scnner Tools: stte utomt nd trnsition digrms.

More information

Compiler Construction D7011E

Compiler Construction D7011E Compiler Construction D7011E Lecture 3: Lexer genertors Viktor Leijon Slides lrgely y John Nordlnder with mteril generously provided y Mrk P. Jones. 1 Recp: Hndwritten Lexers: Don t require sophisticted

More information

COMP 423 lecture 11 Jan. 28, 2008

COMP 423 lecture 11 Jan. 28, 2008 COMP 423 lecture 11 Jn. 28, 2008 Up to now, we hve looked t how some symols in n lphet occur more frequently thn others nd how we cn sve its y using code such tht the codewords for more frequently occuring

More information

Should be done. Do Soon. Structure of a Typical Compiler. Plan for Today. Lab hours and Office hours. Quiz 1 is due tonight, was posted Tuesday night

Should be done. Do Soon. Structure of a Typical Compiler. Plan for Today. Lab hours and Office hours. Quiz 1 is due tonight, was posted Tuesday night Should e done L hours nd Office hours Sign up for the miling list t, strting to send importnt info to list http://groups.google.com/group/cs453-spring-2011 Red Ch 1 nd skim Ch 2 through 2.6, red 3.3 nd

More information

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 3b Lexical Analysis Elias Athanasopoulos ΕΠΛ323 - Θωρία και Πρακτική Μταγλωττιστών Lecture 3 Lexicl Anlysis Elis Athnsopoulos elisthn@cs.ucy.c.cy RecogniNon of Tokens if expressions nd relnonl opertors if è if then è then else è else relop è

More information

Reducing a DFA to a Minimal DFA

Reducing a DFA to a Minimal DFA Lexicl Anlysis - Prt 4 Reducing DFA to Miniml DFA Input: DFA IN Assume DFA IN never gets stuck (dd ded stte if necessry) Output: DFA MIN An equivlent DFA with the minimum numer of sttes. Hrry H. Porter,

More information

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1

Deterministic. Finite Automata. And Regular Languages. Fall 2018 Costas Busch - RPI 1 Deterministic Finite Automt And Regulr Lnguges Fll 2018 Costs Busch - RPI 1 Deterministic Finite Automton (DFA) Input Tpe String Finite Automton Output Accept or Reject Fll 2018 Costs Busch - RPI 2 Trnsition

More information

ECE 468/573 Midterm 1 September 28, 2012

ECE 468/573 Midterm 1 September 28, 2012 ECE 468/573 Midterm 1 September 28, 2012 Nme:! Purdue emil:! Plese sign the following: I ffirm tht the nswers given on this test re mine nd mine lone. I did not receive help from ny person or mteril (other

More information

Assignment 4. Due 09/18/17

Assignment 4. Due 09/18/17 Assignment 4. ue 09/18/17 1. ). Write regulr expressions tht define the strings recognized by the following finite utomt: b d b b b c c b) Write FA tht recognizes the tokens defined by the following regulr

More information

Compilers Spring 2013 PRACTICE Midterm Exam

Compilers Spring 2013 PRACTICE Midterm Exam Compilers Spring 2013 PRACTICE Midterm Exm This is full length prctice midterm exm. If you wnt to tke it t exm pce, give yourself 7 minutes to tke the entire test. Just like the rel exm, ech question hs

More information

Lexical Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Lexical Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay Lexicl Anlysis Amith Snyl (www.cse.iit.c.in/ s) Deprtment of Computer Science nd Engineering, Indin Institute of Technology, Bomy Septemer 27 College of Engineering, Pune Lexicl Anlysis: 2/6 Recp The input

More information

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona CSc 453 Compilers nd Systems Softwre 4 : Lexicl Anlysis II Deprtment of Computer Science University of Arizon collerg@gmil.com Copyright c 2009 Christin Collerg Implementing Automt NFAs nd DFAs cn e hrd-coded

More information

CSE 401 Midterm Exam 11/5/10 Sample Solution

CSE 401 Midterm Exam 11/5/10 Sample Solution Question 1. egulr expressions (20 points) In the Ad Progrmming lnguge n integer constnt contins one or more digits, but it my lso contin embedded underscores. Any underscores must be preceded nd followed

More information

Lexical Analysis and Lexical Analyzer Generators

Lexical Analysis and Lexical Analyzer Generators 1 Lexicl Anlysis nd Lexicl Anlyzer Genertors Chpter 3 COP5621 Compiler Construction Copyright Roert vn Engelen, Florid Stte University, 2007-2009 2 The Reson Why Lexicl Anlysis is Seprte Phse Simplifies

More information

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an

Scanner Termination. Multi Character Lookahead. to its physical end. Most parsers require an end of file token. Lex and Jlex automatically create an Scnner Termintion A scnner reds input chrcters nd prtitions them into tokens. Wht hppens when the end of the input file is reched? It my be useful to crete n Eof pseudo-chrcter when this occurs. In Jv,

More information

CMPT 379 Compilers. Lexical Analysis

CMPT 379 Compilers. Lexical Analysis CMPT 379 Compilers Anoop Srkr http://www.cs.sfu.c/~noop 9//7 Lexicl Anlysis Also clled scnning, tke input progrm string nd convert into tokens Exmple: T_DOUBLE ( doule ) T_IDENT ( f ) T_OP ( = ) doule

More information

CMPSC 470: Compiler Construction

CMPSC 470: Compiler Construction CMPSC 47: Compiler Construction Plese complete the following: Midterm (Type A) Nme Instruction: Mke sure you hve ll pges including this cover nd lnk pge t the end. Answer ech question in the spce provided.

More information

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2014 Dec 11 th /13 th Final Exam Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string. CS 340, Fll 2014 Dec 11 th /13 th Finl Exm Nme: Note: in ll questions, the specil symol ɛ (epsilon) is used to indicte the empty string. Question 1. [5 points] Consider the following regulr expression;

More information

Theory of Computation CSE 105

Theory of Computation CSE 105 $ $ $ Theory of Computtion CSE 105 Regulr Lnguges Study Guide nd Homework I Homework I: Solutions to the following problems should be turned in clss on July 1, 1999. Instructions: Write your nswers clerly

More information

CSCE 531, Spring 2017, Midterm Exam Answer Key

CSCE 531, Spring 2017, Midterm Exam Answer Key CCE 531, pring 2017, Midterm Exm Answer Key 1. (15 points) Using the method descried in the ook or in clss, convert the following regulr expression into n equivlent (nondeterministic) finite utomton: (

More information

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7.

CS 241. Fall 2017 Midterm Review Solutions. October 24, Bits and Bytes 1. 3 MIPS Assembler 6. 4 Regular Languages 7. CS 241 Fll 2017 Midterm Review Solutions Octoer 24, 2017 Contents 1 Bits nd Bytes 1 2 MIPS Assemly Lnguge Progrmming 2 3 MIPS Assemler 6 4 Regulr Lnguges 7 5 Scnning 9 1 Bits nd Bytes 1. Give two s complement

More information

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program:

Example: Source Code. Lexical Analysis. The Lexical Structure. Tokens. What do we really care here? A Sample Toy Program: Lexicl Anlysis Red source progrm nd produce list of tokens ( liner nlysis) source progrm The lexicl structure is specified using regulr expressions Other secondry tsks: (1) get rid of white spces (e.g.,

More information

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011

CSCI 3130: Formal Languages and Automata Theory Lecture 12 The Chinese University of Hong Kong, Fall 2011 CSCI 3130: Forml Lnguges nd utomt Theory Lecture 12 The Chinese University of Hong Kong, Fll 2011 ndrej Bogdnov In progrmming lnguges, uilding prse trees is significnt tsk ecuse prse trees tell us the

More information

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015

Finite Automata. Lecture 4 Sections Robb T. Koether. Hampden-Sydney College. Wed, Jan 21, 2015 Finite Automt Lecture 4 Sections 3.6-3.7 Ro T. Koether Hmpden-Sydney College Wed, Jn 21, 2015 Ro T. Koether (Hmpden-Sydney College) Finite Automt Wed, Jn 21, 2015 1 / 23 1 Nondeterministic Finite Automt

More information

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona

Implementing Automata. CSc 453. Compilers and Systems Software. 4 : Lexical Analysis II. Department of Computer Science University of Arizona Implementing utomt Sc 5 ompilers nd Systems Softwre : Lexicl nlysis II Deprtment of omputer Science University of rizon collerg@gmil.com opyright c 009 hristin ollerg NFs nd DFs cn e hrd-coded using this

More information

CMSC 331 First Midterm Exam

CMSC 331 First Midterm Exam 0 00/ 1 20/ 2 05/ 3 15/ 4 15/ 5 15/ 6 20/ 7 30/ 8 30/ 150/ 331 First Midterm Exm 7 October 2003 CMC 331 First Midterm Exm Nme: mple Answers tudent ID#: You will hve seventy-five (75) minutes to complete

More information

Scanner Termination. Multi Character Lookahead

Scanner Termination. Multi Character Lookahead If d.doublevlue() represents vlid integer, (int) d.doublevlue() will crete the pproprite integer vlue. If string representtion of n integer begins with ~ we cn strip the ~, convert to double nd then negte

More information

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex

Quiz2 45mins. Personal Number: Problem 1. (20pts) Here is an Table of Perl Regular Ex Long Quiz2 45mins Nme: Personl Numer: Prolem. (20pts) Here is n Tle of Perl Regulr Ex Chrcter Description. single chrcter \s whitespce chrcter (spce, t, newline) \S non-whitespce chrcter \d digit (0-9)

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Progrmming Lnguges h"p://www.di.unipi.it/~ndre/did2c/plp- 14/ Prof. Andre Corrdini Deprtment of Computer Science, Pis Lesson 5! Gener;on of Lexicl Anlyzers Creting Lexicl Anlyzer with Lex

More information

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table

LR Parsing, Part 2. Constructing Parse Tables. Need to Automatically Construct LR Parse Tables: Action and GOTO Table TDDD55 Compilers nd Interpreters TDDB44 Compiler Construction LR Prsing, Prt 2 Constructing Prse Tles Prse tle construction Grmmr conflict hndling Ctegories of LR Grmmrs nd Prsers Peter Fritzson, Christoph

More information

ASTs, Regex, Parsing, and Pretty Printing

ASTs, Regex, Parsing, and Pretty Printing ASTs, Regex, Prsing, nd Pretty Printing CS 2112 Fll 2016 1 Algeric Expressions To strt, consider integer rithmetic. Suppose we hve the following 1. The lphet we will use is the digits {0, 1, 2, 3, 4, 5,

More information

From Dependencies to Evaluation Strategies

From Dependencies to Evaluation Strategies From Dependencies to Evlution Strtegies Possile strtegies: 1 let the user define the evlution order 2 utomtic strtegy sed on the dependencies: use locl dependencies to determine which ttriutes to compute

More information

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number>

12 <= rm <digit> 2 <= rm <no> 2 <= rm <no> <digit> <= rm <no> <= rm <number> DDD16 Compilers nd Interpreters DDB44 Compiler Construction R Prsing Prt 1 R prsing concept Using prser genertor Prse ree Genertion Wht is R-prsing? eft-to-right scnning R Rigthmost derivtion in reverse

More information

Applied Databases. Sebastian Maneth. Lecture 13 Online Pattern Matching on Strings. University of Edinburgh - February 29th, 2016

Applied Databases. Sebastian Maneth. Lecture 13 Online Pattern Matching on Strings. University of Edinburgh - February 29th, 2016 Applied Dtses Lecture 13 Online Pttern Mtching on Strings Sestin Mneth University of Edinurgh - Ferury 29th, 2016 2 Outline 1. Nive Method 2. Automton Method 3. Knuth-Morris-Prtt Algorithm 4. Boyer-Moore

More information

Regular Expression Matching with Multi-Strings and Intervals. Philip Bille Mikkel Thorup

Regular Expression Matching with Multi-Strings and Intervals. Philip Bille Mikkel Thorup Regulr Expression Mtching with Multi-Strings nd Intervls Philip Bille Mikkel Thorup Outline Definition Applictions Previous work Two new problems: Multi-strings nd chrcter clss intervls Algorithms Thompson

More information

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the

this grammar generates the following language: Because this symbol will also be used in a later step, it receives the LR() nlysis Drwcks of LR(). Look-hed symols s eplined efore, concerning LR(), it is possile to consult the net set to determine, in the reduction sttes, for which symols it would e possile to perform reductions.

More information

TO REGULAR EXPRESSIONS

TO REGULAR EXPRESSIONS Suject :- Computer Science Course Nme :- Theory Of Computtion DA TO REGULAR EXPRESSIONS Report Sumitted y:- Ajy Singh Meen 07000505 jysmeen@cse.iit.c.in BASIC DEINITIONS DA:- A finite stte mchine where

More information

What are suffix trees?

What are suffix trees? Suffix Trees 1 Wht re suffix trees? Allow lgorithm designers to store very lrge mount of informtion out strings while still keeping within liner spce Allow users to serch for new strings in the originl

More information

CS 241 Week 4 Tutorial Solutions

CS 241 Week 4 Tutorial Solutions CS 4 Week 4 Tutoril Solutions Writing n Assemler, Prt & Regulr Lnguges Prt Winter 8 Assemling instrutions utomtilly. slt $d, $s, $t. Solution: $d, $s, nd $t ll fit in -it signed integers sine they re 5-it

More information

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string.

CS 340, Fall 2016 Sep 29th Exam 1 Note: in all questions, the special symbol ɛ (epsilon) is used to indicate the empty string. CS 340, Fll 2016 Sep 29th Exm 1 Nme: Note: in ll questions, the speil symol ɛ (epsilon) is used to indite the empty string. Question 1. [10 points] Speify regulr expression tht genertes the lnguge over

More information

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata

acronyms possibly used in this test: CFG :acontext free grammar CFSM :acharacteristic finite state machine DFA :adeterministic finite automata EE573 Fll 2002, Exm open book, if question seems mbiguous, sk me to clrify the question. If my nswer doesn t stisfy you, plese stte your ssumptions. cronyms possibly used in this test: CFG :context free

More information

COS 333: Advanced Programming Techniques

COS 333: Advanced Programming Techniques COS 333: Advnced Progrmming Techniques Brin Kernighn wk@cs, www.cs.princeton.edu/~wk 311 CS Building 609-258-2089 (ut emil is lwys etter) TA's: Junwen Li, li@cs, CS 217,258-0451 Yong Wng,yongwng@cs, CS

More information

COS 333: Advanced Programming Techniques

COS 333: Advanced Programming Techniques COS 333: Advnced Progrmming Techniques How to find me wk@cs, www.cs.princeton.edu/~wk 311 CS Building 609-258-2089 (ut emil is lwys etter) TA's: Mtvey Arye (rye), Tom Jlin (tjlin), Nick Johnson (npjohnso)

More information

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009

Sample Midterm Solutions COMS W4115 Programming Languages and Translators Monday, October 12, 2009 Deprtment of Computer cience Columbi University mple Midterm olutions COM W4115 Progrmming Lnguges nd Trnsltors Mondy, October 12, 2009 Closed book, no ids. ch question is worth 20 points. Question 5(c)

More information

CS 321 Programming Languages and Compilers. Bottom Up Parsing

CS 321 Programming Languages and Compilers. Bottom Up Parsing CS 321 Progrmming nguges nd Compilers Bottom Up Prsing Bottom-up Prsing: Shift-reduce prsing Grmmr H: fi ; fi b Input: ;;b hs prse tree ; ; b 2 Dt for Shift-reduce Prser Input string: sequence of tokens

More information

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or

Operator Precedence. Java CUP. E E + T T T * P P P id id id. Does a+b*c mean (a+b)*c or Opertor Precedence Most progrmming lnguges hve opertor precedence rules tht stte the order in which opertors re pplied (in the sence of explicit prentheses). Thus in C nd Jv nd CSX, +*c mens compute *c,

More information

Context-Free Grammars

Context-Free Grammars Context-Free Grmmrs Descriing Lnguges We've seen two models for the regulr lnguges: Finite utomt ccept precisely the strings in the lnguge. Regulr expressions descrie precisely the strings in the lnguge.

More information

2014 Haskell January Test Regular Expressions and Finite Automata

2014 Haskell January Test Regular Expressions and Finite Automata 0 Hskell Jnury Test Regulr Expressions nd Finite Automt This test comprises four prts nd the mximum mrk is 5. Prts I, II nd III re worth 3 of the 5 mrks vilble. The 0 Hskell Progrmming Prize will be wrded

More information

Regular Expressions and Automata using Miranda

Regular Expressions and Automata using Miranda Regulr Expressions nd Automt using Mirnd Simon Thompson Computing Lortory Univerisity of Kent t Cnterury My 1995 Contents 1 Introduction ::::::::::::::::::::::::::::::::: 1 2 Regulr Expressions :::::::::::::::::::::::::::::

More information

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing

10/12/17. Motivating Example. Lexical and Syntax Analysis (2) Recursive-Descent Parsing. Recursive-Descent Parsing. Recursive-Descent Parsing Motivting Exmple Lexicl nd yntx Anlysis (2) In Text: Chpter 4 Consider the grmmr -> cad A -> b Input string: w = cd How to build prse tree top-down? 2 Initilly crete tree contining single node (the strt

More information

Lecture T4: Pattern Matching

Lecture T4: Pattern Matching Introduction to Theoreticl CS Lecture T4: Pttern Mtching Two fundmentl questions. Wht cn computer do? How fst cn it do it? Generl pproch. Don t tlk bout specific mchines or problems. Consider miniml bstrct

More information

CS481: Bioinformatics Algorithms

CS481: Bioinformatics Algorithms CS481: Bioinformtics Algorithms Cn Alkn EA509 clkn@cs.ilkent.edu.tr http://www.cs.ilkent.edu.tr/~clkn/teching/cs481/ EXACT STRING MATCHING Fingerprint ide Assume: We cn compute fingerprint f(p) of P in

More information

Midterm I Solutions CS164, Spring 2006

Midterm I Solutions CS164, Spring 2006 Midterm I Solutions CS164, Spring 2006 Februry 23, 2006 Plese red ll instructions (including these) crefully. Write your nme, login, SID, nd circle the section time. There re 8 pges in this exm nd 4 questions,

More information

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong

LING/C SC/PSYC 438/538. Lecture 21 Sandiway Fong LING/C SC/PSYC 438/538 Lecture 21 Sndiwy Fong Tody's Topics Homework 8 Review Optionl Homework 9 (mke up on Homework 7) Homework 8 Review Question1: write Prolog regulr grmmr for the following lnguge:

More information

Scanning Theory and Practice

Scanning Theory and Practice CHAPTER 3 Scnning Theory nd Prctice 3.1 Overview The primry function of scnner is to red in chrcters from source file nd group them into tokens. A scnner is sometimes clled lexicl nlyzer or lexer. The

More information

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications

Java CUP. Java CUP Specifications. User Code Additions. Package and Import Specifications Jv CUP Jv CUP is prser-genertion tool, similr to Ycc. CUP uilds Jv prser for LALR(1) grmmrs from production rules nd ssocited Jv code frgments. When prticulr production is recognized, its ssocited code

More information

10.5 Graphing Quadratic Functions

10.5 Graphing Quadratic Functions 0.5 Grphing Qudrtic Functions Now tht we cn solve qudrtic equtions, we wnt to lern how to grph the function ssocited with the qudrtic eqution. We cll this the qudrtic function. Grphs of Qudrtic Functions

More information

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries

Tries. Yufei Tao KAIST. April 9, Y. Tao, April 9, 2013 Tries Tries Yufei To KAIST April 9, 2013 Y. To, April 9, 2013 Tries In this lecture, we will discuss the following exct mtching prolem on strings. Prolem Let S e set of strings, ech of which hs unique integer

More information

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID:

Mid-term exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Oct 25, Student's name: Student ID: Fll term 2012 KAIST EE209 Progrmming Structures for EE Mid-term exm Thursdy Oct 25, 2012 Student's nme: Student ID: The exm is closed book nd notes. Red the questions crefully nd focus your nswers on wht

More information

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs.

If you are at the university, either physically or via the VPN, you can download the chapters of this book as PDFs. Lecture 5 Wlks, Trils, Pths nd Connectedness Reding: Some of the mteril in this lecture comes from Section 1.2 of Dieter Jungnickel (2008), Grphs, Networks nd Algorithms, 3rd edition, which is ville online

More information

stack of states and grammar symbols Stack-Bottom marker C. Kessler, IDA, Linköpings universitet. 1. <list> -> <list>, <element> 2.

stack of states and grammar symbols Stack-Bottom marker C. Kessler, IDA, Linköpings universitet. 1. <list> -> <list>, <element> 2. TDDB9 Compilers nd Interpreters TDDB44 Compiler Construction LR Prsing Updted/New slide mteril 007: Pushdown Automton for LR-Prsing Finite-stte pushdown utomton contins lterntingly sttes nd symols in NUΣ

More information

Example: 2:1 Multiplexer

Example: 2:1 Multiplexer Exmple: 2:1 Multiplexer Exmple #1 reg ; lwys @( or or s) egin if (s == 1') egin = ; else egin = ; 1 s B. Bs 114 Exmple: 2:1 Multiplexer Exmple #2 Normlly lwys include egin nd sttements even though they

More information

Lexical Analysis. Role, Specification & Recognition Tool: LEX Construction: - RE to NFA to DFA to min-state DFA - RE to DFA

Lexical Analysis. Role, Specification & Recognition Tool: LEX Construction: - RE to NFA to DFA to min-state DFA - RE to DFA Lexicl Anlysis Role, Specifiction & Recognition Tool: LEX Construction: - RE to NFA to DFA to min-stte DFA - RE to DFA Conducting Lexicl Anlysis Techniques for specifying nd implementing lexicl nlyzers

More information

CS201 Discussion 10 DRAWTREE + TRIES

CS201 Discussion 10 DRAWTREE + TRIES CS201 Discussion 10 DRAWTREE + TRIES DrwTree First instinct: recursion As very generic structure, we could tckle this problem s follows: drw(): Find the root drw(root) drw(root): Write the line for the

More information

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig

CS311H: Discrete Mathematics. Graph Theory IV. A Non-planar Graph. Regions of a Planar Graph. Euler s Formula. Instructor: Işıl Dillig CS311H: Discrete Mthemtics Grph Theory IV Instructor: Işıl Dillig Instructor: Işıl Dillig, CS311H: Discrete Mthemtics Grph Theory IV 1/25 A Non-plnr Grph Regions of Plnr Grph The plnr representtion of

More information

Lecture T1: Pattern Matching

Lecture T1: Pattern Matching Introduction to Theoreticl CS Lecture T: Pttern Mtchin Two fundmentl questions. Wht cn computer do? Wht cn computer do with limited resources? Generl pproch. Don t tlk out specific mchines or prolems.

More information

Algorithm Design (5) Text Search

Algorithm Design (5) Text Search Algorithm Design (5) Text Serch Tkshi Chikym School of Engineering The University of Tokyo Text Serch Find sustring tht mtches the given key string in text dt of lrge mount Key string: chr x[m] Text Dt:

More information

Context-Free Grammars

Context-Free Grammars Context-Free Grmmrs Descriing Lnguges We've seen two models for the regulr lnguges: Finite utomt ccept precisely the strings in the lnguge. Regulr expressions descrie precisely the strings in the lnguge.

More information

Data sharing in OpenMP

Data sharing in OpenMP Dt shring in OpenMP Polo Burgio polo.burgio@unimore.it Outline Expressing prllelism Understnding prllel threds Memory Dt mngement Dt cluses Synchroniztion Brriers, locks, criticl sections Work prtitioning

More information

LEX5: Regexps to NFA. Lexical Analysis. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

LEX5: Regexps to NFA. Lexical Analysis. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class LEX5: Regexps to NFA Lexicl Anlysis CMPT 379: Compilers Instructor: Anoop Srkr noopsrkr.github.io/compilers-clss Building Lexicl Anlyzer Token POern POern Regulr Expression Regulr Expression NFA NFA DFA

More information

Intermediate Information Structures

Intermediate Information Structures CPSC 335 Intermedite Informtion Structures LECTURE 13 Suffix Trees Jon Rokne Computer Science University of Clgry Cnd Modified from CMSC 423 - Todd Trengen UMD upd Preprocessing Strings We will look t

More information

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22)

Homework. Context Free Languages III. Languages. Plan for today. Context Free Languages. CFLs and Regular Languages. Homework #5 (due 10/22) Homework Context Free Lnguges III Prse Trees nd Homework #5 (due 10/22) From textbook 6.4,b 6.5b 6.9b,c 6.13 6.22 Pln for tody Context Free Lnguges Next clss of lnguges in our quest! Lnguges Recll. Wht

More information

Systems I. Logic Design I. Topics Digital logic Logic gates Simple combinational logic circuits

Systems I. Logic Design I. Topics Digital logic Logic gates Simple combinational logic circuits Systems I Logic Design I Topics Digitl logic Logic gtes Simple comintionl logic circuits Simple C sttement.. C = + ; Wht pieces of hrdwre do you think you might need? Storge - for vlues,, C Computtion

More information

COMBINATORIAL PATTERN MATCHING

COMBINATORIAL PATTERN MATCHING COMBINATORIAL PATTERN MATCHING Genomic Repets Exmple of repets: ATGGTCTAGGTCCTAGTGGTC Motivtion to find them: Genomic rerrngements re often ssocited with repets Trce evolutionry secrets Mny tumors re chrcterized

More information

Virtual Machine (Part I)

Virtual Machine (Part I) Hrvrd University CS Fll 2, Shimon Schocken Virtul Mchine (Prt I) Elements of Computing Systems Virtul Mchine I (Ch. 7) Motivtion clss clss Min Min sttic sttic x; x; function function void void min() min()

More information

CIS 1068 Program Design and Abstraction Spring2015 Midterm Exam 1. Name SOLUTION

CIS 1068 Program Design and Abstraction Spring2015 Midterm Exam 1. Name SOLUTION CIS 1068 Progrm Design nd Astrction Spring2015 Midterm Exm 1 Nme SOLUTION Pge Points Score 2 15 3 8 4 18 5 10 6 7 7 7 8 14 9 11 10 10 Totl 100 1 P ge 1. Progrm Trces (41 points, 50 minutes) Answer the

More information

CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1)

CS 236 Language and Computation. Alphabet. Definition. I.2.1. Formal Languages (10.1) C 236 Lnguge nd Computtion Course Notes Prt I: Grmmrs for Defining yntx (II) Chpter I.2: yntx nd Grmmrs (10, 12.1) Anton etzer (Bsed on ook drft y J. V. Tucker nd K. tephenson) Dept. of Computer cience,

More information

UNIT 11. Query Optimization

UNIT 11. Query Optimization UNIT Query Optimiztion Contents Introduction to Query Optimiztion 2 The Optimiztion Process: An Overview 3 Optimiztion in System R 4 Optimiztion in INGRES 5 Implementing the Join Opertors Wei-Png Yng,

More information

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES

UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS 1 COMPUTATION & LOGIC INSTRUCTIONS TO CANDIDATES UNIVERSITY OF EDINBURGH COLLEGE OF SCIENCE AND ENGINEERING SCHOOL OF INFORMATICS INFORMATICS COMPUTATION & LOGIC Sturdy st April 7 : to : INSTRUCTIONS TO CANDIDATES This is tke-home exercise. It will not

More information

Lab 1 - Counter. Create a project. Add files to the project. Compile design files. Run simulation. Debug results

Lab 1 - Counter. Create a project. Add files to the project. Compile design files. Run simulation. Debug results 1 L 1 - Counter A project is collection mechnism for n HDL design under specifiction or test. Projects in ModelSim ese interction nd re useful for orgnizing files nd specifying simultion settings. The

More information

Data Flow on a Queue Machine. Bruno R. Preiss. Copyright (c) 1987 by Bruno R. Preiss, P.Eng. All rights reserved.

Data Flow on a Queue Machine. Bruno R. Preiss. Copyright (c) 1987 by Bruno R. Preiss, P.Eng. All rights reserved. Dt Flow on Queue Mchine Bruno R. Preiss 2 Outline Genesis of dt-flow rchitectures Sttic vs. dynmic dt-flow rchitectures Pseudo-sttic dt-flow execution model Some dt-flow mchines Simple queue mchine Prioritized

More information

Lecture 18: Theory of Computation

Lecture 18: Theory of Computation Introduction to Theoreticl CS ecture 18: Theory of Computtion Two fundmentl questions. Wht cn computer do? Wht cn computer do with limited resources? Generl pproch. Pentium IV running inux kernel.4. Don't

More information

Compression Outline :Algorithms in the Real World. Lempel-Ziv Algorithms. LZ77: Sliding Window Lempel-Ziv

Compression Outline :Algorithms in the Real World. Lempel-Ziv Algorithms. LZ77: Sliding Window Lempel-Ziv Compression Outline 15-853:Algorithms in the Rel World Dt Compression III Introduction: Lossy vs. Lossless, Benchmrks, Informtion Theory: Entropy, etc. Proility Coding: Huffmn + Arithmetic Coding Applictions

More information

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples

COMPUTER SCIENCE 123. Foundations of Computer Science. 6. Tuples COMPUTER SCIENCE 123 Foundtions of Computer Science 6. Tuples Summry: This lecture introduces tuples in Hskell. Reference: Thompson Sections 5.1 2 R.L. While, 2000 3 Tuples Most dt comes with structure

More information