c++ 简单的词法分析

时间:2022-05-06 21:31:27

scanner.h

#include<iostream>
#include<fstream>
#include<string>
using namespace std; class Scanner{
private:
string infile;
string outfile;
string key[33];
string helpkey[33];
public:
Scanner(string infile_temp,string outfile_temp);
void readFile();
void getToken(string s,ofstream &out);
bool isLetter(char ch);
bool isDigit(char ch);
int reserve(const string& s); };

  scanner.cpp

#include "scanner.h"

using namespace std;

Scanner::        Scanner(string infile_temp,string outfile_temp){
infile = infile_temp;
outfile = outfile_temp;
string key_temp[] = {"","auto","double","int","struct","break","else","long","switch",
"case", "enum","register","typedef","char","extern","return","union","const",
"float","short","unsigned","continue","for","signed","void","default","goto",
"sizeof","volatile","do","if","while","static"}; for(int i=;i<;i++){
key[i] = key_temp[i];
} } void Scanner::readFile(){ ifstream in(infile);
ofstream out(outfile);
string s; while(getline(in,s)){
getToken(s,out);
} } //判断是否letter
bool Scanner::isLetter(char ch){
if((ch>=&&ch<=)||(ch>=&&ch<=)||ch==||ch==)
return true;
else
return false; } //判断是否数字
bool Scanner::isDigit(char ch){
if(ch>=&&ch<=)
return true;
else
return false;
} //查找关键字
int Scanner::reserve(const string& s){
for(int i=;i<;i++)
if(s==key[i])
return i;
return ; } void Scanner::getToken(string s,ofstream &out){ size_t i=,code;
char ch;
string temp="";
ch=s[i];
while(i<s.length()){ //如果是空跳过
while(i<s.length()&&ch==' '){
i++;
ch=s[i];
}
//是字母
if(isLetter(ch)){
while((isLetter(ch)||isDigit(ch))&&i<s.length()){
temp+=ch;
i++;
ch=s[i];
}
i--;
code=reserve(temp);
if(code==){
out<<temp<<'\t'<<"标识符"<<endl;
temp="";
}else{
out<<temp<<'\t'<<"关键字"<<endl;
temp="";
} }else if(isDigit(ch)){
while(isDigit(ch)){
temp+=ch;
i++;
ch=s[i];
}
i--;
out<<temp<<'\t'<<"常数"<<endl;
temp="";
}else if(ch=='='){
i++;
ch=s[i];
if(ch== '=' )
out<<"=="<<'\t'<<"判断相等"<<endl;
else{
i--;
out<<"="<<'\t'<<"赋值"<<endl;
} }
else if(ch=='+'){
i++;
ch=s[i];
if(ch=='+')
out<<"++"<<'\t'<<"加1"<<endl;
else{
i--;
out<<"+"<<'\t'<<"加号"<<endl;
}
}
else if(ch=='&'){
i++;
ch=s[i];
if(ch=='&')
out<<"&&"<<'\t'<<"与"<<endl;
else{
i--;
out<<"&"<<'\t'<<"按位与"<<endl;
}
}
else if(ch=='|'){
i++;
ch=s[i];
if(ch=='|')
out<<"||"<<'\t'<<"或"<<endl;
else{
i--;
out<<"|"<<'\t'<<"按位或"<<endl;
}
} else if(ch=='-')
out<<ch<<'\t'<<"减号"<<endl;
else if(ch==';')
out<<ch<<'\t'<<"分号"<<endl;
else if(ch=='(')
out<<ch<<'\t'<<"左括号"<<endl;
else if(ch==')')
out<<ch<<'\t'<<"右括号"<<endl;
else if(ch=='{')
out<<ch<<'\t'<<"左花括号"<<endl;
else if(ch=='}')
out<<ch<<'\t'<<"右花括号"<<endl;
else if(ch=='*'){
i++;
ch=s[i];
if(ch=='*')
out<<"**"<<'\t'<<"运算符"<<endl;
else{
i--;
out<<"*"<<'\t'<<"乘号"<<endl;
} }
else if(ch=='<'){
i++;
ch=s[i];
if(ch=='=')
out<<"<="<<'\t'<<"小于等于"<<endl;
else{
i--;
out<<"<"<<'\t'<<"小于"<<endl;
} }
else if(ch=='>'){
i++;
ch=s[i];
if(ch=='=')
out<<">="<<'\t'<<"大于"<<endl;
else{
i--;
out<<">"<<'\t'<<"小于"<<endl;
} }
else
return ;
i++;
ch=s[i];
} }

c++ 简单的词法分析的更多相关文章

  1. 用C语言编写一个简单的词法分析程序

    问题描述: 用C或C++语言编写一个简单的词法分析程序,扫描C语言小子集的源程序,根据给定的词法规则,识别单词,填写相应的表.如果产生词法错误,则显示错误信息.位置,并试图从错误中恢复.简单的恢复方法 ...

  2. 简单的词法分析和语法分析(C&plus;&plus;实现,CodeBlocks&plus;GCC编译)

    说明: 分析的语言是SNL语言,详见<编译程序的设计与实现>( 刘磊.金英.张晶.张荷花.单郸编著) 词法分析就是实现了词法分析的自动机 语法分析使用递归下降法 运行结果: 词法分析 得到 ...

  3. java 简单的词法分析

    package com.seakt.example; import java.io.*; import java.lang.String; public class J_Scanner { publi ...

  4. 词法分析程序 LEX和VC6整合使用的一个简单例子

    词法分析的理论知识不少,包括了正规式.正规文法.它们之间的转换以及确定的有穷自动机和不确定的有穷自动机等等... 要自己写一个词法分析器也不会很难,只要给出了最简的有穷自动机,就能很方便实现了,用if ...

  5. Yacc 与 Lex 快速入门(词法分析和语法分析)

    我们知道,高级语言,一般的如c,Java等是不能直接运行的,它们需要经过编译成机器认识的语言.即编译器的工作. 编译器工作流程:词法分析.语法分析.语义分析.IR(中间代码,intermediate ...

  6. GCC编译器原理(三)------编译原理三:编译过程(2-1)---编译之词法分析

    二.编译 引用文档:https://blog.csdn.net/chdhust/article/details/9040647 编译过程就是把预处理完的文件进行一系列词法分析.语法分析.语义分析及优化 ...

  7. 使用JavaScript实现一个简单的编译器

    在前端开发中也会或多或少接触到一些与编译相关的内容,常见的有 将ES6.7代码编译成ES5的代码 将SCSS.LESS代码转换成浏览器支持的CSS代码 通过uglifyjs.uglifycss等工具压 ...

  8. jQuery 2&period;0&period;3 源码分析Sizzle引擎 - 词法解析

    声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 浏览器从下载文档到显示页面的过程是个复杂的过程,这里包含了重绘和重排.各家浏览器引擎的工作原理略有差别,但也有一定规则. 简 ...

  9. Yacc 与 Lex 快速入门

    Yacc 与 Lex 快速入门 Lex 与 Yacc 介绍 Lex 和 Yacc 是 UNIX 两个非常重要的.功能强大的工具.事实上,如果你熟练掌握 Lex 和 Yacc 的话,它们的强大功能使创建 ...

随机推荐

  1. myeclipse自动排版

    myeclipse代码排版方式有两种: 1. ctr+f 实现自动排版: 2. myeclipse->Preference->Java->Editor->Sava Action ...

  2. 微信公众平台企业号验证接口、回调 PHP版

    微信公众平台企业号验证接口.回调 PHP版,本人为了解决这个企业号的验证和发送消息的问题,整整研究了几天时间,由于微信企业号刚推出来,网上资料太少了!后来在一些朋友的帮助下和本人重复调试完好下,最终整 ...

  3. java循环

    .增强for循环和iterator遍历的效果是一样的,也就说增强for循环的内部也就是调用iteratoer实现的(可以查看编译后的文件),但是增强for循环 有些缺点,例如不能在增强循环里动态的删除 ...

  4. Get-CrmSetting返回Unable to connect to the remote server的解决办法

    摘要: 微软动态CRM专家罗勇 ,回复302或者20190125可方便获取本文,同时可以在第一间得到我发布的最新博文信息,follow me!我的网站是 www.luoyong.me . 在Dynam ...

  5. 【转】史上最详细的Composer安装tp5教程

    http://www.thinkphp.cn/topic/52362.html Composer安装tp5教程1.下载composer先介绍几个网站Composer官网https://getcompo ...

  6. React(五)State属性

    React 里,只需更新组件的 state,然后根据新的 state 重新渲染用户界面(不要操作 DOM). 以下实例创建一个名称扩展为 React.Component 的 ES6 类,在 rende ...

  7. mysql error You must reset your password using ALTER USER statement before executing this statement&period;

    mysql修改密码Your password does not satisfy the current policy requirements 出现这个问题的原因是:密码过于简单.刚安装的mysql的 ...

  8. linux内核分析 第三周

    一.Linux内核源码(简单分析) README 一开始刚接触内核源码的时候,不知道代码文件是什么功能.不清楚如何使用文件的时候,就需要打开README. README提供了内核的各种编译方法.生成文 ...

  9. Class 1

    “在最艰苦的时候,就是你离成功最近的时候”,让暴风雨来得更猛烈些吧. 健身教练/学员,买的那本Java Web还是那么新,显然假期偷懒了,只能一点一点的补回来了.一个假期没有打开过自己的脑洞,真心醉了 ...

  10. html- 头部元素

    一:HTML <head> 元素 <head> 元素是所有头部元素的容器.<head> 内的元素可包含脚本,指示浏览器在何处可以找到样式表,提供元信息,等等. 以下 ...