-
Notifications
You must be signed in to change notification settings - Fork 1
/
SyntacticAnalyzer.class.h
101 lines (98 loc) · 3.06 KB
/
SyntacticAnalyzer.class.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
#ifndef __SYNTACTICANALYZER_CLASS_H
#define __SYNTACTICANALYZER_CLASS_H
#include"LexicalAnalyzer.class.h"
#include"keywordlist.h"
#include<string>
#include<fstream>
#include<deque>
#include<vector>
#include<set>
class SyntacticAnalyzer
{
private:
enum IdentStackFlag{CONST,VAR,BOUND};
enum SyntacticAnalyzerResult {ERROR=false,RIGHT=true,VALUE=2};
enum QUERYWAY{QUERY_ALL_PROC=true,QUERY_CUR_PROC=false};
class IdentStackNode
{
public:
IdentStackFlag flag;
std::string ident;
int value;
public:
IdentStackNode(const IdentStackFlag _flag,const std::string& _ident="",const int _value=0):flag(_flag),ident(_ident),value(_value){}
};
class TempVar
{
public:
std::string prev_temp_varname;
private:
int seqnum;
public:
TempVar():seqnum(1){}
std::string next();
static bool isTempVar(const std::string& varname);
};
class TempPoint
{
private:
int seqnum;
public:
TempPoint():seqnum(1){}
std::string next();
};
class IntermediateCodeNode
{
public:
std::string command;
std::string oper1,oper2,oper3;
public:
IntermediateCodeNode(const std::string& _command="",const std::string& _oper1="",const std::string& _oper2="",const std::string& _oper3="")
:command(_command),oper1(_oper1),oper2(_oper2),oper3(_oper3){}
};
private:
std::string errorinfo;
std::deque<IdentStackNode> ident_stack;
std::vector<std::string> procedure_vector;
std::vector<std::deque<IntermediateCodeNode> > intermediate_code_deque_vector;
TempVar tempvar;
TempPoint temppoint;
std::string cur_procedure_name;
int cur_procedure_index;
public:
SyntacticAnalyzerResult analyze(LexicalAnalyzer&);
private:
SyntacticAnalyzerResult subprogram(LexicalAnalyzer&);
SyntacticAnalyzerResult constsym(LexicalAnalyzer&);
SyntacticAnalyzerResult varsym(LexicalAnalyzer&);
SyntacticAnalyzerResult procsym(LexicalAnalyzer&);
SyntacticAnalyzerResult statement(LexicalAnalyzer&);
SyntacticAnalyzerResult callsym(LexicalAnalyzer&);
SyntacticAnalyzerResult beginsym(LexicalAnalyzer&);
SyntacticAnalyzerResult ifsym(LexicalAnalyzer&);
SyntacticAnalyzerResult whilesym(LexicalAnalyzer&);
SyntacticAnalyzerResult readsym(LexicalAnalyzer&);
SyntacticAnalyzerResult writesym(LexicalAnalyzer&);
SyntacticAnalyzerResult ident(LexicalAnalyzer&);
SyntacticAnalyzerResult condition(LexicalAnalyzer&,const std::string&,bool&);
SyntacticAnalyzerResult expression(LexicalAnalyzer&,int&);
SyntacticAnalyzerResult item(LexicalAnalyzer&,int&);
SyntacticAnalyzerResult factor(LexicalAnalyzer&,int&);
private:
std::deque<IdentStackNode>::const_iterator ident_stack_find(const std::string&,const QUERYWAY) const;
bool procedure_find(const std::string&) const;
void ident_stack_clear();
void declare_new_var(const std::string&);
public:
SyntacticAnalyzer():cur_procedure_name("[START]"),cur_procedure_index(0)
{
intermediate_code_deque_vector.push_back(std::deque<IntermediateCodeNode>());
procedure_vector.push_back("start");
}
std::string get_error_info() const
{
return this->errorinfo;
}
void print_intermediate_code(std::ostream& out) const;
};
#endif