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 的话,它们的强大功能使创建 ...
随机推荐
- 面向对象程序设计-C++_课时26拷贝构造Ⅰ_课时27拷贝构造Ⅱ
一旦写了一个类,给它3个函数: 1default construtor 2virtual destructor 3copy constructor Constructions vs. assignme ...
- hdu5461 Largest Point(沈阳网赛)
Largest Point Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total ...
- VBA 开发学习--基础语法
MsgBox "开始学习VBA" '提示框 Dim str As String '声明str变量是string类型 Let str = "一起来看流星雨" '给 ...
- JavaScript之Style属性学习
当CSS使用伪类开始侵入DOM和JavaSCript所控制着的行为层时,DOM和JavaScript也使用他们的一系列样式去控制表现层,这篇随笔主要说的就是利用JavaScript去控制元素的表现形式 ...
- 基于Jquery的Ajax分页,只有上一页和下一页
最近项目中用到ajax分页 在网上找到一个非常好用的分页插件jquery-pagination-ajax,以下是链接 http://www.zhangxinxu.com/wordpress/2010/ ...
- 浅谈HtmlParser
使用Heritrix抓取到自己所需的网页后,还需要对网页中的内容进行分类等操作,这个时候就需要用到htmlparser,但是使用htmlparser并不是那么容易!因为相关的文档比较少,很多更能需要开 ...
- 注意:只有xcode5.1创建的项目会自动适配iphone6,iphone6p
注意:只有xcode5.1创建的项目会自动适配iphone6,iphone6p
- TCP的流量控制
TCP协议作为一个可靠的面向字节流的传输协议,其可靠性和流量控制由滑动窗口协议保证,而拥塞控制则由控制窗口结合一系列的控制算法实现. 要区分TCP的流量控制和拥塞控制: 流量控制是发送方的发送数据的速 ...
- ffmpeg用法
1. help ffmpeg.exe -h > help.txt 2. 解码: ffmpeg -i 123.264 123.yuv ffmpeg -i 123.264 -vframes 200 ...
- 一个好用的hash函数(C语言)
typedef unsigned int DWORD; typedef unsigned char BYTE; /******************************************* ...