资源简介
相关博客请访问https://blog.csdn.net/boss_crabe/article/details/80760252
代码片段和文件信息
#include
#include
#include
#include
#include
using namespace std;
const int MAX_SIZE = 1000000;
set reserved_words;
void init_reserved_words() {
reserved_words.insert(“Const“);
reserved_words.insert(“Var“);
reserved_words.insert(“if“);
reserved_words.insert(“then“);
reserved_words.insert(“else“);
reserved_words.insert(“while“);
reserved_words.insert(“do“);
reserved_words.insert(“begin“);
reserved_words.insert(“end“);
}
bool isdigit(char c) { return c >= ‘0‘ && c <= ‘9‘; }
bool isalpha(char c) { return (c >= ‘a‘ && c <= ‘z‘) || (c >= ‘A‘ && c <= ‘Z‘); }
void lexical_analysis(int flag) {
freopen(“file_out.txt“ “w“ stdout);
char s[MAX_SIZE];
char tmp[105];
int len tmp_len pos cnt = 0;
while(gets(s)) {
len = strlen(s);
cnt++;
pos = 0;
while (pos < len && s[pos] == ‘ ‘) pos++;
if (flag) printf(“%3d:“ cnt);
if (flag) {
if (pos == len)
puts(““);
else
printf(“ %s\n“ s + pos);
}
while (pos < len) {
while (pos < len && s[pos] == ‘ ‘) pos++;
if (pos == len) break;
tmp_len = 0;
if (flag) printf(“ %3d: “ cnt);
if (isdigit(s[pos])) {
while (pos < len && isdigit(s[pos]))
tmp[tmp_len++] = s[pos++];
tmp[tmp_len] = ‘\0‘;
if (flag) printf(“Number : %s\n“ tmp);
else printf(“%d %s\n“ cnt tmp);
}
else if (isalpha(s[pos])) {
while (pos < len && (isalpha(s[pos]) || isdigit(s[pos])))
tmp[tmp_len++] = s[pos++];
tmp[tmp_len] = ‘\0‘;
if (flag) {
if (reserved_words.count(tmp))
printf(“Reserved words : %s\n“ tmp);
else
printf(“Identifier : %s\n“ tmp);
}
else {
if (reserved_words.count(tmp))
printf(“%d %s\n“ cnt tmp);
else
printf(“%d %s\n“ cnt tmp);
}
}
else {
while(pos < len && !(isdigit(s[pos]) || isalpha(s[pos]) || s[pos] == ‘ ‘))
tmp[tmp_len++] = s[pos++];
tmp[tmp_len] = ‘\0‘;
if (flag)
printf(“%s\n“ tmp);
else
printf(“%d %s\n“ cnt tmp);
}
}
}
}
int main() {
freopen(“file_in.txt“ “r“ stdin);
init_reserved_words();
lexical_analysis(0);
return 0;
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2018-06-21 14:28 词法分析\
文件 56 2017-12-14 20:23 词法分析\file_in.txt
文件 152 2018-01-09 14:57 词法分析\file_out.txt
文件 648 2018-01-09 14:56 词法分析\file_show.txt
文件 2448 2017-12-15 13:05 词法分析\词法分析.cpp
文件 1899811 2018-01-09 14:57 词法分析\词法分析.exe
文件 14519 2018-06-21 14:28 词法分析\词法分析_谢俊峰.docx
文件 2975 2018-01-09 14:56 词法分析\词法分析展示.cpp
文件 1899811 2018-01-09 14:56 词法分析\词法分析展示.exe
- 上一篇:PL0语言编译器分析实验报告
- 下一篇:RDA5876编程指南_v1.3
相关资源
- 编译原理 词法分析,语法分析,中间
- 词法分析+语法分析(附实验报告)
- Python 实现的 C 词法分析器
- Sample语言编译器(词法分析、正规式
- 编译原理词法分析实验报告内含截图
- 编译原理——词法分析代码
- 实验一 词法分析程序实现
- 编译原理语法分析和词法分析
- PL0(包括对其的扩充)
- 词法分析器和语法分析器
- 编写一个词法分析器,它针对输入文
- Lex词法分析实验
- 算符优先分析和词法分析器的设计与
- TINY+词法分析完全
- 编译原理--词法分析实验(含代码)
- 一个简单编译器前端包括词法分析,
- 完整cmm解释器构造实践 二 词法分析
- pascal语言的词法分析器
- 编译原理上机作业-词法分析程序实现
- 编译程序构造
- 编译原理及实践手工构造词法分析器
- SeuLexSeuYacc
- pl0词法语法分析器
- 编译原理实验+报告+流程图
- 词法分析程序构造原理与实现技术
- 词法分析实验Flex和bison包含实验报告
- TINY+词法分析,语法分析,语义分析与
- 东北大学编译原理实验报告
- 实验一-词法分析通过Lex实现,实现代
评论
共有 条评论