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

📄 main.cc

📁 一个模拟flex的应用程序 主要实现词法分析 语义分析
💻 CC
字号:
#include <iostream>
#include <fstream>#include "DFA.h"using namespace std;
void RecognizeFirstSection(fstream* in,fstream* out){      char first,second;      (*in).get(first);      (*in).get(second);            if(first=='%'&&second=='{')      {         (*in).get(second);        while(second!='%')        {            (*out).put(second);            (*in).get(second);        }        (*in).get(second);      }      else       {         (*in).putback(second);          (*in).putback(first);      }}
int main(int argc,char * argv[])
{       fstream infile,code,outfile;        char temp[128];        //open input file and output file        if (argc == 1)        {                cout<<"usage: "<< argv[0] <<" <filename>\n";             exit(1);       }       else if(argc==2)      {        strcpy(temp,argv[1]) ;        infile.open(temp);        if (! infile)
	{
		cout << "Opening file:  failed!" << endl;
		exit(1);
	}		outfile.open("myLex.cc", ios::out|ios::trunc);	if (! outfile)
	{
		cout << "\nCreating file  failed!" << endl;
		exit(1);
	}	//RecognizeFirstSection      char first,second;      infile.get(first);      infile.get(second);            if(first=='%'&&second=='{')      {         infile.get(second);        while(second!='%')        {            outfile.put(second);           infile.get(second);        }       infile.get(second);      }      else       {         infile.putback(second);         infile.putback(first);      }     //RecognizeSecondSection	code.open("code");	char line[256];     outfile<<"void invoke();\n";	while(!code.eof())	{	    code.getline(line,256);	    outfile<<line<<endl;      }      char ch='%';      while(ch=='%')       {           infile.get(ch);       }         while(ch!='{')         {           infile.get(ch);         }                 infile.get(ch);//omit {            int i=0;        //find regular expression          while(ch!='}')           {              temp[i++]=ch;              infile.get(ch);           }           temp[i]='\0';           outfile<<"         DFA d;\n";           outfile<<"         char* x=new char[128];\n";           outfile<<"         strcpy(x,\""<<temp<<"\");\n";           outfile<<"         d.prepare(x,"<<"argv[1]);\n";           outfile<<"         return 0;\n}\n";           outfile<<"void invoke(){\n";         while(ch!='{')         {           infile.get(ch);          }              infile.get(ch);//omit {           while(ch!='}')           {              outfile<<ch;              infile.get(ch);           }           outfile<<"}\n";              infile.get(ch);//omit {           while(ch!='%')           {              infile.get(ch);           }          while(ch=='%')           {              infile.get(ch);           }           //RecognizeSecondSection           while(!infile.eof())             {              outfile<<ch;              infile.get(ch);             }
    } }

⌨️ 快捷键说明

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