• 大小: 2.97MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-11-16
  • 语言: 其他
  • 标签: lexical  syntax  

资源简介

大学编译原理实验,语法和词法分析器,包括部分错误处理。 program -> compoundstmt compoundstmt -> { stmts } { stmts -> stmt stmts stmt -> assgstmt assgstmt -> ID = arithexpr ; ID arithexpr -> multexpr arithexprprime multexpr -> simpleexpr multexprprime simpleexpr -> NUM NUM

资源截图

代码片段和文件信息

/**************************************************************************
*作    者:王林
*创建时间:2007-12-16
*描    述:完成
*注意事项:无
*遗 留BUG:无
*修改时间:2007-12-16到2007-12-30
**************************************************************************/

#include “LexicalAnalyzer.h“;

///
///LexicalAnalyzer的构造函数
///

LexicalAnalyzer::LexicalAnalyzer(){
row=0;
lex_beginning=0;
forward=0;
state=0;
start=0;
linenumber=0;
lineposition=0;
}
///
///初始化变量
///

///
void LexicalAnalyzer::InitPara(){
lex_beginning=0;
forward=0;
state=0;
start=0;
buffer.clear();
}
///
///Check Delimiters
///

///如果是Delimiter,返回true,否则返回false
bool LexicalAnalyzer::checkDelimiters(){
if(buffer[forward]==‘{‘||buffer[forward]==‘}‘||buffer[forward]==‘(‘||buffer[forward]==‘)‘||buffer[forward]==‘;‘) return true;
else return false;
}
///
///Check Compare Operator
///

///如果是Compare Operator,返回true,否则返回false
bool LexicalAnalyzer::check(){
if( buffer[forward]==‘=‘||buffer[forward]==‘>‘||buffer[forward]==‘<‘||buffer[forward]==‘!‘) return true;
else return false;
}
///
///Check Operator
///

///如果是Operator,返回true,否则返回false
bool LexicalAnalyzer::checkOperators(){
if(buffer[forward]==‘+‘||buffer[forward]==‘-‘||buffer[forward]==‘*‘||buffer[forward]==‘/‘) return true;
else return false;
}
///
///打开输入输出文件
///

///输入文件的路径和文件名
///输出文件的路径和文件名
///如果是C打开成功,返回true,否则返回false
bool LexicalAnalyzer::OpenFile(string infilePathstring outfilePath){
input.open(infilePath.c_str());
output.open(outfilePath.c_str());
if (input&&output) return true;
else return false;
}
///
///关闭输入输出文件
///

///
void LexicalAnalyzer::CloseFile(){
input.close();
output.close();
}
///
///类型匹配,获得下一个token
///

///返回匹配成功的token,或是错误标识
string LexicalAnalyzer::nexttoken(){
while(true){
switch(state){
case 0:                                          //匹配 “//“
c=buffer[forward];
forward++;
if(isspace(c)){
    state=0;
    lex_beginning++;
}
else if(c==0){forward--; return “\n“;}
else if(c==‘/‘) state=1;
else state=fail();
    break;
case 1:
c=buffer[forward];
forward++;
if(c==‘/‘)return “//“;
else state=fail();
break;         
case 2:                                       //匹配if,int
c=buffer[forward];
if(c==‘i‘)state=3;
else state=fail();
break;
case 3:
forward++;
c=buffer[forward];
if(c==‘f‘){
forward++;
if(isspace(buffer[forward])||buffer[forward]==0||checkDelimiters()) return “if“;
if(check()||checkOperators()) state=37;                        //error:+-*/<>=! f

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

     文件      98816  2007-12-26 16:00  上机实践报告_词法分析器.doc

     文件     152576  2007-12-26 21:15  上机实践报告_语法分析器.doc

     文件         23  2008-01-03 20:54  email.txt

     文件      77824  2007-12-26 22:07  词法分析器\LexicalAnalyzer\debug\LexicalAnalyzer.exe

     文件     474088  2007-12-26 22:07  词法分析器\LexicalAnalyzer\debug\LexicalAnalyzer.ilk

     文件     560128  2007-12-26 22:07  词法分析器\LexicalAnalyzer\debug\LexicalAnalyzer.pdb

     文件      10294  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\BuildLog.htm

     文件        405  2007-12-26 20:03  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\LexicalAnalyzer.exe.embed.manifest

     文件        472  2007-12-26 20:03  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\LexicalAnalyzer.exe.embed.manifest.res

     文件        387  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\LexicalAnalyzer.exe.intermediate.manifest

     文件     100995  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\LexicalAnalyzer.obj

     文件      46419  2007-12-26 20:03  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\main.obj

     文件         69  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\mt.dep

     文件     248832  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\vc80.idb

     文件     208896  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer\Debug\vc80.pdb

     文件         82  2007-12-20 10:31  词法分析器\LexicalAnalyzer\LexicalAnalyzer\ffbbx

     文件        397  2007-12-20 00:01  词法分析器\LexicalAnalyzer\LexicalAnalyzer\include.h

     文件      13737  2007-12-26 22:06  词法分析器\LexicalAnalyzer\LexicalAnalyzer\LexicalAnalyzer.cpp

     文件        817  2007-12-20 00:00  词法分析器\LexicalAnalyzer\LexicalAnalyzer\LexicalAnalyzer.h

     文件       4419  2007-12-26 20:08  词法分析器\LexicalAnalyzer\LexicalAnalyzer\LexicalAnalyzer.vcproj

     文件       1411  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer\LexicalAnalyzer.vcproj.ECNU-WL.dream.user

     文件        600  2007-12-20 00:01  词法分析器\LexicalAnalyzer\LexicalAnalyzer\main.cpp

     文件      32768  2007-11-27 15:49  词法分析器\LexicalAnalyzer\LexicalAnalyzer\programming_assignment01-lexical analyzer.doc

     文件        158  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer\tok.txt

     文件          6  2007-12-26 21:59  词法分析器\LexicalAnalyzer\LexicalAnalyzer\toy.txt

     文件    1444864  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer.ncb

     文件        910  2007-12-11 09:44  词法分析器\LexicalAnalyzer\LexicalAnalyzer.sln

    ..A..H.     36864  2007-12-26 22:07  词法分析器\LexicalAnalyzer\LexicalAnalyzer.suo

     文件     286750  2007-12-26 20:02  词法分析器\LexicalAnalyzer.rar

     文件     135168  2007-12-26 21:20  语法分析器\SyntaxAnalyzer\debug\SyntaxAnalyzer.exe

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

评论

共有 条评论