您的位置:首页 > 编程语言 > C语言/C++

词法分析器C++读取外部txt(编译原理)

2020-02-01 04:54 1366 查看

词法分析器C++读取外部txt(编译原理)

词法分析器的参考文章:
https://blog.csdn.net/qq_32623363/article/details/79911321)
在原文章的基础上,我加入了关键字的识别,并将词法分析器设计为一个独立的程序,从文件中读取源程序,将其变换成相应的符号序列;

c++读取txt文件的参考文章https://blog.csdn.net/m0_38033475/article/details/94394087

#include <iostream>
#include <string>
#include<fstream>
using namespace std;

#define MAX_LEN 200

//将一个字符串内容清空
void clearArray(char *c)
{
int len = strlen(c);
for(int i = 0; i < len; i++)
{
c[i] = '\0';
}
}

//判断是否为数字,true为是,false为不是
bool isDigit(char c){
if(c >= '0' && c <= '9')
{
return true;
}
else
{
return false;
}
}

//判断是否为字母,ture为是,false为不是
bool isChar(char c)
{
if( (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') )
{
return true;
}
else
{
return false;
}
}

/*
* 单词输出函数
* 该单词的意义
* 经过词法分析器分析后得到的单个单词
*/
void print(string label, char *word)
{
ofstream outfile("output.txt",ios::app);
outfile << label << ":  " << word << endl;
}

/*
* 词法分析函数
* 输入字符串指针
* 该函数对输入串进行分析,并分割成单词后输出
*/
void analysis(char* inChar)
{

//用于对词法分析后的单词进行保存
char tempChar[MAX_LEN] = {0};
// 作为tempChar的一个索引
int j = 0;

//用于遍历inChar字符数组
int i = 0;
while(inChar[i] != '\0')
{
switch(inChar[i])
{
case '-':
tempChar[j++] = inChar[i];
print("减号",tempChar);
j = 0;
clearArray(tempChar);
break;
case '=':
tempChar[j++] = inChar[i];
print("等号",tempChar);
j = 0;
clearArray(tempChar);
break;
case '+':
tempChar[j++] = inChar[i];
print("加号",tempChar);
j = 0;
clearArray(tempChar);
break;
case ',':
tempChar[j++] = inChar[i];
print("逗号",tempChar);
j = 0;
clearArray(tempChar);
break;
case ';':
tempChar[j++] = inChar[i];
print("分号",tempChar);
j = 0;
clearArray(tempChar);
break;
case '/':
tempChar[j++] = inChar[i];
print("除号",tempChar);
j = 0;
clearArray(tempChar);
break;
case '(':
tempChar[j++] = inChar[i];
print("左括号",tempChar);
j = 0;
clearArray(tempChar);
break;
case ')':
tempChar[j++] = inChar[i];
print("右括号",tempChar);
j = 0;
clearArray(tempChar);
break;
case '<':
tempChar[j++] = inChar[i];
print("小于号",tempChar);
j = 0;
clearArray(tempChar);
break;
case '>':
tempChar[j++] = inChar[i];
print("大于号",tempChar);
j = 0;
clearArray(tempChar);
break;
case '*':
if(inChar[i+1] != '*')
{
tempChar[j++] = inChar[i];
print("乘法",tempChar);
j = 0;
clearArray(tempChar);
}
else
{
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i];
print("平方",tempChar);
j = 0;
clearArray(tempChar);
}
break;
default:
//判断是否为字母+数字
if(isChar(inChar[i]))
{
if(inChar[i]=='i' && inChar[i+1]=='f')
{
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i];
print("关键字",tempChar);
j=0;
clearArray(tempChar);
i++;
continue;
}
else if(inChar[i]=='d' && inChar[i+1]=='o')
{
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i];
print("关键字",tempChar);
j=0;
clearArray(tempChar);
i++;
continue;
}
else if(inChar[i]=='t' && inChar[i+1]=='h' && inChar[i+2]=='e' && inChar[i+3]=='n')
{
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i];
print("关键字",tempChar);
j=0;
clearArray(tempChar);
i++;
continue;
}
else if(inChar[i]=='e' && inChar[i+1]=='l' && inChar[i+2]=='s' && inChar[i+3]=='e')
{
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i];
print("关键字",tempChar);
j=0;
clearArray(tempChar);
i++;
continue;
}
else if(inChar[i]=='w' && inChar[i+1]=='h' && inChar[i+2]=='i' && inChar[i+3]=='l' && inChar[i+4]=='e')
{
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i++];
tempChar[j++] = inChar[i];
print("关键字",tempChar);
j=0;
clearArray(tempChar);
i++;
continue;
}
else
{
j = 0;
tempChar[j++] = inChar[i];
while (isChar(inChar[i + 1]) || isDigit(inChar[i + 1]))
{
i++;
tempChar[j++] = inChar[i];
}
j = 0;
print("标识符", tempChar);
clearArray(tempChar);
}
}
else if(isDigit(inChar[i]))
{
j = 0;
tempChar[j++] = inChar[i];
while(isDigit(inChar[i+1]))
{
i++;
tempChar[j++] = inChar[i];
}
j = 0;
print("数字",tempChar);
clearArray(tempChar);
}
else
{
tempChar[j++] = inChar[i];
print("其他",tempChar);
j = 0;
clearArray(tempChar);
}

}
i++;

}
}

int main()
{
char inChar[MAX_LEN] = {0};
ifstream myfile("input.txt");
ofstream outfile("output.txt",ios::app); //ios::app指追加写入

//获取输入字符串,输入的字符串以空格、回车、换行作为结束符
myfile>>inChar;

//对输入的字符串进行分析
analysis(inChar);

myfile.close();
outfile.close();

return 0;
}

输入示例:

输出:

  • 点赞
  • 收藏
  • 分享
  • 文章举报
eqopp 发布了9 篇原创文章 · 获赞 5 · 访问量 1153 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: