-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLRParser.h
151 lines (120 loc) · 3.86 KB
/
LRParser.h
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// Created by HashEngineering on 4/30/2021.
//
#ifndef _323_LRPARSER_H
#define _323_LRPARSER_H
#include <stack>
#include <map>
#include "syntaxanalyzer.h"
#include "record.h"
#define RULE(x) {x, "<" #x ">"}
using namespace::std;
struct LREntry {
enum Operation {
Shift,
Reduce,
Accept,
Error,
Goto,
Token,
Production,
EndOfFile
};
Operation operation;
int value;
string production;
Record token;
LREntry(Operation operation) : operation(operation), token("", "", true, "", 0, 0, "") {
}
LREntry(Operation operation, int value) : operation(operation), value(value), token("", "", true, "", 0, 0, "") {
}
LREntry(const Record & token) : operation(Token), value (-1), token(token) {
}
LREntry(const string & production, int number) : operation(Production), production(production), value(number), token("", "", true, "", 0, 0, "") {
}
bool operator==(const LREntry & other) const {
return operation == other.operation && value == other.value && production == other.production;
}
bool operator!=(const LREntry & other) const {
return !operator==(other);
}
bool operator<(const LREntry & other) const {
return production.compare(other.production) < 0;
}
};
struct ReduceEntry : public LREntry {
explicit ReduceEntry(int rule) : LREntry(Reduce, rule) {}
};
struct ShiftEntry : public LREntry {
explicit ShiftEntry(int state) : LREntry(Shift, state) {}
};
struct GotoEntry : public LREntry {
explicit GotoEntry(int state) : LREntry(Goto, state) {}
};
class LRParser : public SyntaxAnalyzer {
enum Productions {
Expression = 6,
Term,
Factor
};
enum Terminals {
Identifier = 0,
Addition = 1,
Subtraction = 2,
Multiply = 3,
Divide = 4,
LeftParen = 5,
RightParen = 6,
EndOfFile = 7,
Unknown = 8
};
map<string, int> productionNames = {
{ "E", Expression },
{ "T", Term },
{ "F", Factor}
};
list<LREntry> productionStack;
stack<Node *> parseTreeStack;
static LREntry table[16][11];
static list<pair<LREntry, string>> productions;
public:
LRParser(LexicalScanner & lexicalScanner, SymbolTable & symbolTable, ErrorHandler & errorHandler);
ParseTree * createParseTree();
bool stackProcess();
int columnFromToken(Record & token);
int columnFromRecord(LREntry & record);
int rowFromRecord(LREntry & record);
//int isTerminal(LREntry & entry) {
// return record.token.lexeme != "";
//}
string to_string(LREntry item) {
switch (item.operation) {
case LREntry::Token:
return item.token.lexeme;
case LREntry::Error:
return "Error";
case LREntry::Shift:
return string("S") + ::to_string(item.value);
case LREntry::Reduce:
return string("R") + ::to_string(item.value);
case LREntry::Goto:
return ::to_string(item.value);
case LREntry::Accept:
return "Accept";
case LREntry::EndOfFile:
return "$";
case LREntry::Production:
return item.production;
default:
return "Unknown";
}
}
LREntry getTopMostTerminal();
int getProductionTerminalCount(int rule);
LREntry getProduction(int rule);
string getProductionRHS(int rule);
string getStackAsString();
void printIntermediateCode(ostream & stream);
void printIntermediateCode(ostream & stream, Node * node);
};
#endif //_323_OPERATORPRECEDENCEPARSER_H