⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cifa.cpp

📁 编译原理实验词法分析源程序
💻 CPP
字号:
#include<iostream>
#include<string>
#include<stdio.h>
using std::cout;
using std::cin;
using std::endl;
using std::string;
    char prog[80],token[8];//prog存放原程序字符串,token存放单词自身字符串
    char ch;
    int syn,p,m,n,sum;//syn单词种别码,p是缓冲区prog的指针,m是token的指针
    char *rwtab[6]={"begin","if","then","while","do","end"};
    void scaner();//扫描子程序

int main()
{
    p=0;
    cout<<"please input string:"<<endl;
    do{
        cin>>ch;
        prog[p++]=ch;
    }while(ch!='#');//输入串存入prog数组空间
    p=0;//指针置零

    do{
        scaner();//调用扫描子程序
        switch(syn)
        {

        case 11:cout<<"("<<syn<<","<<sum<<")"<<"  ";
            break;
        case -1:cout<<"error!"<<endl;
            break;
        default:cout<<"("<<syn<<","<<token<<")"<<"  ";
           break;
        }
    }while(syn!=0);

    return 0;
}

void scaner()
{
    for(n=0;n<8;n++)
        token[n]=0;//每次调用都把 token清零
    m=0;
	sum=0;
    ch=prog[p++];   //读入一个字符
    while(ch=='_')
        ch=prog[p++]; //若ch是空格则向后移动

    if(isalpha(ch))//ch为字母字符
    {
        while(isalnum(ch))//ch为字母或数字字符;
        {
            token[m++]=ch;
            ch=prog[p++];//读下一个字符;
        }
        token[m++]='\0';
        p=p-1;//回退一个字符;
         
        for(n=0;n<6;n++)
        {
            if(strcmp(token,rwtab[n])==0)//判断token是否为关键字
            {
                syn=n+1;//设置关键字的syn分别为:1 2 3 4 5 6
                break;
            }
            else
                syn=10;//设置标识符syn
        }
    }

    else if(isdigit(ch))//ch是数字字符
    {
                while(isdigit(ch))//ch为数字字符
                {
                    sum=sum*10+ch-'0';
                    ch=prog[p++];//读下一个字符
                }
                p=p-1;//回退一个字符;
                syn=11;
    }
    else if(ch=='<')
	{
		        token[m++]=ch;
                ch=prog[p++];
                if(ch=='>')
				{
                    syn=21;
                    token[m++]=ch;
				}
                else if(ch=='=')             
				{
                    syn=22;
                    token[m++]=ch;
				}
                else
				{
                    syn=20;
                    p=p-1;//回退一个字符;
				} 
	}
    
	else if(ch=='>')
	{             
		        token[m++]=ch;
                ch=prog[p++];
                if(ch=='=')
                {
                    syn=24;
                    token[m++]=ch;
                }
                else
                {
                     syn=23;
                     p=p-1;//回退一个字符
				}
	}

    else if(ch==':')
	{
		            token[m++]=ch;
                    ch=prog[p++];
                    if(ch=='=')
                    {
                        syn=18;
                        token[m++]=ch;
                    }
                    else
                    {
                        syn=17;
                        p=p-1;
                    }
	 }

     else if(ch=='=') { syn=25;token[0]=ch; }

     else if(ch=='+') { syn=13;token[0]=ch; }
                   
     else if(ch=='-') { syn=14;token[0]=ch; }
       
     else if(ch=='*') { syn=15;token[0]=ch; }
       
     else if(ch=='/') { syn=16;token[0]=ch; }
             
     else if(ch==';') { syn=26;token[0]=ch; }
               
     else if(ch=='(') { syn=27;token[0]=ch; }
               
     else if(ch==')') { syn=28;token[0]=ch; }
	 
     else if(ch=='#') { syn=0;token[0]=ch; }
             
     else syn=-1;
}
    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -