词法分析-----词法扫描器的设计实现 (2)

searchRReserve(char reserve[],char s), searchReserve(char reserve[][20],char s[])函数用于避免出现重复,既每出现一个token类别都到原有的类别数组中搜索一下。输出在Scanner()函数中直接传输到文件。

 

2.3 流程图

词法分析-----词法扫描器的设计实现

 

3 token序列识别流程图

 

 

三、源程序代码:(加入注释)

#include <iostream>

#include<stdio.h>

#include<string.h>

#include<stdlib.h>

 

 

using namespace std;

//保留字表

static char reserveWord[32][20] =

{

    "auto", "break", "case", "char", "const", "continue",

    "default", "do", "double", "else", "enum", "extern",

    "float", "for", "goto", "if", "int", "long",

    "register", "return", "short", "signed", "sizeof", "static",

    "struct", "switch", "typedef", "union", "unsigned", "void",

    "volatile", "while"

};

//界运算符

static char operatorOrDelimiter[36][10]=

{

    "+","-","*","http://www.likecs.com/","<","<=",">",">=","=","==",

    "!=",";","(",")","^",",","\"","\'","#","&",

    "&&","|","||","%","~","<<",">>","[","]","{",

    "}","\\",".","\?",":","!"

};

static  char IDentifierTbl[1000][20]= {""}; //标识符表i

static  char SingleChar[1000]= {""}; //单个字符表ct

static  char StringChar[1000][20]= {""}; //字符串表S

static  char ConstantTbl[1000][20]= {""}; //常数表C

int countct=0;//单个字符表ct

int countc=0;//常数表C

int counts=0; //字符串表S

int countt=0;//标识符表i

char resourceProject[10000];//输入的源程序存放处,最大可以存放10000个字符。

 

//查找保留字,若成功查找,则返回种别码

//否则返回-1,代表查找不成功,即为标识符

int searchReserve(char reserveWord[ ][20], char s[]);

 

bool IsLetter(char letter);

 

bool IsDigit(char digit);

 

void filterResource(char r[],int pProject);

 

void Scanner(int &syn,char resourceProject[],char token[],int &pProject);

 

int searchReserve(char reserve[][20],char s[])

{

    for(int i=0; reserve[i][0]!='\0'; i++)

    {

        if(strcmp(reserve[i],s)==0)

            return i+1;

    }

    return -1;

}

 

int searchRReserve(char reserve[],char s)

{

    for(int i=0; reserve[i]!='\0'; i++)

    {

        if(reserve[i]==s)

            return i+1;

    }

    return -1;

}

bool IsLetter(char letter)

{

    if(letter>='a'&&letter<='z'||letter>='A'&&letter<='Z'||letter=='_')

        return true;

    else

        return false;

}

 

bool IsDigit(char digit)

{

    if(digit>='0'&&digit<='9')

        return true;

    else

        return false;

}

void filterResource(char r[],int pProject)

{

    char tempString[10000];

    int count=0;

    for(int i=0; i<=pProject; i++)

    {

        if(r[i]=='http://www.likecs.com/'&&r[i+1]=='http://www.likecs.com/')

            while(r[i]!='\n')

                i++;

        if(r[i]=='http://www.likecs.com/'&&r[i+1]=='*')

        {

            i+=2;

            while(r[i]!='*'||r[i+1]!='http://www.likecs.com/')

            {

                i++;

                if(r[i]=='$')

                {

                    cout<<"注释出错"<<endl;

                    exit(0);

                }

            }

            i+=2;

        }

        if(r[i]!='\n'&&r[i]!='\t'&&r[i]!='\v'&&r[i]!='\r')

        {

            tempString[count++]=r[i];

        }

    }

    tempString[count]='\0';

    strcpy(r,tempString);

}

 

void Scanner(int &syn,char resourceProject[],char token[],int &pProject)

{

    FILE *fp1;

    if((fp1=fopen("E:\\2017.txt","at"))==NULL)

    {

        cout<<"cam not open";

        exit(0);

    }

 

    int i,count=0;//count用来做token指示器,收集有用字符

    char ch;

    ch=resourceProject[pProject];

    while(ch==' ')

    {

        pProject++;

        ch=resourceProject[pProject];

    }

    for(i=0; i<20; i++)

    {

        token[i]='\0';//收集前先清零

    }

    if(IsLetter(resourceProject[pProject]))

    {

        token[count++]=resourceProject[pProject];

        pProject++;

        while(IsLetter(resourceProject[pProject])||IsDigit(resourceProject[pProject]))

        {

            token[count++]=resourceProject[pProject];

            pProject++;

        }

        token[count]='\0';

        syn=searchReserve(reserveWord,token);

        if(syn!=-1)

        {

            cout<<"{"<<"k"<<","<<syn<<","<<reserveWord[syn-1]<<"}"<<endl;

            fprintf(fp1, "{k   ,   %d   ,%s }\n", syn,reserveWord[syn-1]);

        }

 

        if(syn==-1)

        {

 

            syn=searchReserve(IDentifierTbl,token);

            if(syn==-1)

            {

                strcpy(IDentifierTbl[countt++],token);

                syn=countt;

            }

            cout<<"{"<<"i"<<","<<syn<<","<<IDentifierTbl[syn-1]<<"}"<<endl;

            fprintf(fp1, "{i   ,   %d   ,%s }\n", syn,IDentifierTbl[syn-1]);

        }

    }

    else if(IsDigit(resourceProject[pProject]))

    {

        while(IsDigit(resourceProject[pProject]))

        {

            token[count++]=resourceProject[pProject];

            pProject++;

        }

        token[count]='\0';

        syn=searchReserve(ConstantTbl,token);

        if(syn==-1)

        {

            strcpy(ConstantTbl[countc++],token);

            syn=countc;

        }

        cout<<"{"<<"c"<<","<<syn<<","<<ConstantTbl[syn-1]<<"}"<<endl;

        fprintf(fp1, "{c   ,   %d   ,%s }\n", syn,ConstantTbl[syn-1]);

    }

    else if(resourceProject[pProject]=='\'')

    {

        pProject++;

        if(IsLetter(resourceProject[pProject]))

        {

            pProject++;

            if(resourceProject[pProject]=='\'')

            {

                pProject--;

                syn=searchRReserve(SingleChar,resourceProject[pProject]);

                if(syn==-1)

                {

                    SingleChar[countct++]=resourceProject[pProject];

                    syn=countct;

                }

            }

        }

        cout<<"{"<<"CT"<<","<<syn<<","<<resourceProject[syn-1]<<"}"<<endl;

        fprintf(fp1, "{CT   ,   %d  ,%c }\n", syn,resourceProject[syn-1]);

        pProject+=2;

    }

    else if(resourceProject[pProject]=='"')

    {

        pProject++;

        while(resourceProject[pProject]!='"')

        {

            token[count++]=resourceProject[pProject];

            pProject++;

        }

        token[count]='\0';

        syn=searchReserve(StringChar,token);

        if(syn==-1)

        {

            strcpy(StringChar[counts++],token);

            syn=counts;

        }

        cout<<"{"<<"S"<<","<<syn<<","<<StringChar[syn-1]<<"}"<<endl;

        fprintf(fp1, "{S   ,   %d   ,%s }\n", syn,StringChar[syn-1]);

        pProject++;

    }

    else if (ch == '+' || ch == '-' || ch == '*' || ch == 'http://www.likecs.com/' || ch == ';' || ch == '(' || ch == ')' || ch == '^'

             || ch == ',' || ch == '\"' || ch == '\'' || ch == '~' || ch == '#' || ch == '%' || ch == '['

             || ch == ']' || ch == '{' || ch == '}' || ch == '\\' || ch == '.' || ch == '\?' || ch == ':')

    {

        //若为运算符或者界符,查表得到结果

        token[0] = resourceProject[pProject];

        token[1] = '\0';//形成单字符串

        for (i = 0; i<36; i++)

        {

            //查运算符界符表

            if (strcmp(token, operatorOrDelimiter[i]) == 0)

            {

                syn = i+1;//获得种别码,使用了一点技巧,使之呈线性映射

                break;//查到即推出

            }

        }

        cout<<"{"<<"p"<<","<<syn<<","<<operatorOrDelimiter[syn-1]<<"}"<<endl;

        fprintf(fp1, "{p   ,   %d   ,%s }\n", syn,operatorOrDelimiter[syn-1]);

        pProject++;//指针下移,为下一扫描做准备

    }

    else if(resourceProject[pProject]=='<')

    {

        //<,<=,<<

        pProject++;

        if(resourceProject[pProject]=='=') syn=6;

        else if(resourceProject[pProject]=='<')

            syn=26;

        else

        {

            pProject--;

            syn=5;

        }

        cout<<"{"<<"p"<<","<<syn<<","<<operatorOrDelimiter[syn-1]<<"}"<<endl;

        fprintf(fp1, "{p   ,   %d   ,%s }\n", syn,operatorOrDelimiter[syn-1]);

        pProject++;

    }

    else if(resourceProject[pProject]=='>')

    {

        //>,>=,>>

        pProject++;

        if(resourceProject[pProject]=='=') syn=8;

        else if(resourceProject[pProject]=='>') syn=27;

        else

        {

            pProject--;

            syn=7;

        }

        cout<<"{"<<"p"<<","<<syn<<","<<operatorOrDelimiter[syn-1]<<"}"<<endl;

        fprintf(fp1, "{p   ,   %d   ,%s }\n", syn,operatorOrDelimiter[syn-1]);

        pProject++;

    }

    else if(resourceProject[pProject]=='=')

    {

        //=,==

        pProject++;

        if(resourceProject[pProject]=='=') syn=10;

        else

        {

            pProject--;

            syn=9;

        }

        cout<<"{"<<"p"<<","<<syn<<","<<operatorOrDelimiter[syn-1]<<"}"<<endl;

        fprintf(fp1, "{p   ,   %d   ,%s }\n", syn,operatorOrDelimiter[syn-1]);

        pProject++;

    }

    else if(resourceProject[pProject]=='!')

    {

        // !,!=

        pProject++;

        if(resourceProject[pProject]=='=') syn=11;

        else

        {

            pProject--;

            syn=36;

        }

       cout<<"{"<<"p"<<","<<syn<<","<<operatorOrDelimiter[syn-1]<<"}"<<endl;

        fprintf(fp1, "{p   ,   %d   ,%s }\n", syn,operatorOrDelimiter[syn-1]);

        pProject++;

    }

 

    else if(resourceProject[pProject]=='&')

    {

        //&,&&

        pProject++;

        if(resourceProject[pProject]=='&') syn=21;

        else

        {

            pProject--;

            syn=20;

        }

        cout<<"{"<<"p"<<","<<syn<<","<<operatorOrDelimiter[syn-1]<<"}"<<endl;

        fprintf(fp1, "{p   ,   %d   ,%s }\n", syn,operatorOrDelimiter[syn-1]);

        pProject++;

    }

    else if(resourceProject[pProject]=='|')

    {

        //|,||

        pProject++;

        if(resourceProject[pProject]=='|') syn=23;

        else

        {

            pProject--;

            syn=22;

        }

        cout<<"{"<<"p"<<","<<syn<<","<<operatorOrDelimiter[syn-1]<<"}"<<endl;

        fprintf(fp1, "{p   ,   %d   ,%s }\n", syn,operatorOrDelimiter[syn-1]);

        pProject++;

    }

 

    else if(resourceProject[pProject]=='$')

    {

        //$

        syn=0;

    }

    else

    {

        cout<<"error: no exist "<<resourceProject[pProject]<<endl;

        exit(0);

    }

    fclose(fp1);

}

int main()

{

    char token[20]= {0}; //每次扫描的时候存储已经扫描的结果。

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpgpsp.html