📄 lexer.cpp
字号:
// Lexer.cpp: implementation of the Lexer class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SymbolTable.h"
#include "Lexer.h"
#include <iostream>
using namespace std;
extern SymbolTable symbol_table;
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
Lexer::Lexer(): line_no(1)
{ }
Token Lexer::getNextToken() {
char c;
int digit;
while (1) {
cin >> c;
if (c == ' ' || c == '\t')
; // omit white space
else if (c == '\n')
line_no += 1; // increase number of lines
else if (isdigit(c)) { // number
cin.putback(c);
cin >> digit;
return Token(Token::NUM, digit);
}
else if (isalpha(c)) { // identifier or keywords(reserved words)
int p = 0;
for(; isalnum(c) && p < BSIZE ; ++p, cin >> c) {
lexbuf[p]= c;
}
if (!isalnum(c)) {
cin.putback(c);
string alexeme(lexbuf, lexbuf+p);
int index = symbol_table.find(alexeme);
if (index == -1) {
SymbolEntry se(alexeme, Token::ID); // identifier(ID)
index = symbol_table.append(se);
}
//else: keywords(DIV,MOD)
return symbol_table.getToken(index);
}
else {
error("Lexer::getNextToken..lexbuf overflow..");
}
}
else if (c == EOF) {
return Token(Token::DONE, Token::NONE);
}
else {
return Token(c, Token::NONE); // *,/
}
}//while
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -