Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Assignment: The first phase of compilation is called scanning or lexical analysi

ID: 3881170 • Letter: A

Question

Assignment:

The first phase of compilation is called scanning or lexical analysis. This phase interprets the input program as a sequence of characters and produces a sequence of tokens, which will be used by the parser.

Write a C/C++ program that implements a simple scanner for a source file given as a command-line argument. The format of the tokens is described below. You may assume that the input is syntactically correct. Your program should build a symbol table (a hash table is a good choice), which contains an entry for each token that was found in the input. When all the input has been read, your program should produce a summary report that includes a list of all the tokens that appeared in the input, the number of times each token appears in the input and the class of each token. Your program should also list how many times tokens of each class appeared in the input.

The token format:

   keyword -> if | then | else | begin | end

   identifier -> character | character identifier

   integer -> digit | digit integer

   real -> integer.integer

   special -> ( | ) | [ | ] | + | - | = | , | ;

   digit -> 0|1|2|3|4|5|6|7|8|9

   character -> a|b|c ... |z|A|B|C ... |Z

More details:

Case is not used to distinguish keywords or identifiers.

The delimiters are space, tab, newline, and the special characters.

The token classes that should be recognized are keyword, identifier, integer, real and special.

Your final program must compile and run using a C++ compiler (your choice).

Explanation / Answer

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<iostream.h>
#include<stdlib.h>


int isoperator(char ch)
{
    if(ch=='+')
        return 1;
    elseif(ch=='-')
        return 2;
    elseif(ch=='*')
        return 3;
    elseif(ch=='/')
        return 4;
    elseif(ch=='=')
        return 5;
    elsereturn 0;
}

/*int issymbol(int symcounter,char symbol[][10],char currstr[])
{
    for(int i=0;i<symcounter;i++)
      {
        if(strcmp(symbol[i],currstr)==0)
            return i;
      }
    return -1;
}*/
void main()
{
    char instr[50];
    char currstr[50];
    char symbol[10][10];
    int scantab[6][5]={ 1, 2, 3,-1,-1,
               -1,-1,-1,-1,-1,
               -1, 2, 2, 2,-1,
               -1,-1, 3,-1, 4,
               -1,-1, 5,-1,-1,
               -1,-1, 5,-1,-1 };
    int counter=0,j;
    int currstate=0,symcounter=0;
    char currsym;
    int i=0;
    int count=0;
    clrscr();

    printf("ENTER THE STRING : ");
    scanf("%[^ ]",&instr);

    while(instr[counter]!='')
      {
        count=0;
        while(instr[counter++]==' ') {}

        counter--;

        while(instr[counter]!=' ' && instr[counter]!='')
            currstr[count++]=instr[counter++];
//        printf("%d",count);
        currstr[count]='';
//        printf("%s",currstr);
        currstate=0;
        int i=0;

        while(i<strlen(currstr))
          {

            if(isoperator(currstr[i])>0)
                j=0;
            elseif(isalpha(currstr[i]))
                j=1;
            elseif(isdigit(currstr[i]))
                j=2;
            elseif(currstr[i]=='_')
                j=3;
            elseif(currstr[i]=='.')
                j=4;

            else
              {
                printf("ERROR IN INPUT ! PLEASE TRY AGAIN WITH SUITABLE STRING :");
                getch();
                exit(0);
              }

            currstate=scantab[currstate][j];
            i++;
          }

        if(currstate==1)
            //printf(" <op,%d>",isoperator(currstr[--i]));
            printf(" <op> ");
        elseif(currstate==2)
          {
               /*    int temp;
            temp=issymbol(symcounter,symbol,currstr);
            if(temp>=0)
                printf(" <id#%d>",temp+1);
            else
              {
                strcpy(symbol[symcounter],currstr);
                symcounter++;
                printf(" <id#%d>",symcounter);
              }*/

              printf(" <id> ");
          }
        elseif(currstate==3)
            printf(" <int>");
        elseif(currstate==5)
            printf(" <real>");
        elseif(currstate==-1)
            printf(" error ..");
      }
    getch();
}

token types all


***Tokens and Types***
Types: integers(0), operators(1), illegal(2),
end of text(4), and oversize integers(5)
Type input text, EOF to quit
123 * 723456 + 12
Token = 123 Type = 0
Token = * Type = 1
Token = 72345 Type = 4
Token = + Type = 1
Token = 12 Type = 0
45+23*7
Token = 45 Type = 0
Token = + Type = 1
Token = 23 Type = 0
Token = * Type = 1
Token = 7 Type = 0
x = 8;
Token = x Type = 2
Token = = Type = 2
Token = 8 Type = 0
Token = ; Type = 2
'136D

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote