Repository navigation
Expand file tree
/
Copy pathMyParser.cpp
More file actions
173 lines (145 loc) · 3.33 KB
/
Copy pathMyParser.cpp
File metadata and controls
173 lines (145 loc) · 3.33 KB
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using namespace std;
#include <iostream>
#include <fstream>
#include <string.h>
#include <vector>
#include <limits.h>
#include "rmlsyn.tab.hh"
#include "MyParser.h"
#include "FlexLexer.h"
#include "MyFlexLexer.h"
MyParser::MyParser(int pOptimization)
{
optimization=pOptimization;
}
MyParser::~MyParser()
{
if (lexer!=nullptr)
delete lexer;
if (base!=nullptr)
delete base;
int cnt=stats.size();
for (int i=0;i<cnt;i++)
delete stats[i];
}
int MyParser::lex(yy::MyParserBase::semantic_type *const lval)
{
this->lval=lval;
return lexer->lex(lval);
}
void MyParser::compile(yy::MyParserBase *pBase, ifstream *is, RMLEval *pRmlEval)
{
base=pBase;
rmlEval=pRmlEval;
lexer=new MyFlexLexer(this);
lexer->switch_streams(is);
base->parse();
#if defined(PROJECT03) || defined(PROJECT04)
rmlEval->scanCheckExtractSymbols();
rmlEval->scanCalculateTypes();
rmlEval->messageSet.sort();
#if defined(PROJECT04)
if (rmlEval->messageSet.messageCount(RMLEvalMsgSeverity::Error)==0)
{
rmlEval->scanConstantFolding();
if (rmlEval->messageSet.messageCount(RMLEvalMsgSeverity::Error)==0)
{
rmlEval->scanForIC();
if (optimization & (OPTIMIZE_PH_TRIVIALJUMP))
rmlEval->peepholeIC();
}
}
#endif
#endif
rmlEval->messageSet.appendMessage(INT_MAX, "Completed compilation.", RMLEvalMsgSeverity::Info);
}
int MyParser::getId()
{
lval->STR=new string(lexer->YYText());
return yy::MyParserBase::token::ID;
}
int MyParser::hexDigit(char c)
{
if (c>='0' && c<='9')
return c-'0';
if (c>='A' && c<='F')
return c-'A'+10;
if (c>='a' && c<='f')
return c-'a'+10;
return -1;
}
string *MyParser::makeString(const char *rawStr)
{
char *copyStr=strdup(rawStr),
*p,
*q;
int d;
unsigned char c;
q=copyStr;
for (p=q+1;*p!='"';p++,q++)
{
switch (*p)
{
case '\\':
p++;
switch (*p)
{
case '\\':
*q='\\';
break;
case 't':
*q='\t';
break;
case 'r':
*q='\r';
break;
case 'n':
*q='\n';
break;
case '"':
*q='"';
break;
case 'x':
p++;
c=(unsigned char)hexDigit(*p);
d=hexDigit(p[1]);
if (d>-1)
{
p++;
c=(c<<4) | (unsigned char)d;
}
*q=c;
}
break;
default:
*q=*p;
}
}
*q=0;
string *retVal=new string(copyStr);
free(copyStr);
return retVal;
}
int MyParser::getStr()
{
lval->STR=makeString(lexer->YYText());
return yy::MyParserBase::token::STR;
}
int MyParser::getNumber()
{
lval->NUM=atof(lexer->YYText());
return yy::MyParserBase::token::NUM;
}
void MyParser::setParseErrorLine()
{
parseErrorLine=lexer->lineno();
rmlEval->messageSet.appendMessage(parseErrorLine, "Syntax error", RMLEvalMsgSeverity::Error);
}
int MyParser::getParseErrorLine()
{
return parseErrorLine;
}
void yy::MyParserBase::error(const std::string &msg)
{
driver->setParseErrorLine();
}