首页 > 其他分享 >【Lut语言(1)】词法分析器

【Lut语言(1)】词法分析器

时间:2023-07-22 15:22:18浏览次数:34  
标签:ch Lut cout get int 分析器 else 词法 retract

先为我的Lut语言写个词法分析器再说吧。

#include<bits/stdc++.h>
using namespace std;
string word[11]={"","int","char","double","goto","if","print","scan","eq","daf"};
string s,t,pp;char ch;int now,cnt;
int isword(string p){
	for(int i=1;i<=10;i++){
		if(word[i]==p)return i;
	}
	return 0;
}
char get(){return ch=s[now++];}
void retract(){--now,ch=0;}
void skip(){while(isspace(ch))get();}
void analysis(){
	while(now<s.size()){
		get();
		pp="";
		if(isspace(ch));else if(isalpha(ch)){
			while(isdigit(ch)||isalpha(ch))pp+=ch,get();
			retract();
			if(isword(pp)){
				cout<<++cnt<<":<"<<pp<<","<<"关键字"<<isword(pp)<<">\n";
			}
			else cout<<++cnt<<":<"<<pp<<","<<"标识符>\n";
		}
		else if(isdigit(ch)){
			while(isdigit(ch)||ch=='.')pp+=ch,get();
			retract();
			cout<<++cnt<<":<"<<pp<<","<<"常数>\n";
		}
		else if(ch=='='){
			get();
			if(ch=='=')cout<<++cnt<<":<==,运算符>\n";
			else cout<<++cnt<<":<=,运算符>\n",retract();
		}
		else if(ch=='+'){
			get();
			if(ch=='+')cout<<++cnt<<":<++,运算符>\n";
			else if(ch=='=')cout<<++cnt<<":<+=,运算符>\n";
			else cout<<++cnt<<":<+,运算符>\n",retract();
		}
		else if(ch=='-'){
			get();
			if(ch=='-')cout<<++cnt<<":<--,运算符>\n";
			else if(ch=='=')cout<<++cnt<<":<-=,运算符>\n";
			else cout<<++cnt<<":<-,运算符>\n",retract();
		}
		else if(ch=='*'){
			get();
			if(ch=='=')cout<<++cnt<<":<*=,运算符>\n";
			else cout<<++cnt<<":<*,运算符>\n",retract();
		}
		else if(ch=='/'){
			get();
			if(ch=='=')cout<<++cnt<<":</=,运算符>\n";
			else cout<<++cnt<<":</,运算符>\n",retract();
		}
		else if(ch=='%'){
			get();
			if(ch=='=')cout<<++cnt<<":<%=,运算符>\n";
			else cout<<++cnt<<":<%,运算符>\n",retract();
		}
		else if(ch=='&'){
			get();
			if(ch=='=')cout<<++cnt<<":<&=,运算符>\n";
			else cout<<++cnt<<":<&,运算符>\n",retract();
		}
		else if(ch=='^'){
			get();
			if(ch=='=')cout<<++cnt<<":<^=,运算符>\n";
			else cout<<++cnt<<":<^,运算符>\n",retract();
		}
		else if(ch=='|'){
			get();
			if(ch=='=')cout<<++cnt<<":<|=,运算符>\n";
			else cout<<++cnt<<":<|,运算符>\n",retract();
		}
		else if(ch=='>'){
			get();
			if(ch!='>')cout<<++cnt<<":<>,运算符>\n",retract();
			else{
				get();
				if(ch=='=')cout<<++cnt<<":<>>=,运算符>\n";
				else cout<<++cnt<<":>>,运算符>\n",retract();
			}
		}
		else if(ch=='<'){
			get();
			if(ch!='<')cout<<++cnt<<":<<,运算符>\n",retract();
			else{
				get();
				if(ch=='=')cout<<++cnt<<":<<<=,运算符>\n";
				else cout<<++cnt<<":<<<,运算符>\n",retract();
			}
		}
		else cout<<++cnt<<":<"<<ch<<",未知字符>\n";
	}
}
int main(){
	while(1){
		getline(cin,t);
		if(t=="end")break;
		s+=t+' ';
	}
	analysis();
	return 0;
}

标签:ch,Lut,cout,get,int,分析器,else,词法,retract
From: https://www.cnblogs.com/2021changqing52/p/17573421.html

相关文章

  • Flutter错误
    Flutter错误[Flutter错误|不器小窝](https://xingcxb.com/language/dart/b4845b/#%E5%9C%A8flutter%E5%BC%80%E5%8F%91%E4%B8%AD-%E5%B7%B2%E7%BB%8F%E5%AE%89%E8%A3%85%E4%BA%86cocopods-%E4%BD%86%E6%98%AF%E6%8F%90%E7%A4%BA%E6%9C%AA%E5%AE%89%E8%A3%85%E7%9A%84%E9%97%AE%......
  • Flutter开发VSCode快捷键
    快速创建widget:在dart文件中输入stf或stl出现提示后按⏎即可快速修复:⌘ + .自动生成构造函数:选中final参数,快捷键:⌘ + .添加父组件、变为子组件、删除子组件:⌘ + .重新打开关闭的编辑页面:⌘+ ⇧ +  T通过匹配文本打开文件:⌘ +  T代码格式化:⇧ +......
  • Image 和 Icon 区别 flutter
     https://baijiahao.baidu.com/s?id=1669477477846822809&wfr=spider&for=pc MaterialDesign所有图标可以在其官网查看:https://material.io/tools/icons/......
  • AT_abc251_g Intersection of Polygons Solution
    AT_abc251_gIntersectionofPolygonsSolutionPreface由于某些\(\LaTeX\)的原因,本文的公式无法正常查看,建议读者访问博客以获得正常阅读体验。Statement逆时针地给定一个有\(N\)个顶点,第\(i\)个顶点为\((x_i,y_i)\)的凸包\(P_0\)。再给出\(M\)个向量\((u_i,v......
  • 直播网站程序源码,Flutter:列表布局摘要
    直播网站程序源码,Flutter:列表布局摘要方式1(推荐,适用大量item,动态创建item): ListView.builder(//列表布局 scrollDirection:Axis.vertical, //(默认)vertical垂直排列,horizontal水平排列 itemCount:总个数,        //item总个数 itemExtent:高度值......
  • 编译安装最新的Pluto compiler,以及遇到的一些坑
    好久不见!这段时间在鼓捣一些奇奇怪怪的东西。PlutoCompiler是一款非常优秀的Polyhedral编译器。这玩意拿来优化循环和程序局部性啥的是相当好的。其安装过程涉及到整个llvm的编译过程,如果之前并没能够了解llvm的话估计会够呛,我也是基本上把坑踩了一个遍。所以干脆写篇博客给之......
  • Flutter ncnn 使用
    Flutter实现手机端App,如果想利用AI模型添加新颖的功能,那么ncnn就是一种可考虑的手机端推理模型的框架。本文即是Flutter上使用ncnn做模型推理的实践分享。有如下内容:ncnn体验:环境准备、模型转换及测试Flutter项目体验:本文demo_ncnn体验Flutter项目实现创建......
  • Flutter系列文章-Flutter基础
    Flutter是Google推出的一种新的移动应用开发框架,允许开发者使用一套代码库同时开发Android和iOS应用。它的设计理念、框架结构、以及对Widget的使用,都让开发者能更有效率地创建高质量的应用。一、Flutter设计理念Flutter的设计理念是“一切皆为Widget”。这意味着不论是按钮、字......
  • Flutter | 使用 InkResponse和 InkWell组件 实现事件操作
    可以包裹不具备事件处理的组件,实现水波纹等点击事件的效果;InkWell水波纹限制在文本组件之内;InkResponse水波纹没有限制;InkResponse和InkWell都可以指定各种响应颜色、手势等相关属性;  InkWell(radius:200.0,focusColor:Colors.red,hove......
  • flutter状态管理案例
     FLUTTER项目中管理不同组件、不同页面之间共享的数据关系。当需要共享的数据关系达到几十上百个的时候,我们就很难保持清晰的数据流动方向和顺序了,导致应用内各种数据传递嵌套和回调满天飞。在这个时候,我们迫切需要一个解决方案,来帮助我们理清楚这些共享数据的关系,于是状态管理......