-
编译原理词法分析器
2019-11-02 16:25:08编译原理词法分析器代码 编译原理词法分析器代码 记得输入好路径例如D:\test.txt;讲指定文件导入测试。 编译原理 词法分析器 // // //(1)按规则拼写单词,并转换成二元式形式。 //(2)删除注释行。 //(3)删除空白符。...编译原理词法分析器代码
编译原理词法分析器代码
记得输入好路径例如D:\test.txt;讲指定文件导入测试。
编译原理 词法分析器
//
//
//(1)按规则拼写单词,并转换成二元式形式。
//(2)删除注释行。
//(3)删除空白符。
//(4)列表打印源程序,按照源程序的行打印,在每行的前面加上行号,并且打印出每行包含的二元形式。
//(5)发现并定位错误。
//#include
#include<string.h>
using namespace std;
char prog[1000],ch,token[8];
int p=0,sym=0,n;
char filename[30];
FILE *fpin;
char *keyword[8]={“if”,“then”,“else”,“end”,“repeat”,“until”,“read”,“write”};
void GetToken();
int main()
{
p=0;
cout<<“请输入源文件名:”;
for(;😉
{
cin>>filename;
if((fpin=fopen(filename,“r”))!=NULL)
break;
else cout<<“文件路径错误!请输入源文件名:”;
}
do
{
ch=fgetc(fpin);
prog[p++]=ch;
}while(ch!=EOF);
p=0;
do
{
GetToken();
switch(sym)
{
case -1:
case -2:break;
default:cout<<"("<<sym<<","<<token<<")"<<endl;break;
}
}while(ch!=EOF);
return 0;
}void GetToken()
{
for(n=0;n<8;n++)
{
token[n]=’\0’;
}
n=0;
ch=prog[p++];
while(ch==’ ‘||ch==’\n’||ch==’\t’){ch=prog[p++];}
if((ch>=‘a’&&ch<=‘z’)||(ch>=‘A’&&ch<=‘Z’))
{
sym=1;
do{
token[n++]=ch;
ch=prog[p++];
}while((ch>=‘a’&&ch<=‘z’)||(ch>=‘A’&&ch<=‘Z’));
sym=2;
for(n=0;n<8;n++)
{
if(strcmp(token,keyword[n])0)
{
sym=n+3;
}
}
p–;
}
else if(ch’{’)
{
do{
ch=prog[p++];
}while(ch!=’}’);
sym=-1;
return;
}
else if(ch>=‘0’&&ch<=‘9’)
{
sym=11;
do
{
token[n++]=ch;
ch=prog[p++];
}while(ch>=‘0’&&ch<=‘9’);
sym=12;
p–;
return;
}
else
{
switch(ch)
{
case ‘+’:sym=13;token[0]=ch;break;
case ‘-’:sym=14;token[0]=ch;break;
case ‘*’:sym=15;token[0]=ch;break;
case ‘/’:sym=16;token[0]=ch;break;
case ‘=’:sym=17;token[0]=ch;break;
case ‘<’:sym=18;token[0]=ch;break;
case ‘;’:sym=19;token[0]=ch;break;
default:sym=-2;cout<<“词法分析出错,请检查是否输入非法字符!\n”;break;
}
}
} -
编译原理 词法分析器
2010-05-05 22:51:50词法分析器的单词符号常常表示成以下的二元式(单词种别码,单词符号的属性值)。本实验中,采用的是一类符号一种别码的方式。 2、 单词的BNF表示 <标识符>-> <字母><字母数字串> <字母数字串>-><字母><字母数字串>|... -
lex_实验-编译原理词法分析器实现
2018-05-02 23:51:16这是一个编译原理lex工具,具备词法分析器的功能,方便了解编译原理词法分析器的功能 -
编译原理词法分析器实验报告
2013-06-09 16:49:02词法分析器,使用c++编写,编译原理词法分析器实验报告完整版 -
编译原理词法分析器实验报告含源代码状态转换图
2019-01-07 10:51:50编译原理词法分析器实验报告含源代码,还有状态转换图。C语言实现 -
编译原理 词法分析器 C++
2020-10-19 18:13:47编译原理 词法分析器 C++ #include<iostream> #include<fstream> #include<vector> #include<string> using namespace std; /** C语言语法特性常量 */ const char Monocular_operator[13] ...#include<iostream> #include<fstream> #include<vector> #include<string> using namespace std; const char Monocular_operator[15] = { '+','-','*','/','!','%','~','&','|','^','=','<','>',':','?' }; //单目运算符 const string Binocular_operator[] = { "++","--","&&","||","<=","!=","==",">=","+=","-=","*=","/=" }; //双目运算符 const char Delimiter[8] = { ',','(',')','{','}',';','[',']' }; //界符 const string Keyword[19] = { "break","case","continue","do","default","else", "for","if","include","main","return","switch","typedef","void","while","#","unsigned","true","false" };//控制关键字 const string Variable_type[7] = { "int","byte","long","short","float","double","char" }; //基本类型 const string typeName[] = { "单目运算符","双目运算符","界符","控制关键字","基本类型","变量名","字符","数值","注释" }; const string errTypeName[] = { "标识符表示错误","缺少右引号","数值表示错误","无法识别的标记" }; typedef struct LexItem { int rowNum; int typeId; //1:单目运算符 2:双目运算符 3:界符 4:控制关键字 5:基本类型 6 :变量名 7 字符 8 数值 9注释 string value; LexItem(int x, int y, string z) :rowNum(x), typeId(y), value(z) {} }; /*异常错误信息项*/ typedef struct errItem { int rowNum; int typeId; //1. 标识符表示错误 2.字符或字符串出错 3. 无法识别标记 string value; errItem(int x, int y, string z) :rowNum(x), typeId(y), value(z) {} }; /** 最终分析结果 */ vector<LexItem> analyResult; /** 分析数据缓存 */ vector<string> fileMessage; //文件信息 /*文件名*/ string fileName; /*错误信息汇总*/ vector<errItem>errMessage; /*读取txt分析文件 将其存入 @fileMeessage*/ void readSourceFile(); /*词法分析入口*/ void LexAnalysis(); /*寻找界符下表*/ int findDelimiter(char c); //寻找界符下表 /*寻找单目运算符下表*/ int findMonOperator(char c); /*寻找string 的下表*/ int findStrIndex(string str, const string strarray[], int length); /*结果展示*/ void showResult(); /*写入文件*/ void writeLexFile(); /*@strarray里是否包含@str*/ int findHasStrIndex(string str, const string strarray[], int lenth); /*是否到达结束点*/ bool isComplete(char c); /*找到多行注释结尾*/ bool findMulNoteLocation(int line, int i, int res[]); /*找到多行注释的值*/ string findmulNoteVal(int startline, int starpoint, int loc[]); /*通过类型获取不同类型的符号表*/ vector<LexItem> getLexitemByType(int typeId); /*找到结束点*/ int findCompleteIndex(string str); /*判断变量名是否合法*/ bool judgeIdisLegal(string str); /*获取字符 ’ “的结尾*/ int findstrCompleteIndex(string str); /*获取数字的结尾*/ bool judgeNumlegal(string str); /*主分析函数*/ void LexAnalysis(); int main() { readSourceFile(); LexAnalysis(); showResult(); writeLexFile(); } void LexAnalysis() { int offset = 0; for (int index = 0; index < fileMessage.size(); index++) { string line = fileMessage[index]; if (offset >= line.length()) { offset = 0; continue; } for (int i = 0 + offset; i < line.length(); i++) { if (offset > 0) offset = 0; //注释单独判断 if (line[i] == '/') { if (i < line.size() - 1) { if (line[i + 1] == '/') { //单行注释 string val = ""; if (i != line.size() - 2) { val = line.substr(i + 2); } //非单行末尾 LexItem SNote(index, 9, val); analyResult.push_back(SNote); break; } else if (line[i + 1] == '*') { //多行注释 int loc[] = { 0,0 }; if (findMulNoteLocation(index, i + 2, loc) == false) { //多行注释出错 程序直接终止 showResult(); cout << "第 " << index << " 行 ,注释出错,未找到注解结尾,程序终止!" << endl; exit(-1); } string mval = findmulNoteVal(index, i + 2, loc); LexItem MNote(index, 9, mval); analyResult.push_back(MNote); index = loc[0] - 1; offset = loc[1] + 3; if (offset == 3) { offset = 2; } break; } } } if (line[i] == ' ' || line[i] == '\t') { continue; } //过滤空格 和制表符 else if (line[0] == '#') { break; } //过滤头文件 else if (findDelimiter(line[i]) != -1) { //界符 string temp = " "; temp[0] = line[i]; LexItem delimiter(index, 3, temp); analyResult.push_back(delimiter); } else if (findMonOperator(line[i]) != -1) { //运算符 if (i != line.length() - 1) { //是否为为本行最后一个字符 string str = line.substr(i, 2); if (findStrIndex(str, Binocular_operator, 12) != -1) { //双目运算符 LexItem Bopt(index, 2, Binocular_operator[findStrIndex(str, Binocular_operator, 12)]); analyResult.push_back(Bopt); i++; } else { //单目运算符 string temp = " "; temp[0] = line[i]; LexItem opt(index, 1, temp); analyResult.push_back(opt); int s = 10; } } else { //单目运算符 string temp = " "; temp[0] = line[i]; LexItem opt1(index, 1, temp); analyResult.push_back(opt1); } } else if (((char)line[i] >= 'a' && (char)line[i] <= 'z') || ((char)line[i] >= 'A' && (char)line[i] <= 'Z') || (char)line[i] == '_') { //控制关键字 基本类型 变量名 int K = findHasStrIndex(line.substr(i), Keyword, 19); int V = findHasStrIndex(line.substr(i), Variable_type, 7); if (K != -1) { //可能为控制关键字 if (i + K - 1 == line.size() || isComplete(line[i + K])) { LexItem kw(index, 4, line.substr(i, K)); analyResult.push_back(kw); i += K - 1; } } else if (V != -1) { //可能为基本类型 if (i + V - 1 == line.size() || isComplete(line[i + V])) { LexItem variable(index, 5, line.substr(i, V)); analyResult.push_back(variable); i += V - 1; } } else {//变量名 ID int IDI = findCompleteIndex(line.substr(i)); string IDval = line.substr(i, IDI); if (judgeIdisLegal(IDval)) { //合法标识符 LexItem id(index, 6, IDval); analyResult.push_back(id); } else { errItem iderr(index, 1, IDval); //非法标识符 errMessage.push_back(iderr); } i += IDI - 1; } } else if (line[i] >= 48 && line[i] <= 57) { //数值 int NID = findCompleteIndex(line.substr(i)); string numVal = line.substr(i, NID); if (judgeNumlegal(numVal)) { LexItem num(index, 8, numVal); analyResult.push_back(num); } else { errItem numerr(index, 3, numVal); errMessage.push_back(numerr); } i += NID - 1; } else if (line[i] == '\'' || line[i] == '"') { //字符 C语言字符串只能放在一行 不能拆开 即 “ 或 ‘ 不能出现在不同的行数 int backindex = findstrCompleteIndex(line.substr(i)); if (backindex == -1) { //字符或字符串出错 缺少右引号 errItem Serr(index, 2, line.substr(i)); errMessage.push_back(Serr); break; } else { LexItem str(index, 7, line.substr(i, backindex)); analyResult.push_back(str); i += backindex - 1; } } else { //无法识别标记 int c = findCompleteIndex(line.substr(i)); errItem value(index, 4, line.substr(i,c)); errMessage.push_back(value); i += c - 1; } } } } void readSourceFile() { std::cout << "请输入要分析的源代码文件 : "; cin >> fileName; ifstream file(fileName); if (!file.is_open()) { cout << "文件打开错误!请检查文件路径:" << fileName << endl; exit(1); } char buffer[255]; while (!file.eof()) { file.getline(buffer, 255); fileMessage.push_back(buffer); } file.close(); } void writeLexFile() { string outfile = fileName.substr(0, fileName.size() - 4) + "Out.txt"; ofstream file(outfile); for (int i = 0; i < analyResult.size(); i++) { file << "< " << analyResult[i].rowNum << " , " << analyResult[i].value << " , " << typeName[analyResult[i].typeId - 1] << " >" << endl; } file.close(); } int findDelimiter(char c) { for (int i = 0; i < strlen(Delimiter); i++) { if (Delimiter[i] == c)return i; } return -1; } int findMonOperator(char c) { for (int i = 0; i < strlen(Monocular_operator); i++) { if (Monocular_operator[i] == c)return i; } return -1; } int findStrIndex(string str, const string strarray[], int length) { for (int i = 0; i < length; i++) { if (strarray[i] == str)return i; } return -1; } /*@strarray里是否包含@str*/ int findHasStrIndex(string str, const string strarray[], int lenth) { int maxlength = str.size(); for (int i = 0; i < lenth; i++) { if (strarray[i].size() <= maxlength) { if (strarray[i] == str.substr(0, strarray[i].size()))return strarray[i].size(); } } return -1; } /*是否到达结束点*/ bool isComplete(char c) { if (c == ' ')return true; for (int i = 0; i < strlen(Delimiter); i++) { if (Delimiter[i] == c)return true; } for (int i = 0; i < strlen(Monocular_operator); i++) { if (Monocular_operator[i] == c)return true; } return false; } /*找到结束点*/ int findCompleteIndex(string str) { for (int i = 0; i < str.length(); i++) { if (isComplete(str[i]))return i; } return str.length(); } int findstrCompleteIndex(string str) { for (int i = 1; i < str.length(); i++) { if (str[i] == '\'' || str[i] == '"')return i+1; } return -1; } /*找到多行注释结尾*/ bool findMulNoteLocation(int line, int i, int res[]) { for (int first = i; first < fileMessage[line].size(); first++) {//在同一行 if (first < fileMessage[line].size() - 1) { if (fileMessage[line][first] == '*' && fileMessage[line][first + 1] == '/') { res[0] = line; res[1] = first - 1; return true; } } } for (int index = line + 1; index < fileMessage.size(); index++) { for (int j = 0; j < fileMessage[index].size(); j++) { if (index < fileMessage.size() - 1) { if (fileMessage[index][j] == '*' && fileMessage[index][j + 1] == '/') { res[0] = index; res[1] = j - 1; if (res[1] < 0)res[1] = 0; return true; } } } } return false; } /*找到多行注释的值*/ string findmulNoteVal(int startline, int starpoint, int loc[]) { string mulvalue = ""; if (starpoint <= loc[1]) mulvalue += fileMessage[startline].substr(starpoint, loc[1] - starpoint + 1); for (int index = startline + 1; index <= loc[0]; index++) { if (index == loc[0]) { mulvalue += fileMessage[index].substr(0, loc[1]); } else { mulvalue += fileMessage[index]; } } return mulvalue; } void showResult() { for (int i = 0; i < analyResult.size(); i++) { cout << "< " << analyResult[i].rowNum << " , " << analyResult[i].value << " , " << typeName[analyResult[i].typeId - 1] << " >" << endl; } if (errMessage.size() != 0) { //错误信息 cout << "编译过程出现错误:" << endl; for (int i = 0; i < errMessage.size(); i++) { cout << "< " << errMessage[i].rowNum << " , " << errMessage[i].value << " , " << errTypeName[errMessage[i].typeId - 1] << " >" << endl; } } } /*通过类型获取不同类型的符号表*/ vector<LexItem> getLexitemByType(int typeId) { vector<LexItem> ans; for (auto item : analyResult) { if (item.typeId == typeId)ans.push_back(item); } return ans; } bool judgeIdisLegal(string str) { for (auto c : str) { if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= 48 && c <= 57))continue; return false; } return true; } bool judgeNumlegal(string str) { for (int i = 0; i < str.size(); i++) { if (str[i] >= 48 && str[i] <= 57) { continue; } else if (str[i] == '.' && i != str.size()) { continue; } else { return false; } } return true; }
-
编译原理词法分析器 java版
2012-11-11 11:27:54编译原理词法分析器 java版 包括内容: 1.所识别语言的词法的EBNF描述。 2.所采用的二元式格式说明(如一个单词(不包括用户定义的标识符)一个种别,还是一类一个种别,对用户定义的标识符归为一个种别)。 3... -
编译原理词法分析器实验(从文件读入)
2014-10-05 09:35:01编译原理 词法分析器实验代码 从文件读入 cbb.cpp文件 -
lexical_syntax_analysis:编译原理词法分析器和语法分析器LR(1)实现C ++-源码
2021-01-29 21:47:52lexical_syntax_analysis:编译原理词法分析器和语法分析器LR(1)实现C ++ -
编译原理词法分析器、语法分析器python实现
2018-05-28 11:16:09python实现的词法分析器和语法分析器,哈工大威海编译原理实现,词法分析器能够识别字符串,能够判断所输入的字符串是否符合文法,语法分析器采用自底向上的LR0实现。 -
《编译原理词法分析器java》.doc
2020-01-19 10:37:00编译原理实验报告 计算机与信息学院 第 第 PAGE 14 页 共 NUMPAGES 14 页 实验一 词法分析器的设计 一实验目的 理解词法分析器的任务和输出形式 理解扫描器的工作原理 掌握状态转换图的绘制以及单词的识别技术 掌握... -
编译原理 词法分析器 c++实现
2020-12-05 21:30:02参考附录C.1设计一个简单语言的词法分析程序,要求能够处理换行回车、注释(自定义注释格式)、部分符合运算符(如>= 、 等)。 注意: 附录C.1采用的是控制台输入输出的方式,测试数据要用文本文件保存好。 -
c语言实现编译原理词法分析器
2017-04-18 20:00:59词法分析器 :#include<stdio.h> #include<conio.h> #include<math.h> #include<string.h> #include<stdlib.h>int i, row = 0, line = ...词法分析器
text.txt文件内容:
char word[10]; char pro[100][100] = { "PROGRAM", "BEGIN", "END", "VAR", "INTEGER", "WHILE", "IF", "THEN", "ELSE", "DO", "PROCEDURE" , "char","int","if","else","var" ,"return","break","do","while","for","double","float","sh ort"}; //保留字表 int n = 0; word[n++] = a[i++]; //若字符为A~Z或0~9,则继续读取 while ((a[i] >= 'A'&&a[i] <= 'Z') || (a [i] >= '0' && a[i] <= '9')||(a[i]>='a'&&a[i]<='z')) { word[n++] = a[i++]; } word[n] = '\0'; i--;
执行结果:
-
编译原理词法分析器的设计
2019-11-26 21:01:35编写一个词法分析器,从输入的源程序(编写的语言为C语言的一个子集)中,识别出各个具有独立意义的单词,即基本保留字、标识符、常数、运算符、分隔符五大类。并依次输出各个单词的内部编码及单词符号自身值。... -
编译原理 词法分析器、语法分析器实现,桌面应用程序
2020-08-09 17:36:34visualStudio2019运行,c# wpf桌面应用程序,实现了简单的词法分析器、语法分析器功能,主要功能为打开文件,保存文件,另存文件,词法分析,语法分析(if语句,布尔表达式等等) -
编译原理词法分析器(C++版)源代码
2019-01-05 13:36:32自己实现的编译原理的词法分析器,是自己的实验作业,用Vs2017实现,可以直接运行,代码注释丰富,希望与大家交流学习!欢迎大家下载!
-
37岁老码农现身说法:投了500份简历,却只收到了3个面试邀请
-
【硬核】一线Python程序员实战经验分享(1)
-
int128模板
-
Monkey_test.zip
-
图形衬底参数对LED发光效率的影响
-
454. 四数相加 II
-
Mac中配置gradle环境及使用android studio打包jar包与arr包的方法
-
华工信号与系统真题与详解.zip
-
Python启蒙到架构师的核心技术精讲课程
-
朱老师c++课程第3部分-3.5STL的其他容器讲解
-
SNOW-V-VHDL
-
Keil.STM32F2xx_DFP.2.9.0.1.rar
-
在 Linux 上构建企业级 DNS 域名解析服务
-
项目管理工具与方法
-
jlink-v8 固件.zip
-
2021年 系统架构设计师 系列课
-
选中后样式
-
Command ‘ifconfig’ not found, but can be installed with: sudo apt install net-tools
-
二极管抽运全固态1.319 μm连续锁模激光器
-
Galera 高可用 MySQL 集群(PXC v5.7+Hapro)