Lexical Analysis Part 2 Professor Yihjia Tsai Tamkang University.

Slides:



Advertisements
Similar presentations
4b Lexical analysis Finite Automata
Advertisements

CSC 361NFA vs. DFA1. CSC 361NFA vs. DFA2 NFAs vs. DFAs NFAs can be constructed from DFAs using transitions: Called NFA- Suppose M 1 accepts L 1, M 2 accepts.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Finite Automata Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture 20Oct 28, 2010Carnegie Mellon University.
1 Introduction to Computability Theory Lecture4: Regular Expressions Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Lecture 3UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 3.
1 FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY (For next time: Read Chapter 1.3 of the book)
Finite Automata Finite-state machine with no output. FA consists of States, Transitions between states FA is a 5-tuple Example! A string x is recognized.
1 The scanning process Goal: automate the process Idea: –Start with an RE –Build a DFA How? –We can build a non-deterministic finite automaton (Thompson's.
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.
Lecture 3: Closure Properties & Regular Expressions Jim Hook Tim Sheard Portland State University.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Regular.
Lexical Analysis Part 1 Professor Yihjia Tsai Tamkang University.
CS5371 Theory of Computation Lecture 6: Automata Theory IV (Regular Expression = NFA = DFA)
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.
CS5371 Theory of Computation Lecture 4: Automata Theory II (DFA = NFA, Regular Language)
Lecture 8 Sept 29, 2011 Regular expressions – examples Converting DFA to regular expression. (same works for NFA to r.e. conversion.) Converting R.E. to.
1.Defs. a)Finite Automaton: A Finite Automaton ( FA ) has finite set of ‘states’ ( Q={q 0, q 1, q 2, ….. ) and its ‘control’ moves from state to state.
Lecture 5UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 5.
Today Chapter 1: RE = Regular Languages, nonregular languages RL pumping lemma Chapter 2: Context-Free Languages (CFLs)
FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY
Nondeterminism (Deterministic) FA required for every state q and every symbol  of the alphabet to have exactly one arrow out of q labeled . What happens.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Regular Expressions (RE) Empty set Φ A RE denotes the empty set Empty string λ A RE denotes the set {λ} Symbol a A RE denotes the set {a} Alternation M.
Finite-State Machines with No Output
NFA ε - NFA - DFA equivalence. What is an NFA An NFA is an automaton that its states might have none, one or more outgoing arrows under a specific symbol.
REGULAR LANGUAGES.
Theory of Computation, Feodor F. Dragan, Kent State University 1 Regular expressions: definition An algebraic equivalent to finite automata. We can build.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
CHAPTER 1 Regular Languages
January 9, 2015CS21 Lecture 31 CS21 Decidability and Tractability Lecture 3 January 9, 2015.
NFA defined. NFA A Non-deterministic Finite-state Automata (NFA) is a language recognizing system similar to a DFA. It supports a level of non-determinism.
INHERENT LIMITATIONS OF COMPUTER PROGAMS CSci 4011.
CSCI 2670 Introduction to Theory of Computing September 13.
Finite Automata & Regular Languages Sipser, Chapter 1.
Lecture Notes 
using Deterministic Finite Automata & Nondeterministic Finite Automata
Chapter 5 Finite Automata Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.
 2004 SDU Lecture4 Regular Expressions.  2004 SDU 2 Regular expressions A third way to view regular languages. Say that R is a regular expression if.
1 Introduction to the Theory of Computation Regular Expressions.
Regular Languages Chapter 1 Giorgi Japaridze Theory of Computability.
Theory of Computation Automata Theory Dr. Ayman Srour.
Deterministic Finite Automata Nondeterministic Finite Automata.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
Topic 3: Automata Theory 1. OutlineOutline Finite state machine, Regular expressions, DFA, NDFA, and their equivalence, Grammars and Chomsky hierarchy.
Deterministic Finite-State Machine (or Deterministic Finite Automaton) A DFA is a 5-tuple, (S, Σ, T, s, A), consisting of: S: a finite set of states Σ:
WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Dr. Mohammad Nassef Department of Computer Science, Faculty of Computers and Information, Cairo University.
Lexical analysis Finite Automata
Chapter 2 Finite Automata
Theory of Computation Lecture # 9-10.
Recognizer for a Language
Finite Automata & Regular Languages
Chapter 2 FINITE AUTOMATA.
REGULAR LANGUAGES AND REGULAR GRAMMARS
Non-Determinism 12CS45 Finite Automata.
Non-Deterministic Finite Automata
CS 154, Lecture 3: DFANFA, Regular Expressions.
Principles of Computing – UFCFA3-30-1
COSC 3340: Introduction to Theory of Computation
Non Deterministic Automata
4b Lexical analysis Finite Automata
CSCI 2670 Introduction to Theory of Computing
CSCI 2670 Introduction to Theory of Computing
4b Lexical analysis Finite Automata
Chapter 1 Regular Language
CSCI 2670 Introduction to Theory of Computing
CSCI 2670 Introduction to Theory of Computing
Presentation transcript:

Lexical Analysis Part 2 Professor Yihjia Tsai Tamkang University

2 Regular Expressions and FA are Equivalent Regular expressions NFA DFA For every regular expression, there is a deterministic finite-state machine that defines the same language, and vice versa

3 Closure Operator TheClosure operator is defined as Closure(s) = { s } U states reachable from s using  transitions. Example: Closure(1) = {1,3} b  a a/b a start

4 Subset Construction for NFA-> DFA 1.Compute A = Closure(start) 2.Compute the set of states reachable from A on transition a, call this new set S ’ 3.Compute Closure(S ’ ) – this is the new state and label it with the next available label 4.Continue for all possible transitions from the current state for all applicable elements of  5.Repeat steps 2-4 for each new state

5 Comparison from Last Time a b c     NFA: D A C B a b b c c DFA:

6 Example: aa*b | aab* NFA: 3 1 a b 2 a 5 4 a a b

7 Example: aa*b | aab* DFA State NFA Set of States ab A{1}B B{2,4}CD C{2,5}EF D{3} E{2}ED F{3,5}G G{5}G

8 Resultant DFA D A a b B a C E a b b b F G b a How is this result different than before?

9 Generalized NFA Generalized Nondeterministic Finite Automaton M=(Q, , , q start, q accept ) with Q finite set of states  the input alphabet q start the start state q accept the accept state  :(Q\{q accept })  (Q\{q start })  R the transition function ( R is the set of regular expressions over  )

10 Example GNFA qSqS qAqA 01* 0 0* |  

11 Observation: This GNFA: recognizes the language L(R) Characteristics of GNFA ’ s   :(Q\{q accept })  (Q\{q start })  R The interior Q\{q accept,q start } is fully connected by  From q start only ‘outgoing transitions’ To q accept only ‘ingoing transitions’ Impossible q i  q j transitions are “  (q i,q j ) =  ” qSqS qAqA RRRR

12 DFA M  Equivalent GNFA M ’ Let M have k states Q={q 1,…,q k } - Add two states q accept and q start qSqS q1q1  - Connect q start to earlier q 1 : qiqi  qjqj - Complete missing transitions by qAqA qjqj  - Connect old accepting states to q accept - Join multiple transitions : qiqi 0 qjqj 1 becomes qiqi 0|1 qjqj

13 Remove Internal state of GNFA If the GNFA M has more than 2 states, rip internal q rip to get equivalent GNFA M’ by: - Removing state q rip : Q’=Q\{q rip } - Changing the transition function  by  ’(q i,q j ) =  (q i,q j ) | (  (q i,q rip )(  (q rip,q rip ))*  (q rip,q j )) for every q i  Q’\{q accept } and q j  Q’\{q start } qiqi R 4 |(R 1 R 2 *R 3 ) qjqj qiqi R2R2 qjqj R4R4 q rip R1R1 R3R3 =

14 Try at Home What is the regular expression for –Binary numbers, multiples of 2 –Strings of a’s and b’s with a at start and finish –Strings of a’s and b’s, no consecutive a’s

15 Try at Home What is the NFA for the following RE? ((a|b)*c) | (a b c*)

16 References Aho, A.V., R. Sethi, and J.D. Ullman, Compilers Principles, Techniques and Tools, Addison-Wesley, ISBN Appel, A., Modern Compiler Implementation In Java (2 nd Ed), Cambridge University Press, ISBN X.