Plan for Today and Beginning Next week (Lexical Analysis)

Size: px
Start display at page:

Download "Plan for Today and Beginning Next week (Lexical Analysis)"

Transcription

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

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

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

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

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

6 Empty String A string with no letters: ε (sometimes λ is used) Oservations: ε = 0 εw = wε = w εaa = aaε = aa

7 Regular Expressions Regular expressions descrie regular languages You have proaly seen them in OSs / editors Example: (a ()(c)) * descries the language L((a ()(c))*) = ε,c,aa,ac,ca,... { }

8 Recursive Definition for Specifying Regular Expressions Primitive regular expressions: where α Σ, some alphaet, ε, α Given regular expressions r 1 and r 2 r 1 r 2 r 1 r 2 r 1 * Are regular expressions ( r ) 1

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

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

11 Finite Automaton Input String Output Finite Automaton String

12 Finite Accepter Input String Output Finite Automaton Accept or Reject

13 State Transition Graph aa -Finite Accepter a a a a q 5 q0 q3 q4 initial state state transition final state accept

14 Initial Configuration Input String a a q 0 a a a a q 5 q3 q4

15 Reading the Input a a a a a a q 5 q0 q3 q4

16 a a a a a a q 5 q0 q3 q4

17 a a a a a a q 5 q0 q3 q4

18 a a a a a a q 5 q0 q3 q4

19 Input finished a a a a a a q 5 q0 q3 q4 Output: accept

20 String Rejection a a q 0 a a a a q 5 q3 q4

21 a a a a a a q 5 q0 q3 q4

22 a a a a a a q 5 q0 q3 q4

23 a a a a a a q 5 q0 q3 q4

24 Input finished a a Output: a a a a q 5 q0 q3 q4 reject

25 The Empty String ε q 0 a a a a q 5 q3 q4

26 ε q 0 a a a a q 5 q3 q4 Output: reject Would it e possile to accept the empty string?

27 Another Example a a a a, q0

28 a a a a, q0

29 a a a a, q0

30 a a a a, q0

31 Input finished a a a Output: accept a, q0

32 Rejection a a a, q0

33 a a a, q0

34 a a a, q0

35 a a a, q0

36 Input finished a Which strings are accepted? a a, q0 Output: reject

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

38 Input Alphaet Σ Σ = { } a a a a q 5 q0 q3 q4

39 Set of States Q Q = { q, q q q q q } 0 1, 2, 3, 4, 5 a a a a q 5 q0 q3 q4

40 Initial State q0 q 0 a a a a q 5 q3 q4

41 Set of Final States F F = { } q 4 a a a a q0 q3 q 5 q 4

42 Transition Function δ δ : Q Σ Q a a a a q 5 q0 q3 q4

43 δ ( q ) 0, a = q 1 a a a q a q0 1 q 5 q3 q4

44 δ ( q ) 0, = q 5 q 0 a a a a q 5 q3 q4

45 δ ( q ) 2, = q 3 a a a a q 5 q0 q3 q4

46 Transition Function / tale δ a q q 0 q 1 q 2 q 3 5 q5 q q5 3 q q4 5 δ q 4 q 5 q 5 q 5 q 5 q 5 a a a a q 5 q0 q3 q4

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

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

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

50 Complication 3 3. we want to discard white space and comments and not other the parser with these. So: in scanner generators, we can specify, using a regular expression, white space e.g. [\t\n ] and return no token, i.e. move to the next specify comments using a (NASTY) regular expression and again return no token, move to the next CS453 Lecture Regular Expressions and Transition Diagrams 50

51 Complication 4 4. "123" is a NUMBER ut so is "235" and so is "0", just as "a" is an ID and so is "cd, we want to recognize a token, ut add attriutes to it. So, Scanners return Symols, not tokens. A Symol is a (token, tokenvalue) pair, e.g. (NUMBER,123) or (ID,"a"). Often more information is added to a symol, e.g. line numer and position (as we will do in MeggyJava) CS453 Lecture Regular Expressions and Transition Diagrams 51

52 (Non) Deterministic Finite State Automata A Deterministic Finite State Automaton (DFA) has disjoint character sets on its edges, i.e. the choice which state is next is deterministic. A Non-deterministic Finite State Automaton (NFA) does NOT, i.e. it can have character sets on its edges that overlap (non empty intersection), and empty sets on the some edges (laeled ε ). NFAs are used in the translation from regular expressions to FSAs. E.g. when we comine the reg. exp for IF with the reg.exp for ID y just merging the two Transition graphs, we would get an NFA. NFAs are a first step in creating a DFA for a scanner. The NFA is then transformed into a DFA. CS453 Lecture Regular Expressions and Transition Diagrams 52

53 From regular expressions to NFAs regexp simple letter a empty string AB concat the NFAs A a ε accept state of the NFA for A B A B split merge them ε A ε ε ε B ε A* uild a loop A CS453 Lecture Regular Expressions and Transition Diagrams 53

54 The Prolem DFAs are easy to execute (tale driven interpretation) NFAs are easy to uild from reg. exps, ut hard to execute we would need some form of guessing, implemented y ack tracking To uild a DFA from an NFA we avoid the ack track y taking all choices in the NFA at once, a move with a character or ε gets us to a set of states in the NFA, which will ecome one state in the DFA. We keep doing this until we have exhausted all possiilities. This mechanism is called transitive closure (This ends ecause there is only a finite set of susets of NFA states. How many are there? ) CS453 Lecture Regular Expressions and Transition Diagrams 54

55 Example IF and ID let : [a-z] dig : [0-9] tok : if id if : i f id : let (let dig)* CS453 Lecture Regular Expressions and Transition Diagrams 55

56 Example: NFA for IF and ID 0 ε ε 1 i f IF a-z 2 3 ε ε a-z ε ID IF has priority over ID. From 0, with ε we can get to states 1 and 4 let : [a-z] dig : [0-9] tok : if id if : i f id : let (let dig)* this is called an ε-closure We can now simulate the ehavior of the NFA and uild a tale for the DFA making character moves plus ε-closures CS453 Lecture Regular Expressions and Transition Diagrams 56

57 NFA simulation scanning in 0 ε a-z ε ε ε 1 a-z i f IF 2 3 ID ε DFAstate NFAstates Move Next 0 0,1,4 i 2,5,8,6 1 2,5,6,8 n 6,7,8 Only one of the states in 6,7,8 is an accepting state, an ID accepting state, so in is an ID CS453 Lecture Regular Expressions and Transition Diagrams 57

58 NFA simulation scanning if 0 ε ε 1 a-z ε ε a-z i f IF 2 3 ε ID DFAstate NFAstates Move Next 0 0,1,4 i 2,5,6,8 1 2,5,6,8 f 3,6,7,8 Two of the states in 3,6,7,8 are accepting, an IF accepting state (3) and an ID accepting state (8), IF has priority over ID, so if is an IF CS453 Lecture Regular Expressions and Transition Diagrams 58

59 Definitions: edge(s,c) and closure edge(s,c): the set of all NFA states reachale from state s following an edge with character c closure(s): the set of all states reachale from S with no chars or ε T=S closure(s) = T = S ( repeat T =T; forall s in T { T =T; } until T ==T T = T ' ( s T ' s T edge(s,ε)) edge(s,ε)) This transitive closure algorithm terminates ecause there is a finite numer of states in the NFA CS453 Lecture Regular Expressions and Transition Diagrams 59

60 DFAedge and NFA Simulation Suppose we are in state DFA d = {s i, s k,s l } By moving with character c from d we reach a set of new NFA states, call these DFAedge(d,c), a new or already existing DFA state DFAedge(d, c) = closure( NFA simulation: let the input string e c 1 c k d=closure({s1}) // s 1 the start state of the NFA for i from 1 to k d = DFAedge(d,c i ) s d edge(s, c)) CS453 Lecture Regular Expressions and Transition Diagrams 60

61 Constructing a DFA with closure and DFAEdge state d 1 = closure(s 1 ) the closure of the start state of the NFA make new states y moving from existing states with a character c, using DFAEdge(d,c); record these in the transition tale make accepts in the transition tale, if there is an accepting state in d, decide priority if more than one accept state. Instead of characters we use non-overlapping (DFA) character classes to keep the tale manageale. CS453 Lecture Regular Expressions and Transition Diagrams 61

62 NFA to DFA (let s uild it) i f 2 3 IF 1 a-z ε ε ε a-z ID ε CS453 Lecture Regular Expressions and Transition Diagrams 62

63 NFA to DFA 1 i a-z ε f 2 3 ε ε a-z IF ID ε 1: 1,4 2: i 2,5,6,8 3: ID 3,6,7,8 a-h j-z ID 5: 5,6,8 f a-e g-z 0-9 a-z 0-9 a-z 0-9 a-z 0-9 IF 4: 6,7,8 ID CS453 Lecture Regular Expressions and Transition Diagrams 63

64 The transition tale for IF ID p NFAstates(p) i f a-h a-e,g-z a-z,0-9 ACPT j-z {1,4} {2,5,6,8} {5,6,8} 2 {2,5,6,8} {3,6,7,8} {6,7,8} ID 3 {3,6,7,8} {6,7,8} IF 4 {6,7,8} {6,7,8} ID 5 {5,6,8} {6,7,8} ID CS453 Lecture Regular Expressions and Transition Diagrams 64

65 Suggested Exercise Build an NFA and a DFA for integer and float literals dot:. dig: [0-9] int-lit: dig + float-lit: dig* dot dig+ CS453 Lecture Regular Expressions and Transition Diagrams 65

Languages. A language is a set of strings. String: A sequence of letters. Examples: cat, dog, house, Defined over an alphabet:

Languages. A language is a set of strings. String: A sequence of letters. Examples: cat, dog, house, Defined over an alphabet: Languages 1 Languages A language is a set of strings String: A sequence of letters Examples: cat, dog, house, Defined over an alphaet: a,, c,, z 2 Alphaets and Strings We will use small alphaets: Strings

More information

CS 4120 Lecture 3 Automating lexical analysis 29 August 2011 Lecturer: Andrew Myers. 1 DFAs

CS 4120 Lecture 3 Automating lexical analysis 29 August 2011 Lecturer: Andrew Myers. 1 DFAs CS 42 Lecture 3 Automating lexical analysis 29 August 2 Lecturer: Andrew Myers A lexer generator converts a lexical specification consisting of a list of regular expressions and corresponding actions into

More information

Compilers. Lexical analysis. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers. Lexical analysis. Yannis Smaragdakis, U. Athens (original slides by Sam Compilers Lecture 3 Lexical analysis Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Big picture Source code Front End IR Back End Machine code Errors Front end responsibilities Check

More information

COSE312: Compilers. Lecture 2 Lexical Analysis (1)

COSE312: Compilers. Lecture 2 Lexical Analysis (1) COSE312: Compilers Lecture 2 Lexical Analysis (1) Hakjoo Oh 2017 Spring Hakjoo Oh COSE312 2017 Spring, Lecture 2 March 12, 2017 1 / 15 Lexical Analysis ex) Given a C program float match0 (char *s) /* find

More information

Lexical Analysis. Reinhard Wilhelm, Sebastian Hack, Mooly Sagiv Saarland University, Tel Aviv University.

Lexical Analysis. Reinhard Wilhelm, Sebastian Hack, Mooly Sagiv Saarland University, Tel Aviv University. Lexical Analysis Reinhard Wilhelm, Sebastian Hack, Mooly Sagiv Saarland University, Tel Aviv University http://compilers.cs.uni-saarland.de Compiler Construction Core Course 2017 Saarland University Today

More information

Compiling Techniques

Compiling Techniques Lecture 3: Introduction to 22 September 2017 Reminder Action Create an account and subscribe to the course on piazza. Coursework Starts this afternoon (14.10-16.00) Coursework description is updated regularly;

More information

M = (Q,Σ,δ,q 0,F) Σ is the alphabet over which the transitions are defined.

M = (Q,Σ,δ,q 0,F) Σ is the alphabet over which the transitions are defined. LECTURE 23 10.1 Formal Definition of a DFA Definition 10.1 A Deterministic Finite Automaton (DFA) M, is a five-tuple: where M = (Q,Σ,δ, 0,F) Q is a finite set of states. It is important that the set of

More information

Clarifications from last time. This Lecture. Last Lecture. CMSC 330: Organization of Programming Languages. Finite Automata.

Clarifications from last time. This Lecture. Last Lecture. CMSC 330: Organization of Programming Languages. Finite Automata. CMSC 330: Organization of Programming Languages Last Lecture Languages Sets of strings Operations on languages Finite Automata Regular expressions Constants Operators Precedence CMSC 330 2 Clarifications

More information

Deterministic Finite Automaton (DFA)

Deterministic Finite Automaton (DFA) 1 Lecture Overview Deterministic Finite Automata (DFA) o accepting a string o defining a language Nondeterministic Finite Automata (NFA) o converting to DFA (subset construction) o constructed from a regular

More information

Lexical Analysis Part II: Constructing a Scanner from Regular Expressions

Lexical Analysis Part II: Constructing a Scanner from Regular Expressions Lexical Analysis Part II: Constructing a Scanner from Regular Expressions CS434 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper, Ken Kennedy

More information

Computability Theory

Computability Theory CS:4330 Theory of Computation Spring 2018 Computability Theory Decidable Languages Haniel Barbosa Readings for this lecture Chapter 4 of [Sipser 1996], 3rd edition. Section 4.1. Decidable Languages We

More information

CS 154, Lecture 2: Finite Automata, Closure Properties Nondeterminism,

CS 154, Lecture 2: Finite Automata, Closure Properties Nondeterminism, CS 54, Lecture 2: Finite Automata, Closure Properties Nondeterminism, Why so Many Models? Streaming Algorithms 0 42 Deterministic Finite Automata Anatomy of Deterministic Finite Automata transition: for

More information

Tasks of lexer. CISC 5920: Compiler Construction Chapter 2 Lexical Analysis. Tokens and lexemes. Buffering

Tasks of lexer. CISC 5920: Compiler Construction Chapter 2 Lexical Analysis. Tokens and lexemes. Buffering Tasks of lexer CISC 5920: Compiler Construction Chapter 2 Lexical Analysis Arthur G. Werschulz Fordham University Department of Computer and Information Sciences Copyright Arthur G. Werschulz, 2017. All

More information

Chapter Five: Nondeterministic Finite Automata

Chapter Five: Nondeterministic Finite Automata Chapter Five: Nondeterministic Finite Automata From DFA to NFA A DFA has exactly one transition from every state on every symbol in the alphabet. By relaxing this requirement we get a related but more

More information

CS 154, Lecture 3: DFA NFA, Regular Expressions

CS 154, Lecture 3: DFA NFA, Regular Expressions CS 154, Lecture 3: DFA NFA, Regular Expressions Homework 1 is coming out Deterministic Finite Automata Computation with finite memory Non-Deterministic Finite Automata Computation with finite memory and

More information

CS 154. Finite Automata, Nondeterminism, Regular Expressions

CS 154. Finite Automata, Nondeterminism, Regular Expressions CS 54 Finite Automata, Nondeterminism, Regular Expressions Read string left to right The DFA accepts a string if the process ends in a double circle A DFA is a 5-tuple M = (Q, Σ, δ, q, F) Q is the set

More information

Decision, Computation and Language

Decision, Computation and Language Decision, Computation and Language Non-Deterministic Finite Automata (NFA) Dr. Muhammad S Khan (mskhan@liv.ac.uk) Ashton Building, Room G22 http://www.csc.liv.ac.uk/~khan/comp218 Finite State Automata

More information

MA/CSSE 474 Theory of Computation

MA/CSSE 474 Theory of Computation MA/CSSE 474 Theory of Computation CFL Hierarchy CFL Decision Problems Your Questions? Previous class days' material Reading Assignments HW 12 or 13 problems Anything else I have included some slides online

More information

Finite Automata and Regular Languages (part II)

Finite Automata and Regular Languages (part II) Finite Automata and Regular Languages (part II) Prof. Dan A. Simovici UMB 1 / 25 Outline 1 Nondeterministic Automata 2 / 25 Definition A nondeterministic finite automaton (ndfa) is a quintuple M = (A,

More information

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing CMSC 330: Organization of Programming Languages Pushdown Automata Parsing Chomsky Hierarchy Categorization of various languages and grammars Each is strictly more restrictive than the previous First described

More information

Chapter 5. Finite Automata

Chapter 5. Finite Automata Chapter 5 Finite Automata 5.1 Finite State Automata Capable of recognizing numerous symbol patterns, the class of regular languages Suitable for pattern-recognition type applications, such as the lexical

More information

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 15-453 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY REVIEW for MIDTERM 1 THURSDAY Feb 6 Midterm 1 will cover everything we have seen so far The PROBLEMS will be from Sipser, Chapters 1, 2, 3 It will be

More information

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION FORMAL LANGUAGES, AUTOMATA AND COMPUTATION DECIDABILITY ( LECTURE 15) SLIDES FOR 15-453 SPRING 2011 1 / 34 TURING MACHINES-SYNOPSIS The most general model of computation Computations of a TM are described

More information

CS243, Logic and Computation Nondeterministic finite automata

CS243, Logic and Computation Nondeterministic finite automata CS243, Prof. Alvarez NONDETERMINISTIC FINITE AUTOMATA (NFA) Prof. Sergio A. Alvarez http://www.cs.bc.edu/ alvarez/ Maloney Hall, room 569 alvarez@cs.bc.edu Computer Science Department voice: (67) 552-4333

More information

Lexical Analysis: DFA Minimization & Wrap Up

Lexical Analysis: DFA Minimization & Wrap Up Lexical Analysis: DFA Minimization & Wrap Up Automating Scanner Construction PREVIOUSLY RE NFA (Thompson s construction) Build an NFA for each term Combine them with -moves NFA DFA (subset construction)

More information

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION CSE 105 THEORY OF COMPUTATION Spring 2016 http://cseweb.ucsd.edu/classes/sp16/cse105-ab/ Today's learning goals Sipser Ch 2 Define push down automata Trace the computation of a push down automaton Design

More information

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro Diniz

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro Diniz Compiler Design Spring 2010 Lexical Analysis Sample Exercises and Solutions Prof. Pedro Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292 pedro@isi.edu

More information

HKN CS/ECE 374 Midterm 1 Review. Nathan Bleier and Mahir Morshed

HKN CS/ECE 374 Midterm 1 Review. Nathan Bleier and Mahir Morshed HKN CS/ECE 374 Midterm 1 Review Nathan Bleier and Mahir Morshed For the most part, all about strings! String induction (to some extent) Regular languages Regular expressions (regexps) Deterministic finite

More information

1.3 Regular Expressions

1.3 Regular Expressions 51 1.3 Regular Expressions These have an important role in descriing patterns in searching for strings in many applications (e.g. awk, grep, Perl,...) All regular expressions of alphaet are 1.Øand are

More information

Foundations of

Foundations of 91.304 Foundations of (Theoretical) Computer Science Chapter 1 Lecture Notes (Section 1.3: Regular Expressions) David Martin dm@cs.uml.edu d with some modifications by Prof. Karen Daniels, Spring 2012

More information

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION CSE 105 THEORY OF COMPUTATION Spring 2018 http://cseweb.ucsd.edu/classes/sp18/cse105-ab/ Today's learning goals Sipser Ch 4.1 Explain what it means for a problem to be decidable. Justify the use of encoding.

More information

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION CSE 105 THEORY OF COMPUTATION Spring 2016 http://cseweb.ucsd.edu/classes/sp16/cse105-ab/ Today's learning goals Sipser Ch 3.3, 4.1 State and use the Church-Turing thesis. Give examples of decidable problems.

More information

Compiler Construction Lectures 13 16

Compiler Construction Lectures 13 16 Compiler Construction Lectures 13 16 Lent Term, 2013 Lecturer: Timothy G. Griffin 1 Generating Lexical Analyzers Source Program Lexical Analyzer tokens Parser Lexical specification Scanner Generator LEX

More information

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16) Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.

More information

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16) Compiler Construction Lent Term 2015 Lectures 13 --- 16 (of 16) 1. Return to lexical analysis : application of Theory of Regular Languages and Finite Automata 2. Generating Recursive descent parsers 3.

More information

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Compiler Design. Spring Lexical Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz Compiler Design Spring 2011 Lexical Analysis Sample Exercises and Solutions Prof. Pedro C. Diniz USC / Information Sciences Institute 4676 Admiralty Way, Suite 1001 Marina del Rey, California 90292 pedro@isi.edu

More information

THEORY OF COMPILATION

THEORY OF COMPILATION Lecture 04 Syntax analysis: top-down and bottom-up parsing THEORY OF COMPILATION EranYahav 1 You are here Compiler txt Source Lexical Analysis Syntax Analysis Parsing Semantic Analysis Inter. Rep. (IR)

More information

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa CS:4330 Theory of Computation Spring 2018 Regular Languages Finite Automata and Regular Expressions Haniel Barbosa Readings for this lecture Chapter 1 of [Sipser 1996], 3rd edition. Sections 1.1 and 1.3.

More information

Introduction to Language Theory and Compilation: Exercises. Session 2: Regular expressions

Introduction to Language Theory and Compilation: Exercises. Session 2: Regular expressions Introduction to Language Theory and Compilation: Exercises Session 2: Regular expressions Regular expressions (RE) Finite automata are an equivalent formalism to regular languages (for each regular language,

More information

Nondeterministic Finite Automata

Nondeterministic Finite Automata Nondeterministic Finite Automata Not A DFA Does not have exactly one transition from every state on every symbol: Two transitions from q 0 on a No transition from q 1 (on either a or b) Though not a DFA,

More information

Equivalence of DFAs and NFAs

Equivalence of DFAs and NFAs CS 172: Computability and Complexity Equivalence of DFAs and NFAs It s a tie! DFA NFA Sanjit A. Seshia EECS, UC Berkeley Acknowledgments: L.von Ahn, L. Blum, M. Blum What we ll do today Prove that DFAs

More information

Automata Theory. Lecture on Discussion Course of CS120. Runzhe SJTU ACM CLASS

Automata Theory. Lecture on Discussion Course of CS120. Runzhe SJTU ACM CLASS Automata Theory Lecture on Discussion Course of CS2 This Lecture is about Mathematical Models of Computation. Why Should I Care? - Ways of thinking. - Theory can drive practice. - Don t be an Instrumentalist.

More information

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

LING/C SC/PSYC 438/538. Lecture 17 Sandiway Fong LING/C SC/PSYC 438/538 Lecture 7 Sandiway Fong Today's Topic Review of ungraded homework Closure properties of FSA Practice! Homework out on Thursday From last time Ungraded Homework 6 apply the set-of-states

More information

CSE 311: Foundations of Computing. Lecture 23: Finite State Machine Minimization & NFAs

CSE 311: Foundations of Computing. Lecture 23: Finite State Machine Minimization & NFAs CSE : Foundations of Computing Lecture : Finite State Machine Minimization & NFAs State Minimization Many different FSMs (DFAs) for the same problem Take a given FSM and try to reduce its state set by

More information

Finite Automata Part Two

Finite Automata Part Two Finite Automata Part Two DFAs A DFA is a Deterministic Finite Automaton A DFA is defined relative to some alphabet Σ. For each state in the DFA, there must be exactly one transition defined for each symbol

More information

Building a befer mousetrap. Transi-on Graphs. L = { abba } Building a befer mousetrap. L = { abba } L = { abba } 2/16/15

Building a befer mousetrap. Transi-on Graphs. L = { abba } Building a befer mousetrap. L = { abba } L = { abba } 2/16/15 Building a efer mousetrap Let s uild an FA that only accepts the string aa from Σ = { a } Models of Computa-on Lecture #5 (a?er quiz) Chapter 6 Building a efer mousetrap Let s uild an FA that only accepts

More information

Lecture 3: Finite Automata

Lecture 3: Finite Automata Administrivia Lecture 3: Finite Automata Everyone should now be registered electronically using the link on our webpage. If you haven t, do so today! I d like to have teams formed by next Monday at the

More information

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Language Processing Systems Prof. Mohamed Hamada Software Engineering La. The University of izu Japan Syntax nalysis (Parsing) 1. Uses Regular Expressions to define tokens 2. Uses Finite utomata to recognize

More information

Closure under the Regular Operations

Closure under the Regular Operations September 7, 2013 Application of NFA Now we use the NFA to show that collection of regular languages is closed under regular operations union, concatenation, and star Earlier we have shown this closure

More information

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION CSE 105 THEORY OF COMPUTATION Spring 2017 http://cseweb.ucsd.edu/classes/sp17/cse105-ab/ Review of CFG, CFL, ambiguity What is the language generated by the CFG below: G 1 = ({S,T 1,T 2 }, {0,1,2}, { S

More information

COM364 Automata Theory Lecture Note 2 - Nondeterminism

COM364 Automata Theory Lecture Note 2 - Nondeterminism COM364 Automata Theory Lecture Note 2 - Nondeterminism Kurtuluş Küllü March 2018 The FA we saw until now were deterministic FA (DFA) in the sense that for each state and input symbol there was exactly

More information

Great Theoretical Ideas in Computer Science. Lecture 4: Deterministic Finite Automaton (DFA), Part 2

Great Theoretical Ideas in Computer Science. Lecture 4: Deterministic Finite Automaton (DFA), Part 2 5-25 Great Theoretical Ideas in Computer Science Lecture 4: Deterministic Finite Automaton (DFA), Part 2 January 26th, 27 Formal definition: DFA A deterministic finite automaton (DFA) M =(Q,,,q,F) M is

More information

Reversal of regular languages and state complexity

Reversal of regular languages and state complexity Reversal of regular languages and state complexity Juraj Šeej Institute of Computer Science, Faculty of Science, P. J. Šafárik University Jesenná 5, 04001 Košice, Slovakia juraj.seej@gmail.com Astract.

More information

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION CSE 105 THEORY OF COMPUTATION "Winter" 2018 http://cseweb.ucsd.edu/classes/wi18/cse105-ab/ Today's learning goals Sipser Ch 4.1 Explain what it means for a problem to be decidable. Justify the use of encoding.

More information

UNIT II REGULAR LANGUAGES

UNIT II REGULAR LANGUAGES 1 UNIT II REGULAR LANGUAGES Introduction: A regular expression is a way of describing a regular language. The various operations are closure, union and concatenation. We can also find the equivalent regular

More information

CSE Theory of Computing: Homework 3 Regexes and DFA/NFAs

CSE Theory of Computing: Homework 3 Regexes and DFA/NFAs CSE 34151 Theory of Computing: Homework 3 Regexes and DFA/NFAs Version 1: Fe. 6, 2018 Instructions Unless otherwise specified, all prolems from the ook are from Version 3. When a prolem in the International

More information

TDDD65 Introduction to the Theory of Computation

TDDD65 Introduction to the Theory of Computation TDDD65 Introduction to the Theory of Computation Lecture 2 Gustav Nordh, IDA gustav.nordh@liu.se 2012-08-31 Outline - Lecture 2 Closure properties of regular languages Regular expressions Equivalence of

More information

Lecture 4: Finite Automata

Lecture 4: Finite Automata Administrivia Lecture 4: Finite Automata Everyone should now be registered electronically using the link on our webpage. If you haven t, do so today! I dliketohaveteamsformedbyfriday,ifpossible,butnextmonday

More information

Non-regular languages

Non-regular languages Non-regular languages (Pumping Lemma) Fall 2017 Costas Busch - RPI 1 Non-regular languages { a n n : n 0} { vv R : v{ a, }*} Regular languages a* * c a c( a )* etc... Fall 2017 Costas Busch - RPI 2 How

More information

Lecture 3: Finite Automata

Lecture 3: Finite Automata Administrivia Lecture 3: Finite Automata Everyone should now be registered electronically using the link on our webpage. If you haven t, do so today! I d like to have teams formed by next Wednesday at

More information

Nondeterminism. September 7, Nondeterminism

Nondeterminism. September 7, Nondeterminism September 7, 204 Introduction is a useful concept that has a great impact on the theory of computation Introduction is a useful concept that has a great impact on the theory of computation So far in our

More information

Formal Languages. We ll use the English language as a running example.

Formal Languages. We ll use the English language as a running example. Formal Languages We ll use the English language as a running example. Definitions. A string is a finite set of symbols, where each symbol belongs to an alphabet denoted by. Examples. The set of all strings

More information

CMSC 330: Organization of Programming Languages. Theory of Regular Expressions Finite Automata

CMSC 330: Organization of Programming Languages. Theory of Regular Expressions Finite Automata : Organization of Programming Languages Theory of Regular Expressions Finite Automata Previous Course Review {s s defined} means the set of string s such that s is chosen or defined as given s A means

More information

Context Free Grammars

Context Free Grammars Automata and Formal Languages Context Free Grammars Sipser pages 101-111 Lecture 11 Tim Sheard 1 Formal Languages 1. Context free languages provide a convenient notation for recursive description of languages.

More information

Intro to Theory of Computation

Intro to Theory of Computation Intro to Theory of Computation 1/19/2016 LECTURE 3 Last time: DFAs and NFAs Operations on languages Today: Nondeterminism Equivalence of NFAs and DFAs Closure properties of regular languages Sofya Raskhodnikova

More information

Classes and conversions

Classes and conversions Classes and conversions Regular expressions Syntax: r = ε a r r r + r r Semantics: The language L r of a regular expression r is inductively defined as follows: L =, L ε = {ε}, L a = a L r r = L r L r

More information

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata CISC 4090: Theory of Computation Chapter Regular Languages Xiaolan Zhang, adapted from slides by Prof. Werschulz Section.: Finite Automata Fordham University Department of Computer and Information Sciences

More information

Foundations of Informatics: a Bridging Course

Foundations of Informatics: a Bridging Course Foundations of Informatics: a Bridging Course Week 3: Formal Languages and Semantics Thomas Noll Lehrstuhl für Informatik 2 RWTH Aachen University noll@cs.rwth-aachen.de http://www.b-it-center.de/wob/en/view/class211_id948.html

More information

Nondeterministic Finite Automata and Regular Expressions

Nondeterministic Finite Automata and Regular Expressions Nondeterministic Finite Automata and Regular Expressions CS 2800: Discrete Structures, Spring 2015 Sid Chaudhuri Recap: Deterministic Finite Automaton A DFA is a 5-tuple M = (Q, Σ, δ, q 0, F) Q is a finite

More information

CS 455/555: Finite automata

CS 455/555: Finite automata CS 455/555: Finite automata Stefan D. Bruda Winter 2019 AUTOMATA (FINITE OR NOT) Generally any automaton Has a finite-state control Scans the input one symbol at a time Takes an action based on the currently

More information

Formal Languages, Automata and Models of Computation

Formal Languages, Automata and Models of Computation CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 5 School of Innovation, Design and Engineering Mälardalen University 2011 1 Content - More Properties of Regular Languages (RL)

More information

jflap demo Regular expressions Pumping lemma Turing Machines Sections 12.4 and 12.5 in the text

jflap demo Regular expressions Pumping lemma Turing Machines Sections 12.4 and 12.5 in the text On the menu today jflap demo Regular expressions Pumping lemma Turing Machines Sections 12.4 and 12.5 in the text 1 jflap Demo jflap: Useful tool for creating and testing abstract machines Finite automata,

More information

Computational Theory

Computational Theory Computational Theory Finite Automata and Regular Languages Curtis Larsen Dixie State University Computing and Design Fall 2018 Adapted from notes by Russ Ross Adapted from notes by Harry Lewis Curtis Larsen

More information

Lecture 11 Context-Free Languages

Lecture 11 Context-Free Languages Lecture 11 Context-Free Languages COT 4420 Theory of Computation Chapter 5 Context-Free Languages n { a b : n n { ww } 0} R Regular Languages a *b* ( a + b) * Example 1 G = ({S}, {a, b}, S, P) Derivations:

More information

CSCE 551: Chin-Tser Huang. University of South Carolina

CSCE 551: Chin-Tser Huang. University of South Carolina CSCE 551: Theory of Computation Chin-Tser Huang huangct@cse.sc.edu University of South Carolina Church-Turing Thesis The definition of the algorithm came in the 1936 papers of Alonzo Church h and Alan

More information

Lecture 3: Nondeterministic Finite Automata

Lecture 3: Nondeterministic Finite Automata Lecture 3: Nondeterministic Finite Automata September 5, 206 CS 00 Theory of Computation As a recap of last lecture, recall that a deterministic finite automaton (DFA) consists of (Q, Σ, δ, q 0, F ) where

More information

CSCI 2670 Introduction to Theory of Computing

CSCI 2670 Introduction to Theory of Computing CSCI 267 Introduction to Theory of Computing Agenda Last class Reviewed syllabus Reviewed material in Chapter of Sipser Assigned pages Chapter of Sipser Questions? This class Begin Chapter Goal for the

More information

Sri vidya college of engineering and technology

Sri vidya college of engineering and technology Unit I FINITE AUTOMATA 1. Define hypothesis. The formal proof can be using deductive proof and inductive proof. The deductive proof consists of sequence of statements given with logical reasoning in order

More information

CS 275 Automata and Formal Language Theory

CS 275 Automata and Formal Language Theory CS 275 Automata and Formal Language Theory Course Notes Part II: The Recognition Problem (II) Chapter II.4.: Properties of Regular Languages (13) Anton Setzer (Based on a book draft by J. V. Tucker and

More information

UNIT-VIII COMPUTABILITY THEORY

UNIT-VIII COMPUTABILITY THEORY CONTEXT SENSITIVE LANGUAGE UNIT-VIII COMPUTABILITY THEORY A Context Sensitive Grammar is a 4-tuple, G = (N, Σ P, S) where: N Set of non terminal symbols Σ Set of terminal symbols S Start symbol of the

More information

Computational Models - Lecture 4

Computational Models - Lecture 4 Computational Models - Lecture 4 Regular languages: The Myhill-Nerode Theorem Context-free Grammars Chomsky Normal Form Pumping Lemma for context free languages Non context-free languages: Examples Push

More information

Theory of Computation (II) Yijia Chen Fudan University

Theory of Computation (II) Yijia Chen Fudan University Theory of Computation (II) Yijia Chen Fudan University Review A language L is a subset of strings over an alphabet Σ. Our goal is to identify those languages that can be recognized by one of the simplest

More information

Decidability (intro.)

Decidability (intro.) CHAPTER 4 Decidability Contents Decidable Languages decidable problems concerning regular languages decidable problems concerning context-free languages The Halting Problem The diagonalization method The

More information

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX

Part 4 out of 5 DFA NFA REX. Automata & languages. A primer on the Theory of Computation. Last week, we showed the equivalence of DFA, NFA and REX Automata & languages A primer on the Theory of Computation Laurent Vanbever www.vanbever.eu Part 4 out of 5 ETH Zürich (D-ITET) October, 12 2017 Last week, we showed the equivalence of DFA, NFA and REX

More information

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY 5-453 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY NON-DETERMINISM and REGULAR OPERATIONS THURSDAY JAN 6 UNION THEOREM The union of two regular languages is also a regular language Regular Languages Are

More information

Finite Automata. Finite Automata

Finite Automata. Finite Automata Finite Automata Finite Automata Formal Specification of Languages Generators Grammars Context-free Regular Regular Expressions Recognizers Parsers, Push-down Automata Context Free Grammar Finite State

More information

Fooling Sets and. Lecture 5

Fooling Sets and. Lecture 5 Fooling Sets and Introduction to Nondeterministic Finite Automata Lecture 5 Proving that a language is not regular Given a language, we saw how to prove it is regular (union, intersection, concatenation,

More information

CS 121, Section 2. Week of September 16, 2013

CS 121, Section 2. Week of September 16, 2013 CS 121, Section 2 Week of September 16, 2013 1 Concept Review 1.1 Overview In the past weeks, we have examined the finite automaton, a simple computational model with limited memory. We proved that DFAs,

More information

Automata and Languages

Automata and Languages Automata and Languages Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan Nondeterministic Finite Automata with empty moves (-NFA) Definition A nondeterministic finite automaton

More information

Deterministic Finite Automata (DFAs)

Deterministic Finite Automata (DFAs) Algorithms & Models of Computation CS/ECE 374, Spring 29 Deterministic Finite Automata (DFAs) Lecture 3 Tuesday, January 22, 29 L A TEXed: December 27, 28 8:25 Chan, Har-Peled, Hassanieh (UIUC) CS374 Spring

More information

UNIT-II. NONDETERMINISTIC FINITE AUTOMATA WITH ε TRANSITIONS: SIGNIFICANCE. Use of ε-transitions. s t a r t. ε r. e g u l a r

UNIT-II. NONDETERMINISTIC FINITE AUTOMATA WITH ε TRANSITIONS: SIGNIFICANCE. Use of ε-transitions. s t a r t. ε r. e g u l a r Syllabus R9 Regulation UNIT-II NONDETERMINISTIC FINITE AUTOMATA WITH ε TRANSITIONS: In the automata theory, a nondeterministic finite automaton (NFA) or nondeterministic finite state machine is a finite

More information

Deterministic Finite Automata (DFAs)

Deterministic Finite Automata (DFAs) Algorithms & Models of Computation CS/ECE 374, Fall 27 Deterministic Finite Automata (DFAs) Lecture 3 Tuesday, September 5, 27 Sariel Har-Peled (UIUC) CS374 Fall 27 / 36 Part I DFA Introduction Sariel

More information

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing Plan for Today Recall Predictive Parsing when it works and when it doesn t necessary to remove left-recursion might have to left-factor Error recovery for predictive parsers Predictive parsing as a specific

More information

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever. ETH Zürich (D-ITET) September,

Automata & languages. A primer on the Theory of Computation. Laurent Vanbever.  ETH Zürich (D-ITET) September, Automata & languages A primer on the Theory of Computation Laurent Vanbever www.vanbever.eu ETH Zürich (D-ITET) September, 24 2015 Last week was all about Deterministic Finite Automaton We saw three main

More information

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall

CSE443 Compilers. Dr. Carl Alphonce 343 Davis Hall CSE443 Compilers Dr. Carl Alphonce alphonce@buffalo.edu 343 Davis Hall http://www.cse.buffalo.edu/faculty/alphonce/sp17/cse443/index.php https://piazza.com/class/iybn4ndqa1s3ei Syllabus Posted on website

More information

Compiling Techniques

Compiling Techniques Lecture 5: Top-Down Parsing 26 September 2017 The Parser Context-Free Grammar (CFG) Lexer Source code Scanner char Tokeniser token Parser AST Semantic Analyser AST IR Generator IR Errors Checks the stream

More information

Theory of Computation Prof. Raghunath Tewari Department of Computer Science and Engineering Indian Institute of Technology, Kanpur

Theory of Computation Prof. Raghunath Tewari Department of Computer Science and Engineering Indian Institute of Technology, Kanpur Theory of Computation Prof. Raghunath Tewari Department of Computer Science and Engineering Indian Institute of Technology, Kanpur Lecture 10 GNFA to RE Conversion Welcome to the 10th lecture of this course.

More information

INF Introduction and Regular Languages. Daniel Lupp. 18th January University of Oslo. Department of Informatics. Universitetet i Oslo

INF Introduction and Regular Languages. Daniel Lupp. 18th January University of Oslo. Department of Informatics. Universitetet i Oslo INF28 1. Introduction and Regular Languages Daniel Lupp Universitetet i Oslo 18th January 218 Department of Informatics University of Oslo INF28 Lecture :: 18th January 1 / 33 Details on the Course consists

More information

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 43

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 43 Finite Automata Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Fall 2018 Dantam (Mines CSCI-561) Finite Automata Fall 2018 1 / 43 Outline Languages Review Traffic Light Example Deterministic Finite

More information

Computational Models Lecture 2 1

Computational Models Lecture 2 1 Computational Models Lecture 2 1 Handout Mode Ronitt Rubinfeld and Iftach Haitner. Tel Aviv University. March 16/18, 2015 1 Based on frames by Benny Chor, Tel Aviv University, modifying frames by Maurice

More information