• 大小: 2.26MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-10-28
  • 语言: 其他
  • 标签:

资源简介

东南大学编译原理实验 lex&&yacc

资源截图

代码片段和文件信息

#include “lex.h“
#include 
#include 
#include 
using namespace std;
#include



Lex::Lex()
{
inFile.open(“LexInput.txt“ios::in);
if(!inFile)
{
cerr<<“LexInput.txt doesn‘t exist“;
exit(1);
}

}

Lex::~Lex()
{

}

string Lex::nextToken()
{
int state = 0;
char ch;
string str = ““;
    while(1)
{
switch(state)
{
case 0:
ch = inFile.get();
if(isBlank(ch))
state = 0;
else if(ch==EOF)
{
state = 1;
}
else if(isLetter(ch))
{
state = 2;
str += ch;
}
else if(isDigit(ch))
{
state = 4;
str += ch;
}
else if(isDoubleOperator(ch))

{
if (ch==‘!‘)
{
str +=ch;
ch=inFile.get();
if (ch!=‘=‘)
{
cerr << “error input:   “ ;
state=8;
}
else
{
str +=ch;
state=6;
}
}
else
{
str +=ch;
ch=inFile.get();
if (ch!=‘=‘)
{
                             spBack();
     state=6;

}
 else
{
str +=ch;
state=6;
}
}

}
else if(isOperator(ch))
{
state = 6;
str += ch;
}
else if(isSeparator(ch))
{
state = 7;
str += ch;
}
else
{
state=8;
str+=ch;
cerr << “error input:   “ ;

}
break;

case 1:
return “#“;
break;
case 2:
ch = inFile.get();
if( isLetter(ch) || isDigit(ch) )
{
state = 2;
str += ch;
}
else
{
spBack();
state = 3;
}
break;
case 3:
if(!isKeyword(str))
{
str = “Identifier:        “+str;
state = 0;
return str;
}
else
{
str = “Keyword:           “+str;
state = 0;
return str;
}
break;
case 4:
ch = inFile.get();
if(isDigit(ch))
{
str += ch;
state = 4;
}
else
{
spBack();
state = 5;
}
break;
case 5:
str = “Number:            “+str;
state = 0;
return str;
break;
case 6:
str = “Operator:          “+str;
state = 0;
return str; 
break;
case 7:
str = “Separator:         “+str;

state = 0;
return str;
break;
case 8:
ch = inFile.get();
if( (!isLetter(ch) )&&(!isDigit(ch))&&(!isBlank(ch))&&(!isOperator(ch))&&(!isDoubleOperator(ch))&&(!isSeparator(ch)) )
{
state = 8;
str += ch;
}
else
{
spBack();
state = 0;
}

                    cerr< str =“Error:             “+str;
return str;
break;
default:
cerr << “ERROR: No such states! “ << endl;
exit(0);
break;
}
}
}

void Lex::spBack()
{
inFile.seekg(-1ios::cur);   //让文件指针从当前位置向文件开始方向移动1个字节
}

bool Lex::isBlank(char ch)
{
ch

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       4601  2010-12-14 19:38  09008304_yacc\09008304_yacc.dsp

     文件        549  2010-12-14 19:38  09008304_yacc\09008304_yacc.dsw

     文件      58368  2010-12-29 00:31  09008304_yacc\09008304_yacc.ncb

     文件      54784  2010-12-29 00:31  09008304_yacc\09008304_yacc.opt

     文件        904  2010-12-29 00:22  09008304_yacc\09008304_yacc.plg

     文件     630866  2010-12-29 00:22  09008304_yacc\Debug\09008304_yacc.exe

     文件     915232  2010-12-29 00:22  09008304_yacc\Debug\09008304_yacc.ilk

     文件    2195400  2010-12-29 00:16  09008304_yacc\Debug\09008304_yacc.pch

     文件    1188864  2010-12-29 00:16  09008304_yacc\Debug\09008304_yacc.pdb

     文件     258156  2010-12-29 00:22  09008304_yacc\Debug\lex.obj

     文件     204491  2010-12-14 19:49  09008304_yacc\Debug\main.obj

     文件     292442  2010-12-14 19:49  09008304_yacc\Debug\syntax.obj

     文件      99328  2010-12-29 00:22  09008304_yacc\Debug\vc60.idb

     文件     126976  2010-12-29 00:16  09008304_yacc\Debug\vc60.pdb

     文件       4907  2010-12-14 19:50  09008304_yacc\lex.cpp

     文件        400  2010-12-14 19:49  09008304_yacc\lex.h

     文件         25  2010-12-29 00:22  09008304_yacc\LexInput.txt

     文件        550  2010-12-14 19:31  09008304_yacc\main.cpp

     文件         24  2010-12-29 00:22  09008304_yacc\outputLex.txt

     文件        612  2010-12-29 00:22  09008304_yacc\outputSyntax.txt

     文件       9424  2010-12-14 19:30  09008304_yacc\syntax.cpp

     文件        407  2010-12-14 19:33  09008304_yacc\syntax.h

     文件     374784  2010-12-29 00:31  编译实验报告 09008304.doc

     文件       4466  2010-12-02 22:10  09008304_lex\09008304_lex.dsp

     文件        547  2010-12-02 20:12  09008304_lex\09008304_lex.dsw

     文件      50176  2010-12-29 00:16  09008304_lex\09008304_lex.ncb

     文件      53760  2010-12-29 00:16  09008304_lex\09008304_lex.opt

     文件        878  2010-12-29 00:10  09008304_lex\09008304_lex.plg

     文件     585808  2010-12-29 00:10  09008304_lex\Debug\09008304_lex.exe

     文件     847900  2010-12-29 00:10  09008304_lex\Debug\09008304_lex.ilk

............此处省略18个文件信息

评论

共有 条评论

相关资源