c++ 简单的词法分析
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++ 简单的词法分析的更多相关文章
- 用C语言编写一个简单的词法分析程序
问题描述: 用C或C++语言编写一个简单的词法分析程序,扫描C语言小子集的源程序,根据给定的词法规则,识别单词,填写相应的表.如果产生词法错误,则显示错误信息.位置,并试图从错误中恢复.简单的恢复方法 ...
- 简单的词法分析和语法分析(C++实现,CodeBlocks+GCC编译)
说明: 分析的语言是SNL语言,详见<编译程序的设计与实现>( 刘磊.金英.张晶.张荷花.单郸编著) 词法分析就是实现了词法分析的自动机 语法分析使用递归下降法 运行结果: 词法分析 得到 ...
- java 简单的词法分析
package com.seakt.example; import java.io.*; import java.lang.String; public class J_Scanner { publi ...
- 词法分析程序 LEX和VC6整合使用的一个简单例子
词法分析的理论知识不少,包括了正规式.正规文法.它们之间的转换以及确定的有穷自动机和不确定的有穷自动机等等... 要自己写一个词法分析器也不会很难,只要给出了最简的有穷自动机,就能很方便实现了,用if ...
- Yacc 与 Lex 快速入门(词法分析和语法分析)
我们知道,高级语言,一般的如c,Java等是不能直接运行的,它们需要经过编译成机器认识的语言.即编译器的工作. 编译器工作流程:词法分析.语法分析.语义分析.IR(中间代码,intermediate ...
- GCC编译器原理(三)------编译原理三:编译过程(2-1)---编译之词法分析
二.编译 引用文档:https://blog.csdn.net/chdhust/article/details/9040647 编译过程就是把预处理完的文件进行一系列词法分析.语法分析.语义分析及优化 ...
- 使用JavaScript实现一个简单的编译器
在前端开发中也会或多或少接触到一些与编译相关的内容,常见的有 将ES6.7代码编译成ES5的代码 将SCSS.LESS代码转换成浏览器支持的CSS代码 通过uglifyjs.uglifycss等工具压 ...
- jQuery 2.0.3 源码分析Sizzle引擎 - 词法解析
声明:本文为原创文章,如需转载,请注明来源并保留原文链接Aaron,谢谢! 浏览器从下载文档到显示页面的过程是个复杂的过程,这里包含了重绘和重排.各家浏览器引擎的工作原理略有差别,但也有一定规则. 简 ...
- Yacc 与 Lex 快速入门
Yacc 与 Lex 快速入门 Lex 与 Yacc 介绍 Lex 和 Yacc 是 UNIX 两个非常重要的.功能强大的工具.事实上,如果你熟练掌握 Lex 和 Yacc 的话,它们的强大功能使创建 ...
随机推荐
- HDoj-1228-A + B
A + B Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- How To Set Dark Theme in Visual Studio 2010
Want to use the visual studio color theme editor to set the dark theme or other themes? Below shows ...
- Process Node.js 进程
Process 进程 process.argv 是命令行参数数组,第一个元素是node,第二个元素是脚本文件名,从第三个元素开始每个元素是一个运行参数. process.stdout 标准输出流 co ...
- 从文档流来看内联元素和块元素的css排版
veda原创[抄录]讲得很好存自己这里看 从文档流来看内联元素和块元素的css排版 CSS文档流与块级元素(block).内联元素(inline),之前翻阅不少书籍,看过不少文章, 看到所多的是零碎的 ...
- Asp.net 获取图片列表并打包下载
先引用:ICSharpCode.SharpZipLib.dll 后台代码: using System.IO; using ICSharpCode.SharpZipLib.Zip; using ICSh ...
- javascript中this指针的认识
javascript中上下文环境就是this指针,即被调用函数所处的环境.这个上下文环境在大多数情况下指的是函数运行时封装这个函数的那个对象:当不通过任何对象单独调用一个函数时,上下文环境指的就是全局 ...
- 浅谈Mybatis(三)
一.动态SQL 1.sql片段 解决sql语句的冗余代码问题. <sql id="SELECT_T_USER"> select id,name,password,bir ...
- php ZIP压缩类实例分享
php ZIP压缩类实例分享 <?php $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt ...
- jQ中prop与attr的区别
1.prop适用于HTML元素本身就带有的固有属性 2.attr适用于HTML元素我们自定义的属性 <input type="checkbox" value="复选 ...
- 联想V480关闭UEFI安装Win7
联想V480关闭UEFI安装Win7 http://www.dadclab.com/archives/3283.jiecao 故事背景 兔兔牛入了一枚Lenovo V480,预装Win8,想换成 ...