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

Size: px
Start display at page:

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

Transcription

1 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., \t,\n,\sp) nd comments () line numering lexicl nlyzer token get next token prser A Smple Toy Progrm: Exmple: Source Code (* define vlid mutully recursive procedures *) let function do_nothing1(: int, : string)= do_nothing(+1) function do_nothing(d: int) = do_nothing1(d, str ) in end do_nothing1(0, str ) Wht do we relly cre here? Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 1 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0 The Lexicl Structure Output fter the Lexicl Anlysis token + ssocited vlue LET 51 FUNCTION 56 ID(do_nothing1) 65 LPAREN 76 ID() 77 COLON 78 ID(int) 80 COMMA 8 ID() 85 COLON 86 ID(string) 88 RPAREN 9 EQ 95 ID(do_nothing) 99 LPAREN 110 ID() 111 PLUS 11 INT(1) 11 RPAREN 11 FUNCTION 117 ID(do_nothing) 16 LPAREN 17 ID(d) 18 COLON 19 ID(int) 11 RPAREN 1 EQ 16 ID(do_nothing1) 150 LPAREN 161 ID(d) 16 COMMA 16 STRING(str) 165 RPAREN 170 IN 17 ID(do_nothing1) 177 LPAREN 188 INT(0) 189 COMMA 190 STRING(str) 19 RPAREN 198 END 00 EOF 0 Tokens Tokens re the tomic unit of lnguge, nd re usully specific strings or instnces of clsses of strings. Tokens Smple Vlues Informl Description LET let keyword LET END end keyword END PLUS + LPAREN ( COLON : STRING str RPAREN ) INT 9, 8 integer constnts ID do_nothing1,, int, string EQ = EOF letter followed y letters, digits, nd underscores end of file Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0

2 Lexicl Anlysis, How? First, write down the lexicl specifiction (how ech token is defined?) using regulr expression to specify the lexicl structure: identifier = letter (letter digit underscore) * letter =... z A... Z digit = Second, sed on the ove lexicl specifiction, uild the lexicl nlyzer (to recognize tokens) y hnd, Regulr Expression Spec ==> NFA ==> DFA ==>Trnsition Tle ==> Lexicl Anlyzer Or just y using lex --- the lexicl nlyzer genertor Regulr Expression Spec (in lex formt) ==> feed to lex ==> Lexicl Anlyzer Regulr Expressions regulr expressions re concise, linguistic chrcteriztion of regulr lnguges (regulr sets) identifier = letter (letter digit underscore) * or ech regulr expression define regulr lnguge --- set of strings over some lphet, such s ASCII chrcters; ech memer of this set is clled sentence, or word we use regulr expressions to define ech ctegory of tokens 0 or more For exmple, the ove identifier specifies set of strings tht re sequence of letters, digits, nd underscores, strting with letter. Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 5 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 6 of 0 Regulr Expressions nd Regulr Lnguges Given n lphet the regulr expressions over nd their corresponding regulr lnguges re ) denotes,the empty string, denotes the lnguge { }. ) for ech in, denotes { } --- lnguge with one string. c) if R denotes L R nd S denotes L S then R S denotes the lnguge L R L S, i.e, { x x L R or x L S }. d) if R denotes L R nd S denotes L S then RS denotes the lnguge L R L S, tht is, { xy x L R nd y L S }. e) if R denotes L R then R * denotes the lnguge L R * where L * is the union of ll L i (i=0,...,nd L i is just {x 1 x...x i x 1 L,..., x i L}. Exmple Regulr Expression Explntion * 0 or more s + 1 or more s ( ) * ll strings of s nd s (including ) ( ) * ll strings of s nd s of even length [-za-z] shorthnd for... z A... Z [0-9] shorthnd for ([0-9]) * 0 numers tht strt nd end with 0 ( ) * ( e)?? ll strings tht contin foo s sustring the following is not regulr expression: n n (n > 0) f) if R denotes L R then (R) denotes the sme lnguge L R. Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 7 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 8 of 0

3 Lexicl Specifiction Using regulr expressions to specify tokens keyword = egin end if then else identifier = letter (letter digit underscore) * integer = digit + relop = < <= = <> > >= letter =... z A B... Z digit = Amiguity : is egin keyword or n identifier? Next step: to construct token recognizer for lnguges given y regulr expressions --- y using finite utomt! given string x, the token recognizer sys yes if x is sentence of the specified lnguge nd sys no otherwise Trnsition Digrms Flowchrt with sttes nd edges; ech edge is lelled with chrcters; certin suset of sttes re mrked s s Trnsition from stte to stte proceeds long edges ccording to the next input chrcter letter digit underscore strt letter delimiter 0 1 Every string tht ends up t is ccepted If get stuck, there is no trnsition for given chrcter, it is n error Trnsition digrms cn e esily trnslted to progrms using cse sttements (in C). Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 9 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 10 of 0 Trnsition Digrms (cont d) The token recognizer (for identifiers) sed on trnsition digrms: stte0: stte1: c = getchr(); if (islph(c)) goto stte1; error();... c = getchr(); if (islph(c) isdigit(c) isunderscore(c)) goto stte1; if (c ==,... c == ) ) goto stte; error();... stte: ungetc(c,stdin); /* retrct current chr */ return(id,... the current identifier...); Next: 1. finite utomt re generlized trnsition digrms!. how to uild finite utomt from regulr expressions? Finite Automt Finite Automt re similr to trnsition digrms; they hve sttes nd lelled edges; there re one unique strt stte nd one or more thn one finl sttes Nondeterministic Finite Automt (NFA) : ) cn lel edges (these edges re clled -trnsitions) ) some chrcter cn lel or more edges out of the sme stte Deterministic Finite Automt (DFA) : ) no edges re lelled with ) ech chrcter cn lel t most one edge out of the sme stte NFA nd DFA ccepts string x if there exists pth from the strt stte to leled with chrcters in x NFA: multiple pths DFA: one unique pth Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 11 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 1 of 0

4 Exmple: NFA Exmple: DFA strt stte strt 0 1 An NFA ccepts ( ) * strt stte strt 0 1 A DFA ccepts ( ) * There re mny possile moves --- to ccept string, we only need one sequence of moves tht led to. There is only one possile sequence of moves --- either led to nd ccept or the input string is rejected input string: One sucessful sequence: Another unsuccessful sequence: input string: The sucessful sequence: Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 1 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 1 of 0 Trnsition Tle Finite Automt cn lso e represented using trnsition tles NFA with -trnsitions 1. NFA cn hve -trnsitions --- edges lelled with For NFA, ech entry is set of sttes: For DFA, ech entry is unique stte: 1 STATE 0 {0,1} {0} 1 - {} - {} - - STATE ccepts the regulr lnguge denoted y ( * * ) Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 15 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 16 of 0

5 Regulr Expressions -> NFA How to construct NFA (with -trnsitions) from regulr expression? Algorithm : pply the following construction rules, use unique nmes for ll the sttes. (inportnt invrint: lwys one!) 1. Bsic Construction i f. Inductive Construction R 1 R RE -> NFA (cont d) i initil stte for N 1 nd N N 1 N for N 1 nd N f N 1 : NFA for R 1 N : NFA for R the new nd unique i f R 1 R merge : of N 1 nd initil stte of N initil stte for N 1 N 1 N for N Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 17 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 18 of 0 RE -> NFA (cont d). Inductive Construction (cont d) Exmple : RE -> NFA Converting the regulr expression : ( ) * (in )===> R 1 * initil stte for N 1 for N 1 N 1 : NFA for R 1 (in )===> 5 i N 1 f ====> Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 19 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 0 of 0

6 Exmple : RE -> NFA (cont d) Converting the regulr expression : ( ) * Exmple : RE -> NFA (cont d) Converting the regulr expression : ( ) * ( ) * ====> ( ) * ====> X ====> (severl steps re omitted) Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 1 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0 NFA -> DFA NFA re non-deterministic; need DFA in order to write deterministic prorgm! There exists n lgorithm ( suset construction ) to convert ny NFA to DFA tht ccepts the sme lnguge Sttes in DFA re sets of sttes from NFA; DFA simultes in prllel ll possile moves of NFA on given input. Definition: for ech stte s in NFA, -CLOSURE(s) = { s } { t s cn rech t vi -trnsitions } Definition: for ech set of sttes S in NFA, -CLOSURE(S) = i -CLOSURE(s) for ll s i in S NFA -> DFA (cont d) ech DFA-stte is set of NFA-sttes suppose the strt stte of the NFA is s, then the strt stte for its DFA is - CLOSURE(s) ; the s of the DFA re those tht include NFA-finl-stte Algorithm : converting n NFA N into DFA D ---- Dsttes = {e-closure(s 0 ),s 0 is N s strt stte} Dsttes re initilly unmrked while there is n unmrked D-stte X do { mrk X for ech in S do { T = {sttes reched from ny s i in X vi } Y = e-closure(t) if Y not in Dsttes then dd Y to Dsttes unmrked dd trnsition from X to Y, lelled with } } Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0

7 Exmple : NFA -> DFA converting NFA for ( )* to DFA The strt stte A = -CLOSURE(0) = {0, 1,,, 7}; Dsttes={A} 1st itertion: A is unmrked; mrk A now; -trnsitions: T = {, 8} new stte B= -CLOSURE() -CLOSURE(8) = {, 6, 1,,, 7} 8) = {1,,,, 6, 7, 8} dd trnsition from A to B lelled with -trnsitions: T = {5} new stte C = -CLOSURE(5) = {1,,, 5, 6, 7} dd trnsition from A to C lelled with Dsttes = {A, B, C} nd itertion: B, C re unmrked; we pick B nd mrk B first; B = {1,,,, 6, 7, 8} B s -trnsitions: T = {, 8}; T s -CLOSURE is B itself. dd trnsition from B to B lelled with Exmple : NFA -> DFA (cont d) B s -trnsitions: T = {5, 9}; new stte D = -CLOSURE({5, 9}) = {1,,, 5, 6, 7, 9} dd trnsition from B to D lelled with Dsttes = {A, B, C, D} then we pick C, nd mrk C C s -trnsitions: T = {, 8}; its -CLOSURE is B. dd trnsition from C to B lelled with C s -trnsitions: T = {5}; its -CLOSURE is C itself. dd trnsition from C to C lelled with next we pick D, nd mrk D D s -trnsitions: T = {, 8}; its -CLOSURE is B. dd trnsition from D to B lelled with D s -trnsitions: T = {5, 10}; new stte E = -CLOSURE({5, 10}) = {1,,, 5, 6, 7, 10} Dsttes = {A, B, C, D, E}; E is since it hs 10; next we pick E, nd mrk E Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 5 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 6 of 0 Exmple : NFA -> DFA (cont d) E s -trnsitions: T = {, 8}; its -CLOSURE is B. dd trnsition from E to B lelled with E s -trnsitions: T = {5}; its -CLOSURE is C itself. dd trnsition from E to C lelled with Other Algorithms How to minimize DFA? (see Drgon Book.9, pp11) ll sttes in Dsttes re mrked, the DFA is constructed! A B D C E How to convert RE to DFA directly? (see Drgon Book.9, pp15) How to prove two Regulr Expressions re equivlent? (see Drgon Book pp150, Exercise.) Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 7 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 8 of 0

8 Lex Lex is progrm genertor it tkes lexicl specifiction s input, nd produces lexicl processor written in C. DIGITS [0-9]... Lex Specifiction lex definition Lex Specifiction foo.l Lex lex.yy.c expression ction integer printf( INT ); trnsltion rules lex.yy.c input text C Compiler.out.out sequence of tokens chr getc() {... } user s C functions (optionl) Implementtion of Lex: Lex Spec -> NFA -> DFA -> Trnsition Tles + Actions -> yylex() expression is regulr expression ; ction is piece of C progrm; for detils, red the Lesk&Schmidt pper Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 9 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 0 of 0 ML-Lex ML-Lex is like Lex it tkes lexicl specifiction s input, nd produces lexicl processor written in Stndrd ML. Lex Specifiction foo.lex foo.lex.sml input text ML-Lex ML Compiler Mlex Implementtion of ML-Lex is similr to implementtion of Lex foo.lex.sml module Mlex sequence of tokens ML-Lex Specifiction type pos = int vl linenum =... vl lexresult = %s COMMENT STRING; SPACE=[ \t\n\01]; DIGITS=[0-9];... expression => (ction); integer => (print( INT ));... => (...linenum...); user s ML declrtions ml-lex definitions trnsltion rules cn cll the ove ML declrtions expression is regulr expression ; ction is piece of ML progrm; when the input mtches the expression, the ction is executed, the text mtched is plced in the vrile yytext. Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 1 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0

9 Wht does ML-Lex generte? foo.lex ML-Lex foo.lex.sml smple foo.lex.sml: everything in prt 1 of foo.lex structure Mlex = struct structure UserDeclrtions = struct... end... fun mkelexer yyinput =... end To use the generted lexicl processor: vl lexer = Mlex.mkeLexer(fn _ => input (openin toy )); vl nexttoken = lexer() ech cll returns one token! input filenme ML-Lex Definitions Things you cn write inside the ml-lex definitions section (nd prt): %s COMMENT STRING define new strt sttes %reject REJECT() to reject mtch %count count the line numer %structure {identifier} the resulting structure nme (the defult is Mlex) (hint: you proly don t need use %reject, %count,or %structure for ssignment.) Definition of nmed regulr expressions : identifier = regulr expression SPACE=[ \t\n\01] IDCHAR=[_-zA-Z0-9] Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge of 0 ML-Lex Trnsltion Rules Ech trnsltion rule (rd prt) re in the form <strt-stte-list> regulr expression => (ction); Vlid ML-Lex regulr expressions: (see ML-Lex-mnul pp -6) chrcter stnds for itself except for the reserved chrs:? * + ( ) ^ $ / ;. = < > [ { \ to use these chrs, use ckslsh! for exmple, \\\ represents the string \ using squre rckets to enclose set of chrcters ( \ - ^ re reserved) [c] chr, or, or c [^c] ll chrs except,, c [-z] ll chrs from to z [\n\t\] new line, t, or ckspce [-c] chr - or or or c ML-Lex Trnsltion Rules (cont d) Vlid ML-Lex regulr expressions: (cont d) escpe sequences: (cn e used inside or outside squre rckets) \ ckspce \n newline \t t \ddd ny scii chr (ddd is digit deciml). ny chr except newline (equivlent to [^\n]) x mtch string x exctly even if it contins reserved chrs x? n optionl x x* 0 or more x s x+ 1 or more x s x y x or y ^x if t the eginning, mtch t the eginning of line only {x} sustitute definition x (defined in the lex definition section) (x) sme s regulr expression x x{n} repeting x for n times x{m-n} repeting x from m to n times Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 5 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 6 of 0

10 ML-Lex Trnsltion Rules (cont d) wht re vlid ctions? Actions re siclly ML code (with the following extensions) All ctions in lex file must return vlues of the sme type Use yytext to refer to the current string [-z]+ => (print yytext); [0-9]{} => (print (Chr.ord(su(yytext,0)))); Cn refer to nything defined in the ML-Declrtion section (1st prt) YYBEGIN strt-stte enter into nother strt stte lex() nd continue() to reinvoking the lexing function yypos --- refer to the current position Amiguity wht if more thn one trnsltion rules mtches? 1 A. longest mtch is preferred B. mong rules which mtched the sme numer of chrcters, the rule given first is preferred while => (Tokens.WHILE(...)); [-za-z][-za-z0-9_]* => (Tokens.ID(yytext,...)); < => (Tokens.LESS(...)); <= => (Tokens.LE(yypos,...)); input while mtches rule 1 ccording B ove input <= mtches rule ccording A ove Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 7 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 8 of 0 Strt Sttes (or Strt Conditions) strt sttes permit multiple lexicl nlyzers to run together. ech trnsltion rule cn e prefixed with <strt-stte> the lexer is initilly in predefined strt ste clled INITIAL define new strt sttes (in ml-lex-definitions): %s COMMENT STRING to switch to nother strt sttes (in ction): YYBEGIN COMMENT exmple: multi-line comments in C %s COMMENT <INITIAL> /* => (YYBEGIN COMMENT; continue()); <COMMENT> */ => (YYBEGIN INITIAL; continue()); <COMMENT>. \n => (continue()); <INITIAL>... Implementtion of Lex construct NFA for sum of Lex trnsltion rules (regexp/ction); convert NFA to DFA, then minimize the DFA to recognize the input, simulte DFA to termintion; find the lst DFA stte tht includes NFA, execute ssocited ction (this pickes longest mtch). If the lst DFA stte hs >1 NFA s, pick one for rule tht ppers first how to represent DFA, the trnsition tle: D rry indexed y stte nd input-chrcter too ig! ech stte hs linked list of (chr, next-stte) pirs too slow! hyrid scheme is the est Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 9 of 0 Copyright Zhong Sho, Yle University Lexicl Anlysis : Pge 0 of 0

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

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

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

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

ΕΠΛ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

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

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

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

ΕΠΛ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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Fall Compiler Principles Lecture 1: Lexical Analysis. Roman Manevich Ben-Gurion University of the Negev Fll 2016-2017 Compiler Principles Lecture 1: Lexicl Anlysis Romn Mnevich Ben-Gurion University of the Negev Agend Understnd role of lexicl nlysis in compiler Regulr lnguges reminder Lexicl nlysis lgorithms

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

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

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

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

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

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

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

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

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

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

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

Information Retrieval and Organisation

Information Retrieval and Organisation Informtion Retrievl nd Orgnistion Suffix Trees dpted from http://www.mth.tu.c.il/~himk/seminr02/suffixtrees.ppt Dell Zhng Birkeck, University of London Trie A tree representing set of strings { } eef d

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

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

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

Suffix trees, suffix arrays, BWT

Suffix trees, suffix arrays, BWT ALGORITHMES POUR LA BIO-INFORMATIQUE ET LA VISUALISATION COURS 3 Rluc Uricru Suffix trees, suffix rrys, BWT Bsed on: Suffix trees nd suffix rrys presenttion y Him Kpln Suffix trees course y Pco Gomez Liner-Time

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

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

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

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

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

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

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

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

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

Stack. A list whose end points are pointed by top and bottom

Stack. A list whose end points are pointed by top and bottom 4. Stck Stck A list whose end points re pointed by top nd bottom Insertion nd deletion tke plce t the top (cf: Wht is the difference between Stck nd Arry?) Bottom is constnt, but top grows nd shrinks!

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

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

MA1008. Calculus and Linear Algebra for Engineers. Course Notes for Section B. Stephen Wills. Department of Mathematics. University College Cork

MA1008. Calculus and Linear Algebra for Engineers. Course Notes for Section B. Stephen Wills. Department of Mathematics. University College Cork MA1008 Clculus nd Liner Algebr for Engineers Course Notes for Section B Stephen Wills Deprtment of Mthemtics University College Cork s.wills@ucc.ie http://euclid.ucc.ie/pges/stff/wills/teching/m1008/ma1008.html

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

Recognition of Tokens

Recognition of Tokens 42 Recognton o Tokens The queston s how to recognze the tokens? Exmple: ssume the ollowng grmmr rgment to generte specc lnguge: stmt expr expr then stmt expr then stmt else stmt term relop term term term

More information

CPSC 213. Polymorphism. Introduction to Computer Systems. Readings for Next Two Lectures. Back to Procedure Calls

CPSC 213. Polymorphism. Introduction to Computer Systems. Readings for Next Two Lectures. Back to Procedure Calls Redings for Next Two Lectures Text CPSC 213 Switch Sttements, Understnding Pointers - 2nd ed: 3.6.7, 3.10-1st ed: 3.6.6, 3.11 Introduction to Computer Systems Unit 1f Dynmic Control Flow Polymorphism nd

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

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Winter 2016 Solving Prolems y Serching CS 486/686: Introduction to Artificil Intelligence Winter 2016 1 Introduction Serch ws one of the first topics studied in AI - Newell nd Simon (1961) Generl Prolem Solver Centrl

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

Ma/CS 6b Class 1: Graph Recap

Ma/CS 6b Class 1: Graph Recap M/CS 6 Clss 1: Grph Recp By Adm Sheffer Course Detils Adm Sheffer. Office hour: Tuesdys 4pm. dmsh@cltech.edu TA: Victor Kstkin. Office hour: Tuesdys 7pm. 1:00 Mondy, Wednesdy, nd Fridy. http://www.mth.cltech.edu/~2014-15/2term/m006/

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

2 Computing all Intersections of a Set of Segments Line Segment Intersection

2 Computing all Intersections of a Set of Segments Line Segment Intersection 15-451/651: Design & Anlysis of Algorithms Novemer 14, 2016 Lecture #21 Sweep-Line nd Segment Intersection lst chnged: Novemer 8, 2017 1 Preliminries The sweep-line prdigm is very powerful lgorithmic design

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

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications.

Fall 2018 Midterm 1 October 11, ˆ You may not ask questions about the exam except for language clarifications. 15-112 Fll 2018 Midterm 1 October 11, 2018 Nme: Andrew ID: Recittion Section: ˆ You my not use ny books, notes, extr pper, or electronic devices during this exm. There should be nothing on your desk or

More information

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins.

Problem Set 2 Fall 16 Due: Wednesday, September 21th, in class, before class begins. Problem Set 2 Fll 16 Due: Wednesdy, September 21th, in clss, before clss begins. 1. LL Prsing For the following sub-problems, consider the following context-free grmmr: S T$ (1) T A (2) T bbb (3) A T (4)

More information

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence

Solving Problems by Searching. CS 486/686: Introduction to Artificial Intelligence Solving Prolems y Serching CS 486/686: Introduction to Artificil Intelligence 1 Introduction Serch ws one of the first topics studied in AI - Newell nd Simon (1961) Generl Prolem Solver Centrl component

More information

Topic: Software Model Checking via Counter-Example Guided Abstraction Refinement. Having a BLAST with SLAM. Combining Strengths. SLAM Overview SLAM

Topic: Software Model Checking via Counter-Example Guided Abstraction Refinement. Having a BLAST with SLAM. Combining Strengths. SLAM Overview SLAM Hving BLAST with SLAM Topic: Softwre Model Checking vi Counter-Exmple Guided Abstrction Refinement There re esily two dozen SLAM/BLAST/MAGIC ppers; I will skim. # # Theorem Proving Combining Strengths

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

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

Functor (1A) Young Won Lim 10/5/17

Functor (1A) Young Won Lim 10/5/17 Copyright (c) 2016-2017 Young W. Lim. Permission is grnted to copy, distribute nd/or modify this document under the terms of the GNU Free Documenttion License, Version 1.2 or ny lter version published

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

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

Control-Flow Analysis and Loop Detection

Control-Flow Analysis and Loop Detection ! Control-Flow Anlysis nd Loop Detection!Lst time! PRE!Tody! Control-flow nlysis! Loops! Identifying loops using domintors! Reducibility! Using loop identifiction to identify induction vribles CS553 Lecture

More information

Midterm 2 Sample solution

Midterm 2 Sample solution Nme: Instructions Midterm 2 Smple solution CMSC 430 Introduction to Compilers Fll 2012 November 28, 2012 This exm contins 9 pges, including this one. Mke sure you hve ll the pges. Write your nme on 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

Pointers and Arrays. More Pointer Examples. Pointers CS 217

Pointers and Arrays. More Pointer Examples. Pointers CS 217 Pointers nd Arrs CS 21 1 2 Pointers More Pointer Emples Wht is pointer A vrile whose vlue is the ddress of nother vrile p is pointer to vrile v Opertions &: ddress of (reference) *: indirection (dereference)

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

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have

P(r)dr = probability of generating a random number in the interval dr near r. For this probability idea to make sense we must have Rndom Numers nd Monte Crlo Methods Rndom Numer Methods The integrtion methods discussed so fr ll re sed upon mking polynomil pproximtions to the integrnd. Another clss of numericl methods relies upon using

More information

Lists in Lisp and Scheme

Lists in Lisp and Scheme Lists in Lisp nd Scheme Lists in Lisp nd Scheme Lists re Lisp s fundmentl dt structures, ut there re others Arrys, chrcters, strings, etc. Common Lisp hs moved on from eing merely LISt Processor However,

More information

Section 3.1: Sequences and Series

Section 3.1: Sequences and Series Section.: Sequences d Series Sequences Let s strt out with the definition of sequence: sequence: ordered list of numbers, often with definite pttern Recll tht in set, order doesn t mtter so this is one

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