📄 scaner.h
字号:
#ifndef SCANER_H
#define SCANER_H
#include "macro.h"
#include "Pair.h"
using namespace std;
deque<Pair *> pairs;
class KeyWord{
public:
string word;
int id;
KeyWord(string word = "", int id = 0){
this->word = word;
this->id = id;
}
};
class Scaner{
private:
KeyWord keyword[6];
string token;
ifstream ∈
int lineNo ;
public:
Scaner(ifstream &in): in(in){
keyword[0] = KeyWord("else", 0);
keyword[1] = KeyWord("if", 1);
keyword[2] = KeyWord("int", 2);
keyword[3] = KeyWord("return", 3);
keyword[4] = KeyWord("void", 4);
keyword[5] = KeyWord("while", 5);
token = "";
lineNo = 1;
}
int lookup(string word) {
for(int i=0;i<6;i++) {
if(word == keyword[i].word)
return i;
}
return -1;
}
void outPut(int id ,string word) {
cout << "(" << ((id<10)?" ":"") << id << "," << word << ")" << endl;
if( B11 == id || B12 == id) {// when it is a quote
pairs.push_back(new Pair(id));
}
else if (id == INT){
pairs.push_back(new Pair(id, atoi(word.c_str())));//value type
}
else if (id == ADD || id == MINUS || MULT == id) {
pairs.push_back(new Pair(id));
}
else {
cout << "Illegal char!" << endl;
}
}
void display_char(char ERR_CH) {
cout<< "undeclared identifler "<< ERR_CH << " at " << lineNo << " line!" << endl;
}
bool isalpha(char c) {
if( (c>='a'&&c<='z') || (c>='A'&&c<='Z') )
return true;
else
return false;
}
bool isdigit(char c) {
if(c>='0'&&c<='9')
return true;
else
return false;
}
void scan() {
char ch;
int c;
while(!in.eof()) {
in.get(ch);
if(in.eof())
break;
if(isalpha(ch))/*当以字母打头时*/ {
token = "";
token += ch;
in.get(ch);
while(isalpha(ch)) {
token += ch;
in.get(ch);
if (in.eof()) {
break;
}
}
in.putback(ch);
c=lookup(token);
if(c==-1) //当不是关键字
outPut(ID,token);
else//是关键字
outPut(c,keyword[c].word);
}
else if(isdigit(ch))/*当以数字打头时*/{
token = "";
token += ch;
in.get(ch);
while(isdigit(ch))/*取得其后所有数字*/{
token += ch;
in.get(ch);
if (in.eof()) {
break;
}
}
in.putback(ch);
outPut(INT,token);
}
else//第一个字不是数字,不是ID
switch(ch)
{
case '<':
in.get(ch);
if(ch=='=')
outPut(LE,"<=");
else {
in.putback(ch);
outPut(L,"<");
}
break;
case '=':
in.get(ch);
if(ch=='=')
outPut(EQ, "==");
else {
in.putback(ch);
outPut(ASS, "=");
}
break;
case '>':
in.get(ch);
if(ch=='=')
outPut(ME,">=");
else {
in.putback(ch);
outPut(M,">");
}
break;
case '!':
in.get(ch);
if(ch=='=')
outPut(NE,"!=");
else {
in.putback(ch);
display_char('!');
}
break;
case '+':
outPut(ADD, "+");
break;
case '-':
outPut(MINUS, "-");
break;
case '*':
outPut(MULT, "*");
break;
case ';':
outPut(SECO, ";");
break;
case ',':
outPut(COMA, ",");
break;
case '/':
in.get(ch); //删除程序中的注释
if(ch=='*') {//注释开始
A: do
{
in.get(ch);
if (in.eof()) {
break;
}
if('\n' == ch)
lineNo++;
}while(ch !='*');
//遇到*
in.get(ch);
while ('*' == ch) {
in.get(ch);
if (in.eof()) {
break;
}
}
if('/' != ch)//取完所有的*后如果紧接着的不是/
goto A;
//如果是/,则注释结束
}
else {//仅是除号
in.putback(ch);
outPut(DIV,"/");
}
break;
case '(':
outPut(B11,"(");
break;
case ')':
outPut(B12,")");
break;
case '[':
outPut(B21,"[");
break;
case ']':
outPut(B22,"]");
break;
case '{':
outPut(B31,"{");
break;
case '}':
outPut(B32,"}");
break;
case ' ' :
break;
//删除程序中的空格
case '\n':
lineNo++;
break; //删除程序中的回车,并记录程序编译到第几行
case ' ':
break; //删除程序中的横向制表符
case -1 :
break; //删除文件尾符号
default : display_char(ch);
break;
}
}
return;
}
~Scaner(){
in.close();
}
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -