编译原理语法分析器!!!!!c++,直接运行!直接提交! 你值得拥有!

话不多说,直接上代码。。。

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

using namespace std;

#define MAX 10000 
char ch;
char token[MAX][100];
int ptoken=0;           //指针
const char *pkeyword[]={"if","else","begin","void","int","char","main","scanf","printf","while","for",
"return","end","switch","case"};

int isletter()
{
    if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
        return 1;
    else
    {
        return 0;
    }
}

int isdigit()
{
    if(ch>='0'&&ch<='9')
     return 1;
     else
         return 0;
}

int iskeyword()
{
    for(int i=0;i<15;i++)
    {
        if(strcmp(token[ptoken],pkeyword[i])==0)
            return i;     
    }
     return 15;
}

void scanning(ifstream *file)
{
    ofstream out_file("C://Users//23649//Desktop//Code//VSCode//file//out.txt");
    out_file << setw(20) << setiosflags(ios::left) <<"单词值" <<"种别编码"<< endl;
    file->get(ch);     //ifstream &get(char &ch);功能是从流中读取一个字符,结果保存在ch
    while(1)
    {
        if(isletter())            
        {
            int i=0;
            token[ptoken][i]=ch;     //判断关键词
            file->get(ch);
            while(isletter())   
            {
                i++;
                token[ptoken][i]=ch;
                file->get(ch);
            }
            int check=iskeyword();
            if(check<15)
                out_file << setw(20) << setiosflags(ios::left) << pkeyword[check] << check + 1 << endl;
            else
                {
                    out_file << setw(20) << setiosflags(ios::left) <<token[ptoken]<<"30"<< endl;
                }
            ptoken++; 
        }
        else if(isdigit())
            {
            int i=0;
            token[ptoken][i]=ch;
            while(file->get(ch)&&isdigit())
            {
                i++;
                token[ptoken][i]=ch;
            }
            int flag=0;
            while(ch!=' '&&ch!='\n'&&ch!=';'&&ch!='='&&ch!='\'')
            {
                i++;
                token[ptoken][i]=ch;
                file->get(ch);
                flag=1;
            }
            if(flag==0)
             out_file << setw(20) << setiosflags(ios::left) << token[ptoken] << "31"<< endl;
            else
            {
                out_file<< setw(20) << setiosflags(ios::left) << token[ptoken] << "非法字符" << endl;
            }
            ptoken++;
            }
        else if(ch==' '||ch=='\n')
                file->get(ch);   
            else 
            if(ch=='#')
                break;
                else
                {
                    switch (ch)
                    {
                        case '+':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                               << "16" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case '-':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "17" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case '*':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "18" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case '=':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "19" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case '(':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "20" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case ')':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "21" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case '{':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "22" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case '}':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "23" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case ';':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "24" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case '<':
                            token[ptoken][0] = ch;
                            file->get(ch);
                            if (ch == '=')
                            {
                                token[ptoken][1] = ch;
                                out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                    << "27" << endl;
                                file->get(ch);
                            }
                            else
                                out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                    << "25" << endl;
                            ptoken++;
                            break;
                        case '>':
                            token[ptoken][0] = ch;
                            file->get(ch);
                            if (ch == '=')
                            {
                                token[ptoken][1] = ch;
                                out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                    << "28" << endl;
                                file->get(ch);
                            }
                            else
                                out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                    << "26" << endl;
                            ptoken++;
                            break;
                        case '"':
                            token[ptoken][0] = ch;
                            out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                << "29" << endl;
                            ptoken++;
                            file->get(ch);
                            break;
                        case '\\':
                            token[ptoken][0] = ch;
                            file->get(ch);
                            if (ch == 'n')
                            {
                                token[ptoken][1] = ch;
                                out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                    << "33" << endl;
                                file->get(ch);
                            }
                            else
                                out_file << setw(20) << setiosflags(ios::left) << token[ptoken]
                                    << "32" << endl;
                            ptoken++;
                            break;
                        default:
                            out_file << "无法识别:"
                                << "\t" << ch << endl;
                            file->get(ch);
                            break;
                    }
                }  
    }
}
int main()
{
    ifstream in_file("..//in.txt");  //输入文件,把你的文件地址加进去
    ofstream out_file("..//out.txt");  //输出文件,同上
    if(!in_file)
    {
        out_file<<"file does not exist!"<<endl;
        return 0;
    }
    scanning(&in_file);
    cout<<"lexical analysis done!"<<endl;
    in_file.close();
    out_file.close();
    system("pause");
    return 0;
}

in.txt文件内容
也可以根据自己喜好改一改,但结尾一定要是#。

int main()
{
    5=5;
    int a=2;
    int b=5;
    char c='2';
    if(a<=b)
      printf("a<=b\n");
    else
     printf("a>b\n");
    ()()()
}#

种类编码表
在这里插入图片描述

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值