博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
词法分析器 /c++实现
阅读量:4591 次
发布时间:2019-06-09

本文共 2192 字,大约阅读时间需要 7 分钟。

#include
#include
#include
#include
#include
#include
#include
using namespace std;int line=1,row=1;char c;map
ma;struct kind{ string na; //单词 int num; //内码 string type; //类型 int l,r; //行列数};vector
res; //视图表int tree[10000][30];int numv=0;int w[10000];int key=0; int flag=0;void insert(string s) //关键词trie树{ int u=0; for(int i=0;i
='a'&&c<='z')) if(c!='_') if(!(c>='0'&&c<='9')) break; if(mark==1) { if(tree[u][c-'a']!=0) u=tree[u][c-'a']; else mark=0; } s+=c; } kind t; t.na=s; t.l=line; t.r=row; if(mark==0) { t.num=flag++; t.type=" 标志符"; } else { t.num=w[u]; t.type=" keyword"; } res.push_back(t); return ;}void f2(char &c) //数字分析器{ string s; s+=c; c=getchar(); while(c>='0'&&c<='9') { s+=c; c=getchar(); } int mark=1; while(c>='a'&&c<='z') {s+=c;mark=0;c=getchar();} kind t; t.na=s; t.l=line; t.r=row; if(mark==1) { char *p; strcpy(p,s.c_str()); t.num=atoi(p); t.type=" 数字"; } else { t.type=" error"; t.num=0; } res.push_back(t); return ;}void f3(char &c) //分界符、运算符等其它分析器{ string s; kind t; t.l=line;t.r=row; if(ma.find(c)!=ma.end()) { t.na+=c; if(ma[c]<=8)t.type=" 分界符"; else t.type=" 运算符"; t.num=ma[c]; c=getchar(); } else { t.type=" 关系运算符"; if(c=='>') { c=getchar(); if(c=='=') { t.na=">="; t.num=1; c=getchar(); } else { t.na=">"; t.num=2; } } else if(c=='<') { c=getchar(); if(c=='=') { t.na="<="; t.num=3; c=getchar(); } else if(c=='>') { t.na="<>"; t.num=4; c=getchar(); } else { t.na="<"; t.num=5; } } else if(c=='=') { t.na="="; t.num=6; c=getchar(); } } res.push_back(t); return ;}int main() //总控程序{ ma['[']=1;ma[']']=2;ma[',']=3;ma[';']=4;ma['(']=5;ma[')']=6;ma['{']=7;ma['}']=8; ma['+']=9;ma['-']=10;ma['*']=11;ma['/']=12;ma['%']=13; string s; while(s!="end") { cin>>s; insert(s); } getchar(); cout<<"请输入语句:"<
='a'&&c<='z') f1(c); else if(c>='0'&&c<='9') f2(c); else f3(c); row++; if(c=='\n') { line++; row=1; c=getchar(); } } cout<<"单词"<<'\t'<<'\t'<<"二元序列"<<'\t'<<'\t'<<"类型"<<'\t'<<'\t'<<"位置(行。列)"<
 
 

測试:

if while int char else then do 

end
int a=10;
int b=11;
int s=0;
if(a>=10)
{
    s=a+b%2;
}
else
{
   s=a-b*a/11;
}
int 3b;
@    

转载于:https://www.cnblogs.com/jzdwajue/p/7171298.html

你可能感兴趣的文章
领域驱动设计之聚合与聚合根实例一
查看>>
selenium中各个模块操作:下拉框、鼠标悬浮连贯、拼图拖拽操作
查看>>
C# 调用Windows图片查看器
查看>>
Excel系列教程(1):如何自动填充单元格
查看>>
jQuery中的冒泡事件和阻止冒泡
查看>>
pythonchallenge闯关 第13题
查看>>
linux上很方便的上传下载文件工具rz和sz使用介绍
查看>>
React之特点及常见用法
查看>>
【WEB前端经验之谈】时间一年半,或沉淀、或从零开始。
查看>>
优云软件助阵GOPS·2017全球运维大会北京站
查看>>
java23中设计模式只责任链模式
查看>>
linux 装mysql的方法和步骤
查看>>
poj3667(线段树区间合并&区间查询)
查看>>
51nod1241(连续上升子序列)
查看>>
SqlSerch 查找不到数据
查看>>
集合相关概念
查看>>
Memcache 统计分析!
查看>>
(Python第四天)字符串
查看>>
个人介绍
查看>>
使用python动态特性时,让pycharm自动补全
查看>>