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 的话,它们的强大功能使创建 ...
随机推荐
- 使用sphinx索引mysql数据
数据库表如下 mysql> select * from tb_account; +----+-------+------+ | id | name | age | +----+-------+- ...
- Gridland(规律)
Gridland Time Limit: 2 Seconds Memory Limit: 65536 KB BackgroundFor years, computer scientists ...
- dijkstra 优先队列最短路模板
;;*maxn];,):id(a),dist(b){} )); ;i<=n;i++)dist[i]=inf; dist[st]=; ;i= ...
- [poj 3678]Katu Pazzle[2-SAT常用建图法]
题意: 不说了..典型的2-SAT 常用模型: 重点: 突出"绑定性". 连线表示限制而非可行. 因为最后要求对立点不在同一强连通分量是说同一强连通中的点必须同时选. 坑: 首先是 ...
- ubuntu系统安装FTP
Ubuntu安装vsftp软件 1.更新软件源 首先须要更新系统的软件源,便捷工具下载地址:http://help.aliyun.com/manual?spm=0.0.0.0.zJ3dBU&h ...
- Deep Learning(深度学习)学习笔记整理系列之(七)
Deep Learning(深度学习)学习笔记整理系列 zouxy09@qq.com http://blog.csdn.net/zouxy09 作者:Zouxy version 1.0 2013-04 ...
- iOS 导航条的影响
如果是push出来的控制器,self.view的(0,0)点从状态栏下面开始: 如果有present出来的控制器,self.view的(0,0)点包含状态栏:
- 成功的背后!(给所有IT人)----转载:来自CSDN第一名博主
转载:来自CSDN第一名博主:http://blog.csdn.net/phphot/article/details/2187505 放在这里激励你我! 正文: 成功的背后,有着许多不为人知的故事,而 ...
- SGU 310. Hippopotamus( 状压dp )
题目大意:给N块板, 有A,B2种类型的板, 要求任意M块连续的板中至少有K块B板.1≤n≤60,1≤m≤15,0≤k≤m≤n. dp(x, s)表示第x块板, x前M块板的状态为s, 然后合法状态转 ...
- mysql中判断表中是否存在某条记录
SELECT CASE WHEN EXISTS (SELECT * FROM usergroupmap WHERE groupId = groupIdIn AND userId = v_friendI ...