forked from 2022-cs4301/compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stage0.h
161 lines (133 loc) · 3.6 KB
/
stage0.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
152
153
154
155
156
157
158
159
160
161
#ifndef STAGE0_H
#define STAGE0_H
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
const char END_OF_FILE = '$'; // arbitrary choice
enum storeTypes {INTEGER, BOOLEAN, PROG_NAME};
enum modes {VARIABLE, CONSTANT};
enum allocation {YES, NO};
class SymbolTableEntry
{
public:
SymbolTableEntry(string in, storeTypes st, modes m,
string v, allocation a, int u)
{
setInternalName(in);
setDataType(st);
setMode(m);
setValue(v);
setAlloc(a);
setUnits(u);
}
string getInternalName() const
{
return internalName;
}
storeTypes getDataType() const
{
return dataType;
}
modes getMode() const
{
return mode;
}
string getValue() const
{
return value;
}
allocation getAlloc() const
{
return alloc;
}
int getUnits() const
{
return units;
}
void setInternalName(string s)
{
internalName = s;
}
void setDataType(storeTypes st)
{
dataType = st;
}
void setMode(modes m)
{
mode = m;
}
void setValue(string s)
{
value = s;
}
void setAlloc(allocation a)
{
alloc = a;
}
void setUnits(int i)
{
units = i;
}
private:
string internalName;
storeTypes dataType;
modes mode;
string value;
allocation alloc;
int units;
};
class Compiler
{
public:
Compiler(char **argv); // constructor
~Compiler(); // destructor
void createListingHeader();
void parser();
void createListingTrailer();
// Methods implementing the grammar productions
void prog(); // stage 0, production 1
void progStmt(); // stage 0, production 2
void consts(); // stage 0, production 3
void vars(); // stage 0, production 4
void beginEndStmt(); // stage 0, production 5
void constStmts(); // stage 0, production 6
void varStmts(); // stage 0, production 7
string ids(); // stage 0, production 8
// Helper functions for the Pascallite lexicon
bool isKeyword(string s) const; // determines if s is a keyword
bool isSpecialSymbol(char c) const; // determines if c is a special symbol
bool isNonKeyId(string s) const; // determines if s is a non_key_id
bool isInteger(string s) const; // determines if s is an integer
bool isBoolean(string s) const; // determines if s is a boolean
bool isLiteral(string s) const; // determines if s is a literal
// Action routines
void insert(string externalName, storeTypes inType, modes inMode,
string inValue, allocation inAlloc, int inUnits);
storeTypes whichType(string name); // tells which data type a name has
string whichValue(string name); // tells which value a name has
void code(string op, string operand1 = "", string operand2 = "");
// Emit Functions
void emit(string label = "", string instruction = "", string operands = "",
string comment = "");
void emitPrologue(string progName, string = "");
void emitEpilogue(string = "", string = "");
void emitStorage();
// Lexical routines
char nextChar(); // returns the next character or END_OF_FILE marker
string nextToken(); // returns the next token or END_OF_FILE marker
// Other routines
string genInternalName(storeTypes stype) const;
void processError(string err);
private:
map<string, SymbolTableEntry> symbolTable;
ifstream sourceFile;
ofstream listingFile;
ofstream objectFile;
string token; // the next token
char ch; // the next character of the source file
uint errorCount = 0; // total number of errors encountered
uint lineNo = 0; // line numbers for the listing
};
#endif