资源简介
相关博客请访问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
相关资源
- 编译词法分析器识别关键字常数和符
- SAMPLE (类pascal) 词法分析程序 C 版
- Delphi做的用于分析Pascal语言的词法分
- 编译原理实验:词法分析,语法分析
- 编译原理语法分析器、词法分析器
- 哈工大威海-编译原理实验报告和源码
- 编译原理课程设计 while do循环语句翻
- 词法分析器 -计算器版
- 词法语法分析器
- 词法分析器自动生成器 FLEX
- 中科院汉语词法分析系统ICTCLAS安装包
- 词法分析器Lex(编译原理)
- 华工编译原理实验词法分析+语法分析
- 词法分析器
- 词法分析器有界面
- 武汉大学国际软件学院解释器构造作
- 编译原理课程设计(词法分析和语法
- 利用FLEX设计一个small c的词法分析器文
- 编译原理 词法和语法分析
- 编译原理实验-flex自动构造词法分析
- 编译原理词法分析,LL1,SLR1)实验报
- 词法分析语法分析实验报告及源码
- 编译原理词法分析语法分析
- C Minus 语法分析器及课程设计报告
- 词法分析器+语法分析器+中间代码生成
- 编译原理 王生原老师答案和资料
- 词法分析程序和语法分析程序 编译原
- 词法分析程序可记录行号、识别注释
- 哈工大编译原理词法分析实验报告
- 编译器词法分析,语法分析
评论
共有 条评论