📄 scanner.h
字号:
#ifndef MY_SCANNER_H_
#define MY_SCANNER_H_
#include "Tokenizer.h"
/**: scanner.h header file
&
* 继承自 class Tokenizer, 从源文件中提取出单个的 token;
* author: lonelyforest;
* data: 2006.03.16
*/
enum tokenType {
// reserved Keyword
k_INT = 0/* int */, k_ELSE/* else */, k_RETURN/* return */,
k_VOID/* void */, k_IF/* if */,k_WHILE/* while */,
k_READ/* read */, k_WRITE/* write */,
// operations
ASSIGN/* = */, PLUS/* + */, MINUS/* - */, TIMES/* * */, DIV/* / */,
MOD/* % */, LT/* < */, GT/* > */,
// interpunctions
LPARAN, RPARAN/* ( ) */, LBRACE, RBRACE/* { } */, LSQUARE, RSQUARE/* [ ] */,
COMMA/* , */, SEMI/* ; */,
// complex operations
EQ/* == */, NEQ/* != */,NGT/* <= */, NLT/* >= */,
// others
k_EOF, k_ID, k_NUM, k_ERROR, k_NONE
};
class Token{
public:
Token &operator=(const Token& rh);
tokenType type;
string str;
};
/**: class Scanner
※
* 继承Tokenizer, 从源文件提取出单个的 Token
* 忽略空白,识别关键字, 数字等等, 并且生成list文件。
*
* 重要接口 Token& nextToken() & void push()
* 分别从源文件中提取一个 Token 标识符, 退回一个标识符
*
* author: lonelyforest;
* data: 2006.03.16
*/
//-----------------------------------------------------------------------------
class Scanner: public Tokenizer {
public:
Scanner(const string& filename);
virtual ~Scanner();
//-------------------------------------------------------------------------
void build_key_map(); // build the keyword map;
Token& nextToken(); // return next token !! primary interface
void push(); // push back current token, next return it
//-------------------------------------------------------------------------
int errCount() const { return err_count; }
int warnCount()const { return warn_count; }
void add_warn() { ++warn_count; }
void add_err() { ++err_count;}
//-------------------------------------------------------------------------
void noListFile() { TraceSource =false; }
bool getListFile(); // create log file sourcename.log
bool is_good() { return (err_count <=0 && warn_count <=0) && Tokenizer::is_good(); }
//-------------------------------------------------------------------------
// if in key_word, return tokenType value else return k_NONE;
tokenType reservedLookup(const string& word);
std::vector<Token> key_word;
protected:
//-------------------------------------------------------------------------
Token m_token; // store current token
bool m_pushed; // push back curren token flag;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -