Introduction

Now we’re going to complete our function getToken() of the class LexicalAnalyzer. What we left was the case where the first letter of a prospective word was reached from the file stream.

Because there are operations represented by a single character. This case handles all the possible LEXEMS, including errors. This case is assigned every time the function is called, so we always will use this case.

Beginning with, we’re gonna create a new switch within case 0 to determine the variety of first characteres that we can have:

switch (takenChar.toLatin1()) {

So, next step is to control the process to every case,

If the character is a pound, then go to case 4:

case '#':
                nStates=4;
                break;

 

For the cases of one character, we can handle it in the same way, because we just need to determine whether is a operator or not:

            case '&':
            case '|':
            case '!':
                L.m_type=LEXEM::OPERATOR;
                L.m_sToken+=takenChar;
                m_nLastPos=m_Stream.pos();
                return L;
			break;

Next character is the hyphen of the implication operation:

 case '-':
                L.m_sToken+=takenChar;
                nStates=1;
                break;

Equally to the bi implication operation beginning with the right bracket:

 

            case '<':
                L.m_sToken+=takenChar;
                nStates=2;
                break;

If we find a end of line character space sequence, we can handle it with the LEXEM type EOL:

            case '\n':
                L.m_type=LEXEM::EOL;
                L.m_sToken+=takenChar;
                m_nLastPos=m_Stream.pos();
                return L;

There are characters that we don’t need to handle, as the carrier return, blank space and the tab space sequence:

            case ' ':
            case '\r':
            case '\t':
                break;

It’s important to noting the parenthesis, that are an important part of the rules from our source, because they let us organize rules in hierarchy (hi-are-keys).

           case '(':
                L.m_type=LEXEM::OPENBRACKET;
                L.m_sToken=takenChar;
                m_nLastPos=m_Stream.pos();
                return L;
			break;
            case ')':
                L.m_type=LEXEM::CLOSEDBRACKET;
                L.m_sToken=takenChar;
                m_nLastPos=m_Stream.pos();
                return L;
			break;

Finally, the default is for the LEXEM IDs type , if the character is not an ID then it is defined as an errors:

 

           default:
                if(takenChar.isLetter() || takenChar=='_'){
                    L.m_sToken+=takenChar;
                    nStates=3;
                }
                else{
                    L.m_sToken+=takenChar;
                    L.m_type=LEXEM::ERROR;
                    m_nLastPos=m_Stream.pos();
                    return L;
                }
			break;

Thank you for sharing and posting your comments. I’m going to attach my social media information below the video.

Video

 

Leave a Reply

Your email address will not be published.