Finite Automata & Language Theory

Slides:



Advertisements
Similar presentations
4b Lexical analysis Finite Automata
Advertisements

CPSC Compiler Tutorial 4 Midterm Review. Deterministic Finite Automata (DFA) Q: finite set of states Σ: finite set of “letters” (input alphabet)
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Winter 2007SEG2101 Chapter 81 Chapter 8 Lexical Analysis.
Lexical Analysis (2 Lectures). CSE244 Compilers 2 Overview Basic Concepts Regular Expressions –Language Lexical analysis by hand Regular Languages Tools.
Chapter 3: Lexical Analysis
Lecture 3 Goals: Formal definition of NFA, acceptance of a string by an NFA, computation tree associated with a string. Algorithm to convert an NFA to.
Thompson Construction, Subset Construction Thompson Construction, Subset Construction Continue….. LECTURE 7.
CH3.1 CSE4100 Chapter 3: Lexical Analysis Prof. Steven A. Demurjian Computer Science & Engineering Department The University of Connecticut 371 Fairfield.
1 Chapter 3 Scanning – Theory and Practice. 2 Overview Formal notations for specifying the precise structure of tokens are necessary –Quoted string in.
CS-5800 Theory of Computation II PROJECT PRESENTATION By Quincy Campbell & Sandeep Ravikanti.
Lecture # 3 Chapter #3: Lexical Analysis. Role of Lexical Analyzer It is the first phase of compiler Its main task is to read the input characters and.
Topic #3: Lexical Analysis EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
Lexical Analyzer (Checker)
Overview of Previous Lesson(s) Over View  An NFA accepts a string if the symbols of the string specify a path from the start to an accepting state.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP.
1 November 1, November 1, 2015November 1, 2015November 1, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University Azusa.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
Pembangunan Kompilator.  A recognizer for a language is a program that takes a string x, and answers “yes” if x is a sentence of that language, and.
CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.
Overview of Previous Lesson(s) Over View  Symbol tables are data structures that are used by compilers to hold information about source-program constructs.
Overview of Previous Lesson(s) Over View  Algorithm for converting RE to an NFA.  The algorithm is syntax- directed, it works recursively up the parse.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
Chapter 2 Scanning. Dr.Manal AbdulazizCS463 Ch22 The Scanning Process Lexical analysis or scanning has the task of reading the source program as a file.
using Deterministic Finite Automata & Nondeterministic Finite Automata
Overview of Previous Lesson(s) Over View  A token is a pair consisting of a token name and an optional attribute value.  A pattern is a description.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
1 An automaton is a computation that determines whether a given string belongs to a specified language A finite state machine (FSM) is an automaton that.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
Converting Regular Expressions to NFAs Empty string   is a regular expression denoting  {  } a is a regular expression denoting {a} for any a in 
Lecture 2 Compiler Design Lexical Analysis By lecturer Noor Dhia
COMP 3438 – Part II - Lecture 3 Lexical Analysis II Par III: Finite Automata Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ. 1.
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Finite automate.
Lecture 2 Lexical Analysis
Chapter 3: Lexical Analysis and Flex
Finite-State Machines (FSMs)
Lexical analysis Finite Automata
Finite-State Machines (FSMs)
Chapter 2 Finite Automata
Introduction to Lexical Analysis
The time complexity for e-closure(T).
Two issues in lexical analysis
Recognizer for a Language
Review: NFA Definition NFA is non-deterministic in what sense?
Chapter 3: Lexical Analysis
Chapter 2 FINITE AUTOMATA.
Jaya Krishna, M.Tech, Assistant Professor
Decision Properties of Regular Languages
Non-deterministic Finite Automata (NFA)
Principles of Computing – UFCFA3-30-1
Recognition of Tokens.
Lecture 4: Lexical Analysis II: From REs to DFAs
Animated Conversion of Regular Expressions to C Code
Theory of Computation Lecture #
This method is used for converting an
Finite Automata.
4b Lexical analysis Finite Automata
Chapter 3. Lexical Analysis (2)
4b Lexical analysis Finite Automata
Instructor: Aaron Roth
CSC312 Automata Theory Transition Graphs Lecture # 9
Chapter 1 Regular Language
NFAs and Transition Graphs
Lecture 5 Scanning.
Presentation transcript:

Finite Automata & Language Theory A recognizer that takes an input string & determines whether it’s a valid sentence of the language Non-Deterministic : Has more than one alternative action for the same input symbol. Deterministic : Has at most one action for a given input symbol. Both types are used to recognize regular expressions.

NFAs & DFAs Non-Deterministic Finite Automata (NFAs) easily represent regular expression, but are somewhat less precise. Deterministic Finite Automata (DFAs) require more complexity to represent regular expressions, but offer more precision. We’ll review both plus conversion algorithms, i.e., NFA  DFA and DFA  NFA

Non-Deterministic Finite Automata An NFA is a mathematical model that consists of : S, a set of states , the symbols of the input alphabet move, a transition function. move(state, symbol)  set of states move : S  {}  Pow(S) A state, s0  S, the start state F  S, a set of final or accepting states.

We’ll see examples of both ! Representing NFAs Transition Diagrams : Transition Tables: Number states (circles), arcs, final states, … More suitable to representation within a computer We’ll see examples of both !

What Language is defined ? What is the Transition Table ? Example NFA start 3 b 2 1 a S = { 0, 1, 2, 3 } s0 = 0 F = { 3 }  = { a, b } What Language is defined ? What is the Transition Table ? i n p u t  (null) moves possible j i  Switch state but do not use any input symbol a b state { 0, 1 } { 0 } 1 -- { 2 } 2 -- { 3 }

How Does An NFA Work ? -OR- 3 2 1 start 3 b 2 1 a Given an input string, we trace moves If no more input & in final state, ACCEPT EXAMPLE: Input: ababb -OR- move(0, a) = 0 move(0, b) = 0 move(0, a) = 1 move(1, b) = 2 move(2, b) = 3 ACCEPT ! move(0, a) = 1 move(1, b) = 2 move(2, a) = ? (undefined) REJECT !

Handling Undefined Transitions We can handle undefined transitions by defining one more state, a “death” state, and transitioning all previously undefined transition to this death state. start 3 b 2 1 a 4 a, b 

NFA- Regular Expressions & Compilation Problems with NFAs for Regular Expressions: 1. Valid input might not be accepted 2. NFA may behave differently on the same input Relationship of NFAs to Compilation: 1. Regular expression “recognized” by NFA 2. Regular expression is “pattern” for a “token” 3. Tokens are building blocks for lexical analysis 4. Lexical analyzer can be described by a collection of NFAs. Each NFA is for a language token.

Given the regular expression : (a (b*c)) | (a (b | c+)?) Second NFA Example Given the regular expression : (a (b*c)) | (a (b | c+)?) Find a transition diagram NFA that recognizes it.

Second NFA Example - Solution Given the regular expression : (a (b*c)) | (a (b | c+)?) Find a transition diagram NFA that recognizes it. 4 2 1 3 5 start c b  a String abbc can be accepted.

Alternative Solution Strategy 3 2 b c a 1 a (b*c) 6 5 7 c a 4 b a (b | c+)? Now that you have the individual diagrams, “or” them as follows:

Using Null Transitions to “OR” NFAs 3 2 b c a 1 6 5 7 4 

Other Concepts Not all paths may result in acceptance. 3 2 1 start 3 b 2 1 a aabb is accepted along path : 0  0  1  2  3 BUT… it is not accepted along the valid path: 0  0  0  0  0

Deterministic Finite Automata A DFA is an NFA with the following restrictions:  moves are not allowed For every state s S, there is one and only one path from s for every input symbol a  . Since transition tables don’t have any alternative options, DFAs are easily simulated via an algorithm. s  s0 c  nextchar; while c  eof do s  move(s,c); end; if s is in F then return “yes” else return “no”

What Language is Accepted? Example - DFA start 3 b 2 1 a What Language is Accepted? Recall the original NFA: start 3 b 2 1 a

Conversion : NFA  DFA Algorithm Algorithm Constructs a Transition Table for DFA from NFA Each state in DFA corresponds to a SET of states of the NFA Why does this occur ?  moves non-determinism Both require us to characterize multiple situations that occur for accepting the same string. (Recall : Same input can have multiple paths in NFA) Key Issue : Reconciling AMBIGUITY !

Converting NFA to DFA – 1st Look 8 5 4 7 3 6 2 1  b a c From State 0, Where can we move without consuming any input ? This forms a new state: 0,1,2,6,8 What transitions are defined for this new state ?

The Resulting DFA Which States are FINAL States ? 1, 2, 5, 6, 7, 8 1, 2, 4, 5, 6, 8 0, 1, 2, 6, 8 3 c b a Which States are FINAL States ? D C A B c b a How do we handle alphabet symbols not defined for A, B, C, D ?

Algorithm Concepts NFA N = ( S, , s0, F, MOVE ) -Closure(s) : s S : set of states in S that are reachable from s via -moves of N that originate from s. -Closure(T) : T  S : NFA states reachable from all t  T on -moves only. move(T,a) : T  S, a : Set of states to which there is a transition on input a from some t  T No input is consumed These 3 operations are utilized by algorithms / techniques to facilitate the conversion process.

Illustrating Conversion – An Example Start with NFA: (a | b)*abb 1 2 3 5 4 6 7 8 9 10  a b start First we calculate: -closure(0) (i.e., state 0) -closure(0) = {0, 1, 2, 4, 7} (all states reachable from 0 on -moves) Let A={0, 1, 2, 4, 7} be a state of new DFA, D.

Conversion Example – continued (1) 2nd , we calculate : a : -closure(move(A,a)) and b : -closure(move(A,b)) a : -closure(move(A,a)) = -closure(move({0,1,2,4,7},a))} adds {3,8} ( since move(2,a)=3 and move(7,a)=8) From this we have : -closure({3,8}) = {1,2,3,4,6,7,8} (since 36 1 4, 6 7, and 1 2 all by -moves) Let B={1,2,3,4,6,7,8} be a new state. Define Dtran[A,a] = B. b : -closure(move(A,b)) = -closure(move({0,1,2,4,7},b)) adds {5} ( since move(4,b)=5) From this we have : -closure({5}) = {1,2,4,5,6,7} (since 56 1 4, 6 7, and 1 2 all by -moves) Let C={1,2,4,5,6,7} be a new state. Define Dtran[A,b] = C.

Conversion Example – continued (2) 3rd , we calculate for state B on {a,b} a : -closure(move(B,a)) = -closure(move({1,2,3,4,6,7,8},a))} = {1,2,3,4,6,7,8} = B Define Dtran[B,a] = B. b : -closure(move(B,b)) = -closure(move({1,2,3,4,6,7,8},b))} = {1,2,4,5,6,7,9} = D Define Dtran[B,b] = D. 4th , we calculate for state C on {a,b} a : -closure(move(C,a)) = -closure(move({1,2,4,5,6,7},a))} = {1,2,3,4,6,7,8} = B Define Dtran[C,a] = B. b : -closure(move(C,b)) = -closure(move({1,2,4,5,6,7},b))} = {1,2,4,5,6,7} = C Define Dtran[C,b] = C.

Conversion Example – continued (3) 5th , we calculate for state D on {a,b} a : -closure(move(D,a)) = -closure(move({1,2,4,5,6,7,9},a))} = {1,2,3,4,6,7,8} = B Define Dtran[D,a] = B. b : -closure(move(D,b)) = -closure(move({1,2,4,5,6,7,9},b))} = {1,2,4,5,6,7,10} = E Define Dtran[D,b] = E. Finally, we calculate for state E on {a,b} a : -closure(move(E,a)) = -closure(move({1,2,4,5,6,7,10},a))} = {1,2,3,4,6,7,8} = B Define Dtran[E,a] = B. b : -closure(move(E,b)) = -closure(move({1,2,4,5,6,7,10},b))} = {1,2,4,5,6,7} = C Define Dtran[E,b] = C.

Conversion Example – continued (4) This gives the transition table Dtran for the DFA of: Dstates Input Symbol a b A B C B B D C B C E B C D B E A C B D E start b a

Algorithm For Subset Construction push all states in T onto stack; initialize -closure(T) to T; while stack is not empty do begin pop t, the top element, off the stack; for each state u with edge from t to u labeled  do if u is not in -closure(T) do begin add u to -closure(T) ; push u onto stack end computing the -closure

Algorithm For Subset Construction – (2) initially, -closure(s0) is only (unmarked) state in Dstates; while there is unmarked state T in Dstates do begin mark T; for each input symbol a do begin U := -closure(move(T,a)); if U is not in Dstates then add U as an unmarked state to Dstates; Dtran[T,a] := U end

Regular Expression to NFA Construction We now focus on transforming a Reg. Expr. to an NFA This construction allows us to take: Regular Expressions (which describe tokens) To an NFA (to characterize language) To a DFA (which can be “computerized”) The construction process is component-wise Builds NFA from components of the regular expression in a special order with particular techniques. NOTE: Construction is “syntax-directed” translation, i.e., syntax of regular expression is determining factor for NFA construction and structure.

Motivation: Construct NFA For: b: ab:  | ab : a* ( | ab )* :

Motivation: Construct NFA For:  start i f : a : b: ab:  | ab : a* ( | ab )* : a start 1 b start A B a b  A B start 1

Construction Algorithm : R.E.  NFA Construction Process : 1st : Identify subexpressions of the regular expression   symbols r | s rs r* 2nd : Characterize “pieces” of NFA for each subexpression

Piecing Together NFAs 1. For  in the regular expression, construct NFA L()  start i f 2. For a   in the regular expression, construct NFA a start i f L(a)

Piecing Together NFAs – continued(1) 3.(a) If s, t are regular expressions, N(s), N(t) their NFAs s|t has NFA:  i f N(s) N(t) L(s)  L(t) start where i and f are new start / final states, and -moves are introduced from i to the old start states of N(s) and N(t) as well as from all of their final states to f.

Piecing Together NFAs – continued(2) 3.(b) If s, t are regular expressions, N(s), N(t) their NFAs st (concatenation) has NFA: start i f N(s) N(t) L(s) L(t) Alternative: overlap  where i is the start state of N(s) (or new under the alternative) and f is the final state of N(t) (or new). Overlap maps final states of N(s) to start state of N(t).

Piecing Together NFAs – continued(3) 3.(c) If s is a regular expressions, N(s) its NFA, s* (Kleene star) has NFA:  N(s)  start i f  where : i is new start state and f is new final state -move i to f (to accept null string) -moves i to old start, old final(s) to f -move old final to old start (WHY?)

Properties of Construction Let r be a regular expression, with NFA N(r), then N(r) has #of states  2*(#symbols + #operators) of r N(r) has exactly one start and one accepting state Each state of N(r) has at most one outgoing edge a or at most two outgoing ’s BE CAREFUL to assign unique names to all states !

Parse Tree for this regular expression: Detailed Example See example 3.16 in textbook for (a | b)*abb 2nd Example - (ab*c) | (a(b|c*)) Parse Tree for this regular expression: r13 r12 r5 r3 r11 r4 r9 r10 r8 r7 r6 r0 r1 r2 b * c a | ( ) What is the NFA? Let’s construct it !

Detailed Example – Construction(1) b r2: c b  r1: r4 : r1 r2 b  c r5 : r3 r4 b  a c

Detailed Example – Construction(2)  r8: r11: a r7: b r6: c c  r9 : r7 | r8 b r10 : r9 c  r12 : r11 r10 b a

Detailed Example – Final Step r13 : r5 | r12 b  a c 1 6 5 4 3 8 2 10 9 12 13 14 11 15 7 16 17

Direct Simulation of an NFA s  s0 c  nextchar; while c  eof do s  move(s,c); end; if s is in F then return “yes” else return “no” DFA simulation S  -closure({s0}) c  nextchar; while c  eof do S  -closure(move(S,c)); end; if SF then return “yes” else return “no” NFA simulation