College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.

Slides:



Advertisements
Similar presentations
1 Turing Machines and Equivalent Models Section 13.2 The Church-Turing Thesis.
Advertisements

CSE 311 Foundations of Computing I
Compiler Theory – 08/10(ppt) By Anindhya Sankhla 11CS30004 Group : G 29.
Compiler Designs and Constructions
Intermediate Code Generation
Finite Automata CPSC 388 Ellen Walker Hiram College.
CSE 311: Foundations of Computing Fall 2013 Lecture 23: Finite state machines and minimization.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Compiler Construction Sohail Aslam Lecture Finite Automaton of Items Then for every item A →  X  we must add an  -transition for every production.
Introduction to Programming Lesson 1. Objectives Skills/ConceptsMTA Exam Objectives Understanding Computer Programming Understand computer storage and.
1 Introduction to Computability Theory Lecture12: Decidable Languages 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.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
CS5371 Theory of Computation
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
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.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
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.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation.
NFA- to NFA conversion. Purpose This presentation presents an example execution of the algorithm which takes as input an NFA with -transitions and produces.
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
Algorithms. Introduction Before writing a program: –Have a thorough understanding of the problem –Carefully plan an approach for solving it While writing.
Fall 2004COMP 3351 Another NFA Example. Fall 2004COMP 3352 Language accepted (redundant state)
CSE 311: Foundations of Computing Fall 2014 Lecture 23: State Minimization, NFAs.
Rosen 5th ed., ch. 11 Ref: Wikipedia
CPSC 388 – Compiler Design and Construction Scanners – Finite State Automata.
CS-5800 Theory of Computation II PROJECT PRESENTATION By Quincy Campbell & Sandeep Ravikanti.
Automating Construction of Lexers. Example in javacc TOKEN: { ( | | "_")* > | ( )* > | } SKIP: { " " | "\n" | "\t" } --> get automatically generated code.
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.
CS412/413 Introduction to Compilers Radu Rugina Lecture 4: Lexical Analyzers 28 Jan 02.
Prof. Busch - LSU1 NFAs accept the Regular Languages.
Transition Diagrams Lecture 3 Wed, Jan 21, Building Transition Diagrams from Regular Expressions A regular expression consists of symbols a, b,
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.
 Regular Grammar and Regular Language [Def 3.1] Regular Grammar(use to in lexical analysis) Type 3 grammar(regular grammar, RG) Type 3 grammar(regular.
CMSC 330: Organization of Programming Languages Finite Automata NFAs  DFAs.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions NFAs  DFAs.
August 23, 2005 CSCI 2670 Introduction to Theory of Computing August 23, 2005.
UNIT - I Formal Language and Regular Expressions: Languages Definition regular expressions Regular sets identity rules. Finite Automata: DFA NFA NFA with.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2007.
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.
1 Chapter Constructing Efficient Finite Automata.
CSE 311 Foundations of Computing I Lecture 24 FSM Limits, Pattern Matching Autumn 2011 CSE 3111.
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.
Chapter 5 Finite Automata Finite State Automata n Capable of recognizing numerous symbol patterns, the class of regular languages n Suitable for.
Bottom Up Parsing CS 671 January 31, CS 671 – Spring Where Are We? Finished Top-Down Parsing Starting Bottom-Up Parsing Lexical Analysis.
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 Section 11.3 Constructing Efficient Finite Automata First we’ll see how to transform an NFA into a DFA. Then we’ll see how to transform a DFA into a.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2006.
Chapter 2-II Scanning Sung-Dong Kim Dept. of Computer Engineering, Hansung University.
CS412/413 Introduction to Compilers Radu Rugina Lecture 3: Finite Automata 25 Jan 02.
June 13, 2016 Prof. Abdelaziz Khamis 1 Chapter 2 Scanning – Part 2.
Chapter 2 Scanning From Regular Expression to DFA Gang S.Liu College of Computer Science & Technology Harbin Engineering University.
CSE 311 Foundations of Computing I
The time complexity for e-closure(T).
Compiler Construction
Two issues in lexical analysis
Recognizer for a Language
Review: NFA Definition NFA is non-deterministic in what sense?
Chapter 2 FINITE AUTOMATA.
CSE 311 Foundations of Computing I
Automating Scanner Construction
Regular Grammars.
Presentation transcript:

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -1- Compiler Construction Principles & Implementation Techniques Dr. Ying JIN Associate Professor Oct. 2007

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -2- Implementation of DFA

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -3- Implementation of DFA Objective (meaning of implementing a DFA) –Given a DFA which defines rules for a set of strings –Develop a program, which Read a string Check whether this string is accepted by the DFA If a string is accepted by a DFA, –Next state; –Stop in the final state; If a string is not accepted by a DAF, –No next state ( ⊥ ); –Not stop in the final state;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -4- Implementation of DFA Two ways –Basing on transforming table of DFA –Basing on graphical representation of DFA

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -5- Transforming Table based Implementation Main idea –Input : a string –Output: true if acceptable, otherwise false –Data structure Transforming table (two dimensional array T) –Two variables CurrentState: record current state; CurrentChar: record current character that is read in the string;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -6- Transforming Table based Implementation Main idea –General Algorithm 1.CurrentState = S 0 2. read the first character as CurrentChar 3. if CurrentChar is not the end of the string, if T(CurrentState,CurrentChar)  error, CurrentState = T(CurrentState,CurrentChar), read next character of the string as CurrentChar, goto 3; 4. if CurrentChar is the end of the string and CurrentState is one of the terminal states, return true; otherwise, return false.

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -7- Example abcd S0S0 S1 ⊥ S2S*S* S1 ⊥ ⊥ S2 S*S* ⊥⊥⊥ S*S* ⊥⊥ S*S* ⊥ 1) abbacc true 2) cab false

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -8- Transforming table for the DFA Variables: CurrentChar, CurrentState Read the string that want to be checked Checking process: Program structure for Table-based Implementation

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -9- each state corresponds to a case statement each edge corresponds to a goto statement for accept state, add one more branch, if current char is the end of the string then accept; Graph based Implementation of DFA i b j k a Li: case CurrentChar of a : goto Lj b : goto Lk other : Error( ) i Li: case CurrentChar of a : goto Lj b : goto Lk # : return true; other : Error( )

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -10- S0S0 a S1 S2 S*S* c d d a b c LS0: read character to CurrentChar; case CurrentChar of a: goto LS1; c: goto LS2: d: goto LS3; other: return false; LS1: read character to CurrentChar; case CurrentChar of b: goto LS1; d: goto LS2: other: return false;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -11- S0S0 a S1 S2 S*S* c d d a b c LS2: read character to CurrentChar; case CurrentChar of a: goto LS3; other: return false; LS3: read character to CurrentChar; case CurrentChar of c: goto LS2: #: return true; other: return false;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -12- Definition of NFA

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -13- Formal Definition – (  , SS, SS0, , TS ) –  ( alphabet ), set of allowed characters, each character can be called as input symbol; –SS = {S0, S1, S2, ……} , a finite set, each element is called state; –S 0  SS, set of start states –  : SS    power set of SS  {  }, transforming function –TS  SS, set of terminal (accept) states –Note :  is a function which accepts a state and a symbol and returns a set of states or  (no definition) ;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -14- Differences between DFA & NFA DFANFA Start stateOne start stateSet of start states  T (S, a) S’ or ⊥ {S1, …, Sn} or ⊥ implementationeasyNon-deterministic

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -15- Example of NFA :: {a, b, c, d} SS: {S 0, S1 0, S2, S*} Set of Start state: {S 0, S1 0 } Set of terminal states: {S * } :: {(S 0,a)  {S1 0, S* },(S 0,  )  {S2}, (S1 0,b)  {S1 0 }, (S1 0,  )  {S2}, (S2,  )  {S *}, (S *, c)  {S * }} S0S0 a S1 0 S2 S*S*  a   b c

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -16- From NFA to DFA

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -17- Main Idea Solve two problems –  edge  -closure (SS)  闭包 –Merging those edges with the same symbol NextStates(SS, a) Conversion of NFA to DFA –Using a set of states in NFA as one state in DFA –Assuring accepting the same set of strings

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -18- The process to calculate  -closure (  闭包 ) For a given NFA A, and a set of states SS, –  -closure( SS ) = SS ; –If there exists a state s in SS, which has a  -edge referring to a state s’ and s’  -closure( SS ), add to s’ to  -closure( SS ) ; –Repeat until there is no state having  -edge to states that is not in  -closure( SS ) ;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -19-  -closure (  闭包 ) -- Example S0S0 a S1 0 S2 S*S*  a   b c  -closure({S 0, S1 0 }) = ① {S 0, S1 0 } ② {S 0, S1 0, S2} ③ {S 0, S1 0, S2} ④ {S 0, S1 0, S2,S * }

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -20- Moving States For a given set of states SS and a symbol a in a NFA A, –NextStates( SS, a ) = {s | if there is a state s1  SS, and a edge s1 s in A } a

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -21- Moving States S0S0 a S1 0 S2 S*S*  a   b c NextStates({S 0, S1 0 }, a) = {S1 0, S * } NextStates({S 0, S1 0 }, b) = {S2}

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -22- Algorithm Given a NFA A = { , SS, SS 0, , TS} Generating an equivalent DFA A’ = { , SS’,S 0,  ’, TS’} Steps –(1) S 0 =  -closure(SS 0 ), add S 0 to SS’; –(2) select one state s from SS’, for any symbol a , let s’ = NextStates(  -closure(s), a), add (s, a)  s’ to  ’, if s’  SS’, add s’ to SS’; –(3) repeat (2) until all states are handled; –(4) for a state s in SS’, s = {S1,.., Sn}, if there exists Si  TS, then s is an accept state in A’, add s to TS’;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -23- Example S0S0 a S1 0 S2 S*S*  a   b c  = {a, b, c}, S0 =  -closure({S 0, S1 0 }) = {S 0, S1 0, S2,S * }, abc {S 0, S1 0, S2,S * }{S1 0, S *,S2} {S*}{S*} {S*}{S*} {S*}{S*}{S*}{S*}

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -24- Minimizing DFA

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -25- Problem Equivalent of two DFAs –If the set of strings accepted by two DFAs are the same; Among those DFAs that accept the same set of strings, the minimal DFA refers to the one that has minimal number of states; How this happens?

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -26- Equivalent DFAs S0S0 a S1 S4 * b d S3 * S2 d S0S0 a S1 b d S*S* There are states that accepting the same set of strings! {ad, bd}

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -27- Main Idea Equivalent states( 等价状态 ) –For two states s1 and s2 in a DFA, if treat s1 and s2 as start states and they accept the same set of strings, s1 and s2 will be called equivalent states; Two ways to minimizing DFA –Merging equivalent states; ( 状态合并 ) –Splitting non-equivalent states; (状态分离)

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -28- Algorithm Given a DFA A = { , SS, S 0, , TS} Generating an equivalent DFA A’ = { , SS’,S 0 ’,  ’, TS’} Splitting Steps –(1) two groups {non-terminal states}, {terminal states}; –(2) select one group of states SS i = {Si1,…, Sin}, replace SS i with split(SS i ); –(3) repeat (2) until all groups are handled; –(4) SS’ = set of groups; –(5) S 0 ’ is the group consisting of S 0 ; –(6) if the group consisting of terminal states of A, it is terminal state of A’; –(7)  ’: SS i SS j, if there is Si Sj in A, Si  SS i, Sj  SS j aa

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -29- Splitting a Set of States Given –a NFA A = { , SS, S 0, , TS }; –Groups of states {SS1, …, SSm}, SS1  ……  SSm = SS; –SS i = {Si1,…, Sin}, split(SS i ) is to split SS i into two group G1 and G2, –For j = 1 to n for any a , If (Si1,a )  Sk  (Sij, a)  Sl  Sk and Sl belong to the same group SSp, add Sij to G1; Otherwise, add Sij to G2;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -30- Simple Example S0S0 a S1 S4 * b d S3 * S2 d {S 0, S1, S2}, {S3 *, S4 * } {S 0 }, {S1, S2}, {S3 *, S4 * }

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -31- Assignment Define a DFA for accepting a set of binary numbers, each binary number can be divided by 4; ( 定义一个 DFA, 它所接受的符号串为能被 4 整除的 二进制数 ;) Implementation of the DFA above;

College of Computer Science & Technology Compiler Construction Principles & Implementation Techniques -32- Assignment From NFA to DFA, and minimize it; S0S0 a S1 S3 * S2 b a b a  