微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何解决用C ++编写的词法分析器的问题?

如何解决如何解决用C ++编写的词法分析器的问题?

我想使用可以--p的c ++制作词法分析器

  1. 删除单行/多行注释并生成新文本
  2. 识别关键字
  3. 相同的变量
  4. 识别特殊符号 从文本文件作为程序的输入,最后将它们全部输出到单独的文本文件中作为程序的输出

我已经对该程序进行了编码。但是它无法将标识符标识为变量,也无法从给定文本中删除任何注释行。我的代码如下:

#include<bits/stdc++.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>

using namespace std;

int isKeyword(char buffer[])
{
    char keywords[32][10] =
    {
        "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"
    };
    int i,flag = 0;

    for(i = 0; i < 32; ++i)
    {
        if(strcmp(keywords[i],buffer) == 0)
        {
            flag = 1;
            break;
        }
    }

    return flag;
}

int main()
{
    char ch,buffer[15],special[]=",;\(){}[]'':";
    ifstream My_input_file("D:\\input1.txt");
    ofstream My_output_file("D:\\output1.txt");
    int mark[1000]= {0};
    int i,j=0,kc=0,ic=0,sc=0;
    vector < string > k;
    vector<char >id;
    vector<char>sp;
    string line;
    int flag=1;
    if(My_input_file.is_open())
{
    while(getline(My_input_file,line))
    {
        int lenth=line.length(),i;
        char* newline = new char[lenth+1];
        strcpy(newline,line.c_str());
        for(i=0; i<lenth; i++)
        {
            if(newline[i]=='/'&& newline[i+1]=='/')
                break;
            if (newline[i]=='/'&& newline[i+1]=='*')
                flag=2;
            if (newline[i]=='*'&& newline[i+1]=='/')
                {flag=1;
                i++;}
            else if(flag==1)
            My_output_file<<newline[i];
        }
        My_output_file<<"\n";

    while(!My_input_file.eof())
    {
        ch = My_input_file.get();
        for(i = 0; i < 12; ++i)
        {
            if(ch == special[i])
            {
                int aa=ch;
                if(mark[aa]!=1)
                {
                    sp.push_back(ch);
                    mark[aa]=1;
                    ++sc;
                }
            }
        }

        if(isalnum(ch))
        {
            buffer[j++] = ch;
        }
        else if((ch == ' ' || ch == '\n') && (j != 0))
        {
            buffer[j] = '\0';
            j = 0;

            if(isKeyword(buffer) == 1)
            {

                k.push_back(buffer);
                ++kc;
            }
            else
            {
                if(buffer[0]>=97 && buffer[0]<=122)
                {
                    if(mark[buffer[0]-'a']!=1)
                    {
                        id.push_back(buffer[0]);
                        ++ic;
                        mark[buffer[0]-'a']=1;
                    }

                }

            }

        }

    }
 }
}
    My_input_file.close();

    My_output_file<<"Keyword List: "<<endl;
    for(int f=0; f<kc; ++f)
    {
        if(f==kc-1)
        {
            My_output_file<<k[f]<<"\n";
        }
        else
        {
            My_output_file<<k[f]<<endl;
        }
    }
    My_output_file<<endl;
    My_output_file<<"Variable List: "<<endl;
    for(int f=0; f<ic; ++f)
    {
        if(f==ic-1)
        {
            My_output_file<<id[f]<<"\n";
        }
        else
        {
            My_output_file<<id[f]<<endl;
        }
    }
    My_output_file<<endl;
    My_output_file<<"Special Symbol List: "<<endl;
    for(int f=0; f<sc; ++f)
    {
        if(f==sc-1)
        {
            My_output_file<<sp[f]<<"\n";
        }
        else
        {
            My_output_file<<sp[f]<<endl;
        }

    }

    return 0;
}

我正在使用此文本文件作为输入。 Input Text File Image

将此代码运行到代码块中后,将产生以下输出Output Text File Image

但是,我想要这样的输出Output Required From the Program

我要去哪里了?请帮助我进行梳理。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。