symboltable.cpp
来自「compiler principle how to ananilyze」· C++ 代码 · 共 136 行
CPP
136 行
// SymbolTable.cpp: implementation of the SymbolTable class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SymbolTable.h"
#include <string>
using namespace std;
SymbolTable symbol_table;
SymbolTable::SymbolTable() {
// token_type token_kind lexeme
// -------------------------------------------------------------------------------------------
// VN V: Non-terminal symbol [A-Za-z][A-Za-z_0-9]*
// VT V: terminal symbol \'Escape\' | 'epsilon'
// Escape -> NoEscapeChar Escape | '\\\\' Escape | '\\\'' Escape | epsilon;
// NoEscapeChar -> [^\'];
// GN GT(GrammarTitle): GrammarName [A-Za-z][A-Za-z_0-9]*
// ALTER KEYWORD: meta-symbol |
// ARROW KEYWORD: meta-symbol ->
// DELIMITER KEYWORD: meta-symbol ;
// EPSILON KEYWORD: meta-symbol epsilon
// LEFT_BRACKET KEYWORD: meta-symbol [
// RIGHT_BRACKET KEYWORD: meta-symbol ]
//--------------------------------------------------------------------------------------------
table.push_back(SymbolEntry(SymbolEntry::p_ALTER, SymbolEntry::ALTER));
table.push_back(SymbolEntry(SymbolEntry::p_ARROW, SymbolEntry::ARROW));
table.push_back(SymbolEntry(SymbolEntry::p_DELIMITER, SymbolEntry::DELIMITER));
table.push_back(SymbolEntry(SymbolEntry::p_EPSILON, SymbolEntry::EPSILON));
table.push_back(SymbolEntry(SymbolEntry::p_LEFT_BRACKET, SymbolEntry::LEFT_BRACKET));
table.push_back(SymbolEntry(SymbolEntry::p_RIGHT_BRACKET, SymbolEntry::RIGHT_BRACKET));
}
int SymbolTable::find(const string& alexeme) const {
int i=0;
int size = table.size();
for(; i< size && (table[i]).lexeme.compare(alexeme) != 0; ++i) {}
return i< size ? i: -1;
}
int SymbolTable::find(const string& alexeme, int token_type) const {
VectorTable::const_iterator i = table.begin();
VectorTable::const_iterator e = table.end();
for(; i != e && ((*i).token_type != token_type ||
(*i).lexeme.compare(alexeme) != 0);
++i) {}
return i != e ? i-table.begin(): -1;
}
int SymbolTable::findByKind(const string& alexeme, int token_kind) const {
VectorTable::const_iterator i = table.begin();
VectorTable::const_iterator e = table.end();
for(; i != e && ((*i).token_kind != token_kind ||
(*i).lexeme.compare(alexeme) != 0);
++i) {}
return i != e ? i-table.begin(): -1;
}
int SymbolTable::findInKeywords(const string& alexeme) const {
return findByKind(alexeme, SymbolEntry::KEYWORD);
}
int SymbolTable::findInVs(const string& alexeme) const {
return findByKind(alexeme, SymbolEntry::V);
}
int SymbolTable::append(const SymbolEntry& se) {
table.push_back(se);
return table.size() -1; //return index of the last SymbolEntry
}
int SymbolTable::insert(const string& alexeme, int token_type) {
int index = find(alexeme, token_type);
if (index != -1) return index;
return append(SymbolEntry(alexeme, token_type));
}
Token SymbolTable::getToken(int index) const {
return Token((table[index]).token_type, index);
}
const string& SymbolTable::getLexeme(int index) const {
return table[index].lexeme;
}
const SymbolEntry& SymbolTable::operator[](int index) const {
return table[index];
}
SymbolEntry& SymbolTable::operator[](int index) {
return table[index];
}
void SymbolTable::setTokenType(int index, int token_type) {
SymbolEntry& se = table[index];
se.setTokenType(token_type);
}
string SymbolTable::toStr() const {
string str = "----------------------- symbol_table -----------------------\n";
str += "lexeme\ttoken_type: token_kind\tlocations\n";
VectorTable::const_iterator i = table.begin();
VectorTable::const_iterator e = table.end();
for(; i != e; ++i) {
str += (*i).toStr();
str += "\n";
}
str += "----------------------- symbol_table -----------------------\n";
return str;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?