PL/0 lexical analyzer based on C language

Requirement: Input a program, analyze the identifiers, basic words, operators, delimiters, constants and output

Algorithm idea: first define a two-dimensional array, store the read program segment, and then process the two-dimensional array

The specific processing method is: first read a character, if it is a letter or number, continue to read it later, if it is of the same type, store it in a temporary array (if it is a letter, compare it with the reserved word to determine whether it is a reserved word, If it is a number, output it), otherwise jump out and make a judgment; if it is an operator, judge the current character and the next character, and judge whether it is two consecutive operators such as >= or <=; the delimiter is judged directly.


Above code:


#include<stdio.h>
#include<string.h>
#include <stdbool.h> //Introduce boolean type, there is no boolean type in stdio.h file


#define lineLength 100 //fix the length of each line
#define primLength 10 //The length of the read string (identifier, etc.)
#define numLength 20 //The length of the read constant


//Store the basic word
char mPrimary[13][20]= {"begin","call", "const","do","end","if","odd","procedure","read","then","var","while","write"};//Order, The premise of binary search is order (in alphabetical order)


char text[lineLength][lineLength];//Store the read content




void main()
{


int i=0;
bool flag=false; //Determine whether the input is over , and whether the last marker (.) of the two-dimensional array is read, and the pl/0 language program ends with a dot.


//read input into a 2D character array
int t;
for (t = 0; t < 100;



        if (!*text[t]) //When the input row is empty if the condition is satisfied, execute the break statement to jump out of the loop.
        {
            break;
        }
    }


    //Process the two-dimensional character array,
     int mid;
    for(i=0; i<t; i++)
    {
        char prim[primLength];//Create a new character array to store basic words or variables Name
        char num[numLength];//Create a new character array to store constants


        for(mid=0; mid<primLength; mid++) //Empty the array
            prim[mid]='\0';


        for(mid=0; mid <numLength; mid++) //clear the array
            num[mid]='\0';




        int j;
        for(j=0; text[i][j]!='\0'; j++)
        {
            if(text[i ][j]=='.')
            {
                printf(". delimiter/terminator\n");
                flag=true;
                break;
            }
            else if(text[i][j]==' ')
            {
                continue;
            }
            else if(text[i][j]>='a'&&text[i][j]<= 'z')
            {
                for(mid=0; (text[i][j]>='a'&&text[i][j]<='z')||(text[i][j]>=' 0'&&text[i][j]<='9'); j++,mid++) //Read in the string
                    prim[mid]=text[i][j];
                j--;//Because j is used to judge At the end of the loop, one character needs to be returned here


                for(mid=0; prim[mid]!='\0'; mid++) //output string
                    printf("%c",prim[mid]);


                int head =0,tail=12;//Binary search,Find whether it is a basic word, there are 13 reserved words
                int mid2;//Intermediate comparison variable
                while(head<=tail)
                {
                    mid2=(head+tail)/2;
                    if(strcmp(prim,mPrimary[mid2])<=0) //String comparison uses strcpy() function
                        tail=mid2-1;
                    if (strcmp(prim,mPrimary[mid2])>=0)
                        head=mid2+1;
                }
                if(head-1>tail)//The search is successful, it is determined as the basic word
                    printf("Basic word/%s \n", mPrimary[mid2]);
                else
                    printf("identifier\n");


                for(mid=0; prim[mid]!='\0'; mid++) //Empty the array for next time read
                    prim[mid]= '\0';
            }
            else if(text[i][j]>='0'&&text[i][j]<='9')
            {
                for(mid=0; text[i][j]>='0'&&text[i][j]<='9'; j++,mid++)//Read in numbers
                    num[mid]=text[i] [j];
                j--;//Because j is used to judge the end of the loop, a character needs to be returned here


                for(mid=0; num[mid]!='\0'; mid++) //Output numbers, And clear it for next time reading
                {
                    printf("%c",num[mid]);
                    num[mid]='\0';
                }
                printf(" constant\n");
            }
            else//processing "+ - * /,#() etc"
            {
                switch(text[i][j])
                {
                case '<':
                {
                    if(text[i][j+1]=='=')
                    {
                        printf("%c%c operator/less than or equal sign\n",text[i][j],text[i][j+1]);
                        j++;
                    }
                    else
                    {
                        printf("%c operator\n ",text[i][j]);
                    }
                    break;
                }
                case '>':
                {
                    if(text[i][j+1]=='=')
                    {
                        printf("%c%c operator/greater than Equal sign\n",text[i][j],text[i][j+1]);
                        j++;
                    }
                    else
                    {
                        printf("%c operator\n",text[i][j]);
                    }
                    break;
                }
                case ':':
                {
                    if(text[i][j+1]=='=')
                    {
                        printf( "%c%c operator\n",text[i][j],text[i][j+1]);
                        j++;
                    }
                    else
                    {
                        printf("%c operator\n",text[i] [j]);
                    }
                    break;
                }
                case '+':
                case '-':
                case '*':
                case '/':
                {
                    printf("%c operator\n",text[i][j]);
                    break;
                }
                case '(':
                {
                    printf("%c delimiter\n",text[i] [j]);
                    break;
                }
                case ')':
                {
                    printf("%c delimiter\n",text[i][j]);
                    break;
                }
                case ',':
                {
                    printf("%c delimiter /comma\n",text[i][j]);
                    break;
                }
                case ';':
                {
                    printf("%c delimiter/semicolon\n",text[i][j]);
                    break;
                }
                default:
                    printf("%c identifier\n",text[i][j]);
                }


            }
        }
        if(flag)
            break;
    }




}



Run the screenshot:




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325436814&siteId=291194637
Recommended