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 的话,它们的强大功能使创建 ...
随机推荐
- 初探swift语言的学习笔记(闭包-匿名函数或block块代码)
使用Block的地方很多,其中传值只是其中的一小部分,下面介绍Block在两个界面之间的传值: 先说一下思想: 首先,创建两个视图控制器,在第一个视图控制器中创建一个UILabel和一个UIButto ...
- poj2987 Firing
以前只是A过很简单的最大闭合权像hdu1565之类,完全的最大流模板题.但是都完全不太懂最大闭合权的定义及其用途. 关于最大流的基础知识,大家可以自己网上搜索关键字.有点基础的哥们妹们,推荐看看胡伯涛 ...
- Performance tool httperf
httperf: A relatively well-known open source utility developed by HP, for Linux operating systems on ...
- java 数组插入元素
import java.util.Arrays; public class AddArray { public static void main(String[] args) { int[] arr ...
- codeforces 455C 并查集
传送门 给n个点, 初始有m条边, q个操作. 每个操作有两种, 1是询问点x所在的连通块内的最长路径, 就是树的直径. 2是将x, y所在的两个连通块连接起来,并且要合并之后的树的直径最小,如果属于 ...
- PyCrpyto windows安装使用方法
PyCrypto - The Python Cryptography Toolkit PyCrypto是一个免费的加密算法库,支持常见的DES.AES加密以及MD5.SHA各种HASH运算. ---- ...
- [C++]Store Credit——Google Code Jam Qualification Round Africa 2010
Google Code Jam Qualification Round Africa 2010 的第一题,很简单. Problem You receive a credit C at a local ...
- Windows Azure 自动伸缩已内置
WindowsAzure平台提供的主要优点之一是能够在有需要时快速缩放云中的应用程序以响应波动.去年7月以前,您必须编写自定义脚本或使用其他工具(如Wasabi或MetricsHub)来启用自动 ...
- UVA 11475 Extend to Palindrome(后缀数组+ST表)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/27647 [题目大意] 给出一个字符串,要求在其后面添加最少的字符数,使得其成为一个回文串.并输出这个回文串 ...
- 有道翻译API
轻奢侈品_百度百科 轻奢侈品 有道翻译API 有道翻译API申请成功 API key:72763558 keyfrom:lexus-studio