14
TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP NO. : 9

TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

Embed Size (px)

Citation preview

Page 1: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

TRANSITION DIAGRAM BASED LEXICAL ANALYZER

andFINITE AUTOMATA

Class date : 12 August, 2013Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020GROUP NO. : 9

Page 2: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

Stream of input characters (source)

State Transition Machine 1

State Transition Machine 2

State Transition Machine 3

Token

Lex Compiler

pattern1 pattern 2 pattern 3

Page 3: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

Simulating the transition machines :

Input string : x1, x2 ,x 3 …… x n

• Scan the input using forward pointer• Machine accepts or rejects based on the

transition function• Match the longest prefix of the input• If accepted, token is produced

Page 4: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

Two approach to simulate the machine :

Approach 1 : Sequential simulation

• Sequentially simulate the transition machines

• If the previous machine fails, reset the forward pointer

• Start the next transition machine

Page 5: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

Approach 2 : Parallel simulation

• Simulate all transition diagrams in parallel

• The machine stops when no match is found

Page 6: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

Pattern : Define based on regular expression

Lex compiler

C Compiler

a.out

Pattern

a.out

Source code

tokens

generate state transition machines NFA : Advantage

DFA : advantage

Page 7: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

FINITE AUTOMATA

• Recognize regular languages• Accepts or rejects a possible input string • Two types : 1. Non deterministic finite automata(NFA) 2. Deterministic finite automata(DFA)

Page 8: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

NFA

Definition :

N={ Σ, S, so , F, Δ}where Σ : set of input symbolS : finite set of statesso : start state

F : finite set of final states Δ : transition function (S × Σ → P(S)) where P(S) is the power set of S

Page 9: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

More about NFA

• Given an input a, NFA allows to go from one state to a multiple state

a a

• ϵ-transition is allowed ϵ

Page 10: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

DFA

Definition :

D={ Σ, S, so , f, δ}where Σ : set of input symbolS : finite set of statesso : start state

F : finite set of final states δ : transition function (δ : S × Σ → S)

Page 11: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

More on DFA

• No ϵ-transition is allowed

• For a given input a, DFA allows to moves from one state to a single state

a

Page 12: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

Simulating DFAInput : - a string x - DFA with start state so , accepting

states F and transition function detect

Output : “yes” if accepts and “no” if rejects

Page 13: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

Algorithm :

s= so ;

c= read_next_char(); //read_next_char() returns the next //character while(c!=‘\0’) { s=detect(s,c); c=read_next_char(); } if(s ϵ F) return yes; else return no;

Page 14: TRANSITION DIAGRAM BASED LEXICAL ANALYZER and FINITE AUTOMATA Class date : 12 August, 2013 Prepared by : Karimgailiu R Panmei Roll no. : 11CS10020 GROUP

THE END