 Hello everyone, welcome to the session on implementation of transition diagrams. Now transition diagrams are used for lexical analyzer implementation. In this video the learning outcomes are at the end of this session you will able to write a programming code for a given transition diagram or a pattern. Consider this transition diagram where it is having these many states, it is for the relational operators how many relational operators are there like less than equal to greater than not equal to equal to greater than greater than equal to these one. And every transition diagram whenever it is coming to the accepting state it is returning two things here one as a token another one as an attribute value. Another transition diagram this transition diagram is for identifiers and keywords it is reading a letter and then a combinations of letter or digits. It is returning two things one is a token and other one is this entry to that particular particular identifier. Like get token it is returning a token as ID if it is an identifier and if it is a keyword the keyword itself is returning and the install ID function is returning zero if it is a keyword and it is returning a pointer to the symbol table entry for a particular identifier. This is the next transition diagram which is used for simple unsigned numbers with the fractional and exponential values. You can see the examples here like 12.3, E4 or 10, E15 so these are the values which are which it is this particular transition diagram is accepting. The next transition diagram is only for fractional numbers you can go for and for examples 12.3 is it is accepting like one as a digit three as another digit point then again three and finally for other it is coming to the accepting state for 10.15 one as a digit zero as a digit point as a fractional value here again one as a digit five as a deal and finally it is coming to the accepting state. Now another transition diagram is there for a simple numeric constant so digit or the digit combinations it is reading 3, 1, 4 or 9 single digit or multiple digits or all and a function install num is installing the numeric the number in the literal table and it is returning the pointer to it. For white space this is a transition diagram simply it is reading new line tab space or spaces and all and it is returning WS as a token. Now the implementations basics of transition diagram the transition diagram means it is followed by one by one it is going from one state to another one and it is returning the token the next token is returned. What happens if a failure occurs? If a failure occurs in one transition diagram then it is moving to the second transition diagram and it is retracting the forward pointer it means if a particular lexem is in the middle it is there and the transition diagram is not moving to the next transition diagram so in that case what will happen that it is again coming back to the forward pointer beginning and then again that is given to the next transition diagram to read and it is activating the next transition diagram and if all the all the transition diagrams whatever implemented are failed then the lexical analyzer is calling inocking an error recovery routine. What is this error recovery routine? What it is doing is it means that if a no transition diagram is accepting the lexem it means there is something some error in the lexem so it is showing that error and it is recovering so that it will go ahead for an other lexem to check. If recovery is not there then it may happen that the it will lexical analyzer will be saying there only it is not it cannot move ahead so for that error recovery is used. White spaces are usually used at the beginning of the transition diagram so that the white spaces are removed or ignored and every state in the transition diagram is converted as a segment of a code. Let us see here we will consider the basics here we have taken the three variables like state what is the variable state it is used for the current state the variable start is used for the start state of the current transition diagram because we have considered here few transition diagram so every transition diagram is having more one more start state so whatever the current transition diagram where our lexem is moving so that state is stored in start and lexical value it is returning the pointers returned by install id or install num accordingly it may be identifier or a number. One more function is there which we are implementing for a transition diagram called as fail and this fail is usually invoked whenever a transition diagram is failed for reading the point reading a particular pattern and what this fail function will do it will see what is the current transition diagram it will retract means it will come back to the beginning of the lexem again and whatever the next transition diagram is there the pointer moves to the next transition diagram so that it will move ahead to check that thing so consider this scenario here whatever the earlier transition diagrams are there I have combined here all these one so this is the first transition diagram the second one the third one fourth one fifth one and the sixth one the first transition diagram you can see it is starting with zero and it is having the accepting state like through two three four seven eight the second transition diagram is starting with nine the third transition diagram is starting with 12 you can see here that the last state of this transition diagram is eight so the next state is nine here the new transition diagram similarly last eight is 11 here the next state of the next transition diagram is 12 so immediately after that now here the last state is 19 the next transition diagram is starting with 20, the fifth transition diagram is with 25 and 28 which is the last next of this one. So, for our implementation we are considering these five transition diagrams. So, you should take care of these one that we have these many transition diagrams and we have to implement it. Now, see this coding part, this is the code which we have generated, so the first thing here is for state what is the state current state is taken as 0, initially the first transition diagram we have taken is the first transition diagram which is starting with 0 itself, so start is holding the current transition diagram state as 0, the lexical value is nothing here we have just declared. The function fail is initialized here, what did fail does that is this is calling as I told earlier that is whenever a transition fails transition diagram fails for a given one then it is moving to the next transition diagram for that this fail is implemented. See what we have implemented forward is equal to token beginning it means whatever currently wherever it has hanged now currently again the pointer is moving to the beginning of the lexum that is the first thing means the same lexum again has to go to the next transition diagram. Now in the switch case what we have done if it is the 0th one case 0 means your start is 0 it means it is if it is the first transition diagram what is the next transition diagram next transition diagram is starting at 9, so therefore start is taken as 9, so it will move to the next transition diagram. If the current transition diagram start state is 9 then the next transition diagram is starting with 12 which we have seen earlier here, so accordingly. So if transition diagram 3 is there it has to go to transition diagram 4 which is starting with state number 20 similarly 25 and so on. If nothing is there then it will call an error recovery routine it means our transition our whatever the transition diagrams are there, so that lexum is not accepted by anyone so error is there it will show the error and it will go for the error recovery and finally what it is returning it is returning a start what is that is the next transition diagram starts it. The next one is called as a function called as next token what it is doing it is returning the next token. This while loop which is the infinite loop because when we are stopping when we have read all the combinations then we are stopping. So the state, so if 0th state is there it is going for the next one it means it has to read if it is a blank or a tab or a new line then it has to be in the 0th state only otherwise if less than is there then it has to go to state number 1, if greater than is there it has to go to state number next one 6 like this it is moving and this is for the white space removal and these like this one we go on for all the relational operators so else one it will call the state fail so that it will go to the next transition diagram. So for 1 to 8 all the states we are implementing here for all the relational operators. Now pause the video and write here that what is the purpose of calling next care function here see the purpose of next care function is reading the next character from a buffer. So whenever we move on one by one all the characters are ready that is it is returning a character pointed by forward pointer and automatically the forward pointer is incrementing. Now see this code here what we are doing if case 9 is there for the second transition diagram it is reading letter and then it is moving to the next state as 10 number state if it is a letter or a digit the state is as it is so that is written otherwise install id is caught. So the second transition diagram is implemented here similarly for the next one and retract one is used for coming back by one character here so this is for the numeric constant case number 25 and 26 for the last transition diagram it is entering this install num is entering the symbol table the data into that particular num literal table. These are the references thank you.