创建词法分析相关函数签名
This commit is contained in:
parent
4e8d942ca3
commit
b2c1f28b37
@ -259,10 +259,7 @@ a = point {x:24, y:43};
|
|||||||
| tuple | 21 |
|
| tuple | 21 |
|
||||||
| print | 22 |
|
| print | 22 |
|
||||||
| println | 23 |
|
| println | 23 |
|
||||||
|
|
|
||||||
| |
|
|
||||||
| | |
|
|
||||||
| | |
|
|
||||||
|
|
||||||
#### 界符表
|
#### 界符表
|
||||||
|
|
||||||
|
@ -2,13 +2,52 @@
|
|||||||
#include "stdc++.h"
|
#include "stdc++.h"
|
||||||
#include "Token.h"
|
#include "Token.h"
|
||||||
#include "Tbs.h"
|
#include "Tbs.h"
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
class Scanner {
|
class Scanner {
|
||||||
public:
|
public:
|
||||||
Scanner(std::string source_code)
|
Scanner(std::string source_code, Tbs tables)
|
||||||
: m_source_code(source_code) {}
|
: m_source_code(source_code), m_tables(tables) {}
|
||||||
|
|
||||||
|
|
||||||
|
void scan() {
|
||||||
|
int len = 0;
|
||||||
|
for (int i = 0; i < m_source_code.size(); i++) {
|
||||||
|
if (len = process_const_table(i)) {
|
||||||
|
i += len - 1;
|
||||||
|
len = 0;
|
||||||
|
} else if (len = process_identifier_table(i)) {
|
||||||
|
i += len - 1;
|
||||||
|
len = 0;
|
||||||
|
} else if (len = process_key_table(i)) {
|
||||||
|
i += len - 1;
|
||||||
|
len = 0;
|
||||||
|
} else if (len = process_punct_table(i)) {
|
||||||
|
i += len - 1;
|
||||||
|
len = 0;
|
||||||
|
} else {
|
||||||
|
std::cerr << "Error: Tokenize" << std::endl;
|
||||||
|
exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::vector<Token> get_token_list() {
|
||||||
|
return m_token_list;
|
||||||
|
}
|
||||||
|
|
||||||
|
int process_const_table(int i);
|
||||||
|
int process_identifier_table(int i);
|
||||||
|
int process_key_table(int i);
|
||||||
|
int process_punct_table(int i);
|
||||||
|
|
||||||
|
|
||||||
int is_int(int index);
|
|
||||||
private:
|
private:
|
||||||
std::string m_source_code;
|
std::string m_source_code;
|
||||||
|
std::vector<Token> m_token_list;
|
||||||
|
Tbs m_tables;
|
||||||
int index;
|
int index;
|
||||||
};
|
};
|
@ -1,9 +1,90 @@
|
|||||||
# pragma once
|
# pragma once
|
||||||
|
|
||||||
#include "stdc++.h"
|
#include "stdc++.h"
|
||||||
|
#include <unordered_map>
|
||||||
|
|
||||||
using std::unordered_map,std::string;
|
using std::unordered_map,std::string;
|
||||||
class Tbs {
|
class Tbs {
|
||||||
public:
|
public:
|
||||||
unordered_map<int,string> ConstTable;
|
unordered_map<int,string> ConstTable;
|
||||||
unordered_map<int,string> IdTable;
|
unordered_map<int,string> IdTable;
|
||||||
};
|
std::unordered_map<int, std::string> KeyTable = {
|
||||||
|
{1, "var"},
|
||||||
|
{2, "i8"},
|
||||||
|
{3, "i16"},
|
||||||
|
{4, "i32"},
|
||||||
|
{5, "i64"},
|
||||||
|
{6, "u8"},
|
||||||
|
{7, "u16"},
|
||||||
|
{8, "u32"},
|
||||||
|
{9, "u64"},
|
||||||
|
{10, "float32"},
|
||||||
|
{11, "float64"},
|
||||||
|
{12, "char"},
|
||||||
|
{13, "for"},
|
||||||
|
{14, "if"},
|
||||||
|
{15, "else"},
|
||||||
|
{16, "bool"},
|
||||||
|
{17, "string"},
|
||||||
|
{18, "vector"},
|
||||||
|
{19, "array"},
|
||||||
|
{20, "struct"},
|
||||||
|
{21, "tuple"},
|
||||||
|
{22, "print"},
|
||||||
|
{23, "println"}
|
||||||
|
};
|
||||||
|
|
||||||
|
std::unordered_map<int, std::string> PunctTable = {
|
||||||
|
{1, "-"},
|
||||||
|
{2, "!"},
|
||||||
|
{3, "~"},
|
||||||
|
{4, "/"},
|
||||||
|
{5, "*"},
|
||||||
|
{6, "%"},
|
||||||
|
{7, "+"},
|
||||||
|
{8, "-"},
|
||||||
|
{9, "<<"},
|
||||||
|
{10, ">>"},
|
||||||
|
{11, ">"},
|
||||||
|
{12, ">="},
|
||||||
|
{13, "<"},
|
||||||
|
{14, "<="},
|
||||||
|
{15, "=="},
|
||||||
|
{16, "!="},
|
||||||
|
{17, "&"},
|
||||||
|
{18, "^"},
|
||||||
|
{19, "|"},
|
||||||
|
{20, "&&"},
|
||||||
|
{21, "||"},
|
||||||
|
{22, "="},
|
||||||
|
{23, "%="},
|
||||||
|
{24, "*="},
|
||||||
|
{25, "/="},
|
||||||
|
{26, "+="},
|
||||||
|
{27, "-="},
|
||||||
|
{28, "|="},
|
||||||
|
{29, "&="},
|
||||||
|
{30, "^="},
|
||||||
|
{31, "<<="},
|
||||||
|
{32, ">>="},
|
||||||
|
{33, "("},
|
||||||
|
{34, ")"},
|
||||||
|
{35, "<"},
|
||||||
|
{36, ">"},
|
||||||
|
{37, ","},
|
||||||
|
{38, "."},
|
||||||
|
{39, "["},
|
||||||
|
{40, "]"},
|
||||||
|
{41, "?"},
|
||||||
|
{42, ":"},
|
||||||
|
{43, "->"}
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
enum Table_Type {
|
||||||
|
CONST_TABLE,
|
||||||
|
ID_TABLE,
|
||||||
|
KEY_TABLE,
|
||||||
|
PUNCT_TABLE
|
||||||
|
};
|
||||||
|
|
||||||
|
@ -1,7 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "stdc++.h"
|
#include "stdc++.h"
|
||||||
|
#include "Tbs.h"
|
||||||
|
|
||||||
struct Token{
|
struct Token{
|
||||||
int id,type;
|
int id;
|
||||||
Token (int id,int type):id(id),type(type){}
|
Table_Type type;
|
||||||
};
|
};
|
@ -1,6 +1,17 @@
|
|||||||
#include "Scanner.h"
|
#include "Scanner.h"
|
||||||
|
|
||||||
using std::vector,std::string;
|
|
||||||
|
|
||||||
int Scanner::is_int(int index) {
|
int Scanner::process_const_table(int index) {
|
||||||
|
}
|
||||||
|
|
||||||
|
int Scanner::process_identifier_table(int index) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int Scanner::process_key_table(int index) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int Scanner::process_punct_table(int index) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,16 +1,16 @@
|
|||||||
|
#include "Token.h"
|
||||||
#include "doctest.h"
|
#include "doctest.h"
|
||||||
#include "stdc++.h"
|
#include "stdc++.h"
|
||||||
#include "Scanner.h"
|
#include "Scanner.h"
|
||||||
#include "Tbs.h"
|
#include "Tbs.h"
|
||||||
|
#include <vector>
|
||||||
using std::string,std::vector;
|
using std::string,std::vector;
|
||||||
|
|
||||||
TEST_CASE("Scanner_Test") {
|
TEST_CASE("Scanner test const table") {
|
||||||
Scanner scanner;
|
Tbs tables;
|
||||||
Tbs tb;
|
std::string src = "1.23";
|
||||||
scanner.scan("to be or not to be is a problem.",tb);
|
Scanner scan(src, tables);
|
||||||
unordered_map<int,string> test;
|
scan.scan();
|
||||||
test[1] = "to",test[2] = "be",test[3] = "or",test[4] = "not",test[5] = "to",test[6] ="be",test[7] = "is",test[8] = "a";
|
|
||||||
|
|
||||||
CHECK(tb.ConstTable == test);
|
CHECK(scan.get_token_list() == std::vector<Token>({Token{1, CONST_TABLE}}));
|
||||||
CHECK(tb.IdTable == test);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user