【编译原理】c++实现词法分析器
写在前面:本博客为本人原创,严禁任何形式的转载!本博客只允许放在博客园(.cnblogs.com),如果您在其他网站看到这篇博文,请通过下面这个唯一的合法链接转到原文!
本博客全网唯一合法URL:http://www.cnblogs.com/acm-icpcer/p/8867199.html
Talk is cheap, show you my source code:
/*
this code was first initiated by TZ
contact email:xmb028@163.com
personal website:wnm1503303791.github.io
personal blogs:www.cnblogs.com/acm-icpcer/
this code has been posted on my personal blog,checking url:www.cnblogs.com/acm-icpcer/p/8867199.html
Copyright © 2018 TZ.
All Rights Reserved.
*/ #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<bitset>
#include<cstdlib>
#include<cmath>
#include<set>
#include<list>
#include<deque>
#include<map>
#include<queue>
using namespace std; char buffer[];
char remains[][]=
{
"begin",
"call",
"const",
"do",
"end",
"if",
"odd",
"procedure",
"read",
"then",
"var",
"while",
"write"
};
char r_output[][]=
{
"beginsym",
"callsym",
"constsym",
"dosym",
"endsym",
"ifsym",
"oddsym",
"proceduresym",
"readsym",
"thensym",
"varsym",
"whilesym",
"writesym"
}; int number(int i)
{
char temp[];
int t=;
while(buffer[i]>=&&buffer[i]<=)
{
temp[t++]=buffer[i++];
}
cout<<"(number,"<<temp<<")"<<endl;
return i;
} int letter(int i)
{
char temp[];
memset(temp,'\0',strlen(temp));
int t=;
while((buffer[i]>=&&buffer[i]<=)||(buffer[i]>=&&buffer[i]<=)||(buffer[i]>=&&buffer[i]<=))//判断基本字和标识符
{
temp[t++]=buffer[i++];
}
for(int a=;a<;a++)
{
if(strcmp(temp,remains[a])==)
{
cout<<"("<<r_output[a]<<","<<remains[a]<<")"<<endl;
return i;
}
}
cout<<"(ident,"<<temp<<")"<<endl;//默认该语法的标识符不可以以数字开头,否则此代码需重构
return i;
} int delimiter(int i)
{
char temp=buffer[i];
switch (temp)
{
case '(':
cout<<"(lparen, "<<buffer[i]<<" )"<<endl;
break;
case ')':
cout<<"(rparen, "<<buffer[i]<<" )"<<endl;
break;
case ',':
cout<<"(comma, "<<buffer[i]<<" )"<<endl;
break;
case ';':
cout<<"(semicolon, "<<buffer[i]<<" )"<<endl;
break;
case '.':
cout<<"(period, "<<buffer[i]<<" )"<<endl;
break;
}
return ++i;
} int operators(int i)
{
char temp=buffer[i];
switch (temp)
{
case '+':
cout<<"(plus, "<<buffer[i]<<" )"<<endl;
return ++i;
case '-':
cout<<"(minus, "<<buffer[i]<<" )"<<endl;
return ++i;
case '*':
cout<<"(times, "<<buffer[i]<<" )"<<endl;
return ++i;
case '/':
cout<<"(slash, "<<buffer[i]<<" )"<<endl;
return ++i;
case '=':
cout<<"(eql, "<<buffer[i]<<" )"<<endl;
return ++i; case '<':
if(buffer[i+]=='>')
{
cout<<"(neq, "<<buffer[i]<<buffer[i+]<<" )"<<endl;
return (i+);
}
if(buffer[i+]=='=')
{
cout<<"(leq, "<<buffer[i]<<buffer[i+]<<" )"<<endl;
return (i+);
}
cout<<"(less, "<<buffer[i]<<" )"<<endl;
return ++i; case '>':
if(buffer[i+]=='=')
{
cout<<"(geq, "<<buffer[i]<<buffer[i+]<<" )"<<endl;
return (i+);
}
cout<<"(gtr, "<<buffer[i]<<" )"<<endl;
return ++i; case ':':
if(buffer[i+]=='=')
{
cout<<"(becomes, "<<buffer[i]<<buffer[i+]<<" )"<<endl;
return (i+);
}
else return i;
}
} int main()
{
memset(buffer,'\0',strlen(buffer));
while(scanf("%s",&buffer))
{
int pointer=;
//processing
while(pointer<strlen(buffer))
{
//1:number
if( buffer[pointer]>=
&&buffer[pointer]<= )//go to the number process
pointer=number(pointer);
//2:letter
else if( (buffer[pointer]>=&&buffer[pointer]<=)
||(buffer[pointer]>=&&buffer[pointer]<=) )//go to letter process
pointer=letter(pointer);
//3:delimiter
else if( buffer[pointer]=='('
||buffer[pointer]==')'
||buffer[pointer]==','
||buffer[pointer]==';'
||buffer[pointer]=='.' )//prcessing delimiter,in chinese we call:jie fu~
pointer=delimiter(pointer);
//4:operators
else if( buffer[pointer]=='+'
||buffer[pointer]=='-'
||buffer[pointer]=='*'
||buffer[pointer]=='/'
||buffer[pointer]=='=' ||buffer[pointer]=='<'
||buffer[pointer]=='>'
||buffer[pointer]==':' )//prcessing operators
pointer=operators(pointer);
else
{
cout<<"ERROR!"<<endl;
break;
}
}
} return ;
}
源码运行结果:

将源代码改为使用文件输出后的结果:


tz@COI HZAU
2018/4/17
【编译原理】c++实现词法分析器的更多相关文章
- 编译原理 #01# 简易词法分析器(js实现)
// 实验存档 效果图: 代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- 编译原理-一种词法分析器LEX原理
1.将所有单词的正规集用正规式描述 2.用正规式到NFA的转换算 得到识别所有单词用NFA 3.用NFA到DFA的转换算法 得到识别所有单词用DFA 4.将DFA的状态转换函数表示成二维数组 并与DF ...
- 编译原理(简单自动词法分析器LEX)
编译原理(简单自动词法分析器LEX)源程序下载地址: http://files.cnblogs.com/files/hujunzheng/%E6%B1%87%E7%BC%96%E5%8E%9F%E7 ...
- <编译原理 - 函数绘图语言解释器(1)词法分析器 - python>
<编译原理 - 函数绘图语言解释器(1)词法分析器 - python> 背景 编译原理上机实现一个对函数绘图语言的解释器 - 用除C外的不同种语言实现 解释器分为三个实现块: 词法分析器: ...
- 编译原理实战——使用Lex/Flex进行编写一个有一定词汇量的词法分析器
编译原理实战--使用Lex/Flex进行编写一个有一定词汇量的词法分析器 by steve yu 2019.9.30 参考文档:1.https://blog.csdn.net/mist14/artic ...
- 跟vczh看实例学编译原理——三:Tinymoe与无歧义语法分析
文章中引用的代码均来自https://github.com/vczh/tinymoe. 看了前面的三篇文章,大家应该基本对Tinymoe的代码有一个初步的感觉了.在正确分析"print ...
- 跟vczh看实例学编译原理——二:实现Tinymoe的词法分析
文章中引用的代码均来自https://github.com/vczh/tinymoe. 实现Tinymoe的第一步自然是一个词法分析器.词法分析其所作的事情很简单,就是把一份代码分割成若干个tok ...
- Compiler Theory(编译原理)、词法/语法/AST/中间代码优化在Webshell检测上的应用
catalog . 引论 . 构建一个编译器的相关科学 . 程序设计语言基础 . 一个简单的语法制导翻译器 . 简单表达式的翻译器(源代码示例) . 词法分析 . 生成中间代码 . 词法分析器的实现 ...
- 【编译原理】语法分析LL(1)分析法的FIRST和FOLLOW集
近来复习编译原理,语法分析中的自上而下LL(1)分析法,需要构造求出一个文法的FIRST和FOLLOW集,然后构造分析表,利用分析表+一个栈来做自上而下的语法分析(递归下降/预测分析),可是这个FIR ...
- javac编译原理(一)
我们都知道,计算机只能识别二进制语言,是不能直接识别java c c++等高级语言的.将高级语言转化成计算机可以是别的二进制语言,这个过程就叫编译. 有次面试,面试官问了一道“java的编译原理是什么 ...
随机推荐
- 【Linux高级驱动】linux设备驱动模型之平台设备驱动机制
[1:引言: linux字符设备驱动的基本编程流程] 1.实现模块加载函数 a.申请主设备号 register_chrdev(major,name,file_operations); b.创 ...
- Nginx的upstream目前支持5种分配方式
本文转自:http://mp.weixin.qq.com/s?__biz=MzI4OTU3ODk3NQ==&mid=2247484058&idx=1&sn=f4da816bfa ...
- 【iCore4 双核心板_ARM】例程二十一:LWIP_TCP_SERVER实验——以太网数据传输
实验现象: 核心代码: int main(void) { system_clock.initialize(); led.initialize(); adc.initialize(); delay.in ...
- Android文档-开发者指南-第一部分:入门-中英文对照版
发布的博客,排版太不行了,整理下发在百度盘上了: 第一部分:Introduction(入门) 0.Introduction to Android(引进到Android) 1.Application F ...
- 【PHP】xampp配置多个监听端口和不同的网站目录(转)
转自:https://blog.csdn.net/cc1314_/article/details/75646344 windows下使用xampp配置多个监听端口和不同的网站目录 一:配置Apache ...
- js给原型增加新属性和方法
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- redis aof和rdb区别
转自https://blog.csdn.net/m0_38110132/article/details/76906422 1.前言 最近在项目中使用到Redis做缓存,方便多个业务进程之间共享数据.由 ...
- B - Is It A Tree?
来源 hdu 1325 A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...
- F - Rails
There is a famous railway station in PopPush City. Country there is incredibly hilly. The station wa ...
- windows 上的 neovim 配置
可以使用简单的 linux 下 neovim 配置,增加了对 golang, python, ruby 脚本文件一键运行快捷方式. """""&qu ...