• 大小: 391KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-06-15
  • 语言: 其他
  • 标签: 东南大学  

资源简介

东南大学编译原理课程设计,学长源码,还有实验报告

资源截图

代码片段和文件信息

#pragma warning(disable:4786)
#include
#include
#include
#include
#include
#include
#include
#include“NFA.h“
using namespace std;
#define ERROR  -1;
#define BEGIN   1;
#define END     2;
#define SEGMENT 3;
ifstream ifile;
ofstream ofile;
int lines=0;
mapreTable;
mapreaction;
vector actionTable;
struct RE{
string one;
string two;
struct *next;
}RE;

int check(char ch)//判断%下一个字符串是什么
{
if(ch==‘%‘)
{
char nextch=ifile.get();
switch(nextch)
{
case ‘{‘:return BEGIN;
case ‘}‘:return END;
case ‘%‘:return SEGMENT;
default:ifile.seekg(-1ios::cur);
break;
}
}
return ERROR;
}
bool IsLetterNum(char ch)//判断规则部分字符串是否符合规则
{
if ((ch<=‘9‘&&ch>=‘0‘)||(ch<=‘z‘&&ch>=‘a‘)||(ch<=‘Z‘&&ch>=‘A‘))
{
return true;
}
return false;
}

bool compleRe(string &re)//把规则部分转化成正则表达式
{
int intcount;
string str=““;
string temp;
map::iterator iter;
int i=0;
int j;
int offset;
char ch=re[i];
while(ch!=‘\0‘)
{
switch(ch)
{
case‘[‘:
str.append(1‘(‘);
ch=re[++i];
continue;
break;
case‘]‘:
str.append(1‘)‘);
ch=re[++i];
break;
case‘-‘:
{
char before=re[i-1];
char after=re[i+1];
str.erase(str.length()-11);
if(IsLetterNum(re[i-2]))
{
str.append(1‘|‘);
}
while(before {
str.append(1before);
str.append(1‘|‘);
before++;
}
str.append(1after);
ch=re[i+2];
i=i+2;
break;
}
case‘{‘:
offset=re.find_first_of(‘}‘i);
for(j=i+1;j {
temp.append(1re[j]);
}
iter=reTable.find(temp);
intcount=reTable.count(temp);
if(intcount<=0)
{
return false;
}
if(iter!=reTable.end())
{
str.append(1‘(‘);
str.append(iter->second);
str.append(1‘)‘);
}
temp=““;
i=offset;
ch=re[++i];
break;
case‘+‘:
str.append(1re[i-1]);
str.append(1‘*‘);
ch=re[++i];
case‘|‘:
str.append(1‘|‘);
ch=re[++i];
break;
case‘“‘:
offset=re.find_first_of(‘“‘i+1);
temp=re.substr(i+1offset-i-1);
str.append(1‘(‘);
str.append(temp);
str.append(1‘)‘);
i=offset;
ch=re[++i];
break;
default:
str.append(1ch);
ch=re[++i];
break;
}
}
re=str;
return true;
}

void GenerateCode(DFA & dfa);
int main()
{
NFA nfa1;
ofile.open(“yylex.cpp“ios::out);
    ifile.open(“test.txt“ios::out);
if(!ifile)
{
cerr<<“File could not be opened“< exit(1);
}
char ch;
ch=ifile.get();
int state;
state=check(ch);
if(state!=1)
{
cerr<<“The formation of the inputfile is wrong“< exit(1);
}
while(!ifile.eof()&&state!=2)
{
char nextch=ifile.get();
//if(nextch==‘\t‘||nextch==‘ ‘)
//continue;
if(nextch==‘%‘)
{
state=check(nextch);
continue;
}
if(next

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2015-08-31 08:45  090091黄海汇\
     文件      264900  2012-06-05 11:04  090091黄海汇\lex yacc.pptx
     文件      109258  2012-06-02 01:14  090091黄海汇\lex实验报告.docx
     目录           0  2015-08-31 08:45  090091黄海汇\seulex\
     文件       18006  2015-06-09 14:18  090091黄海汇\seulex\DFA.CPP
     文件        2387  2012-06-02 10:42  090091黄海汇\seulex\DFA.H
     文件        1880  2012-05-17 11:50  090091黄海汇\seulex\graph.h
     文件        8521  2015-06-05 16:34  090091黄海汇\seulex\lex.cpp
     文件       13118  2012-05-30 00:37  090091黄海汇\seulex\NFA.CPP
     文件        2536  2012-06-02 02:21  090091黄海汇\seulex\NFA.H
     文件       12934  2012-05-17 14:07  090091黄海汇\seulex\ourlex.cpp
     文件         534  2012-06-02 15:00  090091黄海汇\seulex\test.txt
     文件           0  2012-06-02 15:00  090091黄海汇\seulex\yylex.cpp
     目录           0  2015-08-31 08:45  090091黄海汇\yacc\
     文件         110  2012-06-02 02:11  090091黄海汇\yacc\mini.txt
     文件        3104  2012-06-02 01:22  090091黄海汇\yacc\minic.txt
     文件       24160  2012-06-02 14:45  090091黄海汇\yacc\table.txt
     文件       16488  2012-06-02 14:45  090091黄海汇\yacc\yacc.cpp
     文件       34412  2012-06-04 19:11  090091黄海汇\yacc实验报告.docx

评论

共有 条评论