-
Notifications
You must be signed in to change notification settings - Fork 1
/
globalData.cpp
74 lines (67 loc) · 3.51 KB
/
globalData.cpp
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "type.h"
#include "globalData.h"
const string tokenTypeStr[40] = {"Ident", "IntConst", "PRINT_STRCON", "Main", "ConstToken", "Int", "Break", "Continue", "If", "Else",
"Not", "And", "Or", "While", "GetInt", "PutInt", "Return", "Add", "Minus",
"Mul", "Div", "Mod", "Lt", "Leq", "Gt", "Geq", "Eq", "Neq",
"Assign", "Semicolon", "Comma", "LParen", "RParen", "LBracket", "RBracket", "LBrace", "RBrace", "Void"};
// reserved words' map
const map<string, TokenType> reservedMap = {
{"main", Main},
{"const", ConstToken},
{"int", Int},
{"void", Void},
{"break", Break},
{"continue", Continue},
{"if", If},
{"else", Else},
{"while", While},
// {"getint", GetInt},
// {"putint", PutInt},
{"return", Return}
};
// separatorMap
const map<string, TokenType> separatorMap = {{"!", Not},
{"&&", And},
{"||", Or},
{"+", Add},
{"-", Minus},
{"*", Mul},
{"/", Div},
{"%", Mod},
{"<", Lt},
{"<=", Leq},
{">", Gt},
{">=", Geq},
{"==", Eq},
{"!=", Neq},
{"=", Assign},
{";", Semicolon},
{",", Comma},
{"(", LParen},
{")", RParen},
{"[", LBracket},
{"]", RBracket},
{"{", LBrace},
{"}", RBrace}
};
const string syntaxTypeStr[SyntaxTypeNum] = {
"CompUnit", "ConstDecl", "BType", "ConstDef", "VarDecl", "VarDef", "FuncDef", "MainFuncDef", "FuncType",
"FuncFParams", "FuncFParam", "Block", "Exp", "Cond", "LVal", "Number", "UnaryOp", "FuncRParams",
"MulExp", "AddExp", "RelExp", "EqExp", "LAndExp", "LOrExp", "ConstExp",
"Decl", "ConstInitVal", "InitVal", "BlockItem", "Stmt", "PrimaryExp", "UnaryExp"
};
const map<SyntaxType, set<int>> firstSetMap = {
{ConstDecl, { ConstToken}},
{VarDecl, { Int}},
{ConstExp, { LParen, Ident, IntConst, Add, Minus, Not}},
{Exp, { LParen, Ident, IntConst, Add, Minus, Not}}, // aka First(PriExp) + First(UnaryOp) + {Ident}
{Decl, { ConstToken, Int}},
{Stmt, { Ident, LParen, Add, Minus, Not, IntConst, Semicolon, If, While, Break, Return, LBrace}},
{LVal, { Ident}},
{Block, { LBrace}},
{Number, { IntConst}},
{PrimaryExp, { LParen, Ident, IntConst}},
{UnaryOp, { Add, Minus, Not}},
{FuncRParams, {LParen, Ident, IntConst, Add, Minus, Not}}, // aka First(Exp)
{FuncFParams, {Int}}, // aka First(BType)
};