 Hi everyone, welcome to this video session of implementation of transition diagram which is a part of a lexical analyzer in a compiler part, one of the phase of the compiler. At the end of this session, the learner will be able to write a small programming code for a given transition diagram or a pattern. The transition diagram is implemented by the regular expressions. Here this transition diagram where it is having state 0 as the beginning state and these are the accepting states 2, 3, 4, 5, 7 and 8. Every accepting state is returning two things here. One is the category of the token that is a token itself, reload and the attribute of it. What kind of token it is? So if the transition diagram moving from 0 to 6 that is this greater than and then equal to it is moving to state number 7, it means that it is greater than equal to token. Next transition diagram is for identifier and keyword. So any identifier or a keyword, how the identifier and keyword is there? It is starting with a letter and it may have letter or digit if it is an identifier for a keyword it is always all the characters are there. So it is accepting that and it is returning two things. One is a return, it is calling a function called as get token. Get token is returning id as a token if it is an identifier and the keyword itself if it is a keyword. And if the keyword is there then install id function is returning 0 because it is nothing to do with the symbol table entry there. But if it is an identifier then the entry for that particular identifier is made in the symbol table if it is not there and it is returning. If entry is there then it is returning the pointer of it. This transition diagram is for unsigned numbers let us see that. So this is for the exponential values, the fractional exponential values these you can see that few tokens lexems it is reading this is 12.3 A4 and this one that is accepted by this. And it is calling install num function it is installing the number in the literal table and it is returning a pointer to that. The next transition diagram is for the simple float fractional values the examples like 12.3 10.15 these numbers are recognized by this one and it is generating a token for it. This is for the numeric constant like 3, 1, 4 simple numeric constant like a simple digit 9 and this number some so simply one by one all the digits it is reading and again some it is installing the function in that one install num which is returning making the entry in the literal table and it is returning a token also. This transition diagram is for wide spaces wide spaces means the extra lines number of lines tab spaces or simple spaces and those and it is returning WS. Now some basics are there the transition diagrams are usually used for following one by one determination of the tokens one by one all the tokens are returned by this transition diagram. What happens if the failure occurs in our current transition diagram there are many transition diagrams so what happens if failure occurs? So if failure occurs then the transition diagram retracts it will come to the beginning of the lexem again where it has failed and it will go to the next transition diagram that is we call it as activation of the next transition diagram. And if failure occurs in all transition diagram then it means that it is not at all accepting the lexem. So there might be an error in that so it is detecting that error and it is calling the error recovery routine and error recovery routine will recover that error. Every wide space is expected to occur frequently it means every after every lexem there might be wide spaces are there. So for wide spaces the transition diagram for this is put near the beginning of every transition diagram and in this implementation every small state of a transition diagram is having a small segment of a code we are going to implement that later on. Let us see few basic things in that state is a variable we are using here for storing the current state of the transition diagram. Start is the variable we are using which is holding the start state of the current transition diagram as there are many transition diagrams. A lexical value it is storing the value returned by install id function or install num function. And fail is a transition function which is called for whenever a failure occurs in any transition diagram. There are two things it is doing one thing is if the current transition diagram fails it retracts the forward pointer it will come to the beginning of the lexem and it will start the next transition diagram and that lexem is given to the next transition diagram to accept. And if everywhere it fails every transition diagram then the fail calls the error recovery routine. So these are the transition diagrams we have we are considering here for our implementation. So there are six transition diagrams are there every transition diagram look at here this is starting with 0 and these are the accepting states. This transition diagram for keywords and identifier it is starting with 9. The transition diagram for fractional values, exponential values it is starting with 12. Remember these one because these states we are going to use for the implementation. So state number 20 is the start state of the next transition diagram which is for the fractional value similarly for the wide spaces these are the things. So total 6 transition diagrams we are considering and for this we are implementing a code. Let us see that. So this is the implementation of a transition diagram. At the beginning we are initializing three things here make the state current state as 0 as we are starting with the first transition diagram first state is 0 and the first transition diagrams start state is 0. So start is holding the transition diagrams initial state. Transition value is just declared because it is holding the values written by install idea and install now. The fail function, fail function calls for the failure of the transition diagram. So if the first transition diagram fails which is the beginning initial state of the first transition diagram 0 then it has to move to the next transition diagram which is starting with 9 state number 9 so that is this. Similarly if the second transition diagram fails then it has to move to the next transition diagram which is starting with 12. Like this the remaining transition diagrams are there and if all are failure then it is calling a recover function. So this is the thing fail calls when the transition diagram for a given diagram is failing and it moves to the next transition diagram. You can see here return start means it is returning the start state of the transition diagram. And the next part of this is there is one more function called as next token. What this next token is doing it is returning all the tokens. So this is a continuous process which we are implementing now in this for every case every state as a case we are writing a segment of a code. So for white space if it is at the beginning as I told that white space is usually there for the beginning of the every transition diagram. So this is the first transition diagram. So if it is reading the white space it has to do nothing. So that is done by this one. Otherwise if no white space is there it has to implement its own states. So state from state 0 by reading less than it is moving to state number 1. Similarly if state equal to is there then it has to move to next state as 5. So these are the three types of states. So these are for the relational operators. Similarly we are implementing all case 1 to 8 whatever the number of states are there for relational operator. Now pause the video here and write what is the purpose of the next care function. You can see the code here just imagine for what purpose we are using the next care function. The purpose of the next care function is it is reading next character in a buffer. As there is the characters the lexems are stored in a buffer. So one by one the pointer is moving and every time the next character is moving one by one by incrementing the forward pointer there. So it is returning a character pointed by that forward pointer and that is incremented. Now the implementation case number 25 that is state number 25. Case number 25 it is reading the next character and what is the next thing? If it is a digit then it has to move to next state that is state number 26 else it has to call the fail function. If it is the state number 26 then it has to check whether it is a digit and then it is moving to the next state. So and if it is a number simple number then it is accepted and whenever it is reaching to the last transition diagram at the end of that it is retracting by one and it is calling an install num function. This install num function is entering the number in the literal table and it is returning that. You can see in the earlier part of code here as case number 9 is there it is reading letter if it is reading letter it will go to the next state. If it is a digit or letter for case number 10 then it is moving to the next it is retaining in that state only. So like this for every state a part of code is written. In this one you can see that it is returning the retract function it is retracting the forward pointer by one character so that whatever the next thing is there it has to read the next lexam again from the beginning. So it is not ignoring that part whatever the extra character it has read. So that is the purpose of the retraction and then it is calling the install num function and it is making the number entry in the literal table. So this is what the implementation of the transition diagram. These are my references thank you.