博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
c++ 简单的词法分析
阅读量:4562 次
发布时间:2019-06-08

本文共 1869 字,大约阅读时间需要 6 分钟。

scanner.h

#include
#include
#include
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[33] = {
"","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=0;i<33;i++){ key[i] = key_temp[i]; } }void Scanner::readFile(){ ifstream in(infile); ofstream out(outfile); string s; while(getline(in,s)){ getToken(s,out); }}//判断是否letterbool Scanner::isLetter(char ch){ if((ch>=65&&ch<=90)||(ch>=97&&ch<=122)||ch==35||ch==46) return true; else return false;}//判断是否数字bool Scanner::isDigit(char ch){ if(ch>=48&&ch<=57) return true; else return false;}//查找关键字int Scanner::reserve(const string& s){ for(int i=1;i<33;i++) if(s==key[i]) return i; return 0;}void Scanner::getToken(string s,ofstream &out){ size_t i=0,code; char ch; string temp=""; ch=s[i]; while(i
'){ i++; ch=s[i]; if(ch=='=') out<<">="<<'\t'<<"大于"<
"<<'\t'<<"小于"<

 

转载于:https://www.cnblogs.com/seakt/p/4478049.html

你可能感兴趣的文章
HDU 5435
查看>>
git从已有分支拉新分支开发
查看>>
滚动条隐藏兼容写法
查看>>
SQL2005查询所有表的大小
查看>>
Shell 正则表达式
查看>>
Docker run命令参数整理
查看>>
qt-opencv配置mingw编译器
查看>>
CSS之Medial Queries的另一用法:实现IE hack的方法
查看>>
linux-CentOS6.4下安装oracle11g详解
查看>>
实力为王 八年DBA经验谈
查看>>
2-sat 问题 【例题 Flags(2-sat+线段树优化建图)】
查看>>
ext3.2 右击动态添加node的treepanel
查看>>
Database links
查看>>
数据库事务
查看>>
xe7 控件升级
查看>>
TFrame bug
查看>>
刚学习的如何才能自信的拍美美的婚纱照呢(要结婚啦)
查看>>
M51文件注释
查看>>
关于临界资源访问互斥量的死锁问题
查看>>
django-view层
查看>>