-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLA.h
More file actions
232 lines (230 loc) · 5.32 KB
/
Copy pathLA.h
File metadata and controls
232 lines (230 loc) · 5.32 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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<string>
using namespace std;
int row, column;
const string keyword[15] = { "program", "const", "var", "procedure", "begin", "if", "end", "while", "call", "read", "write", "then", "odd", "do" ,"else" };
fstream infile;
fstream outfile;
/*void GetChar()
{
if(GetBC())
{
while(GetBC()) infile>>ch;
}
}*/
/* whether it is blank */
bool GetBC(char ch)
{
if (ch == '\n' || ch == '\r')
{
row++;
column = 1;
return true;
}
else if (ch == '\t')
{
column += 4;
return true;
}
else if (ch == ' ')
{
column++;
return true;
}
else return false;
}
string Concat(string strToken, char ch)
{
return strToken + ch;
}
/* whether it is a letter */
bool IsLetter(char ch)
{
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) return true;
else return false;
}
/* whether it is a number */
bool IsDigit(char ch)
{
if (ch >= '0' && ch <= '9') return true;
else return false;
}
/* whether it is a keyword */
int Reserve(string strToken)
{
for (int i = 0; i < 15; i++)
{
if (strToken.compare(keyword[i]) == 0) return i + 1;
}
return 0;
}
/* back one letter */
void Retract()//回调一个字符位置
{
if (!infile.eof())
{
infile.seekg(-1, ios::cur);
}
}
int LA()
{
infile.open("test2.txt");//open file "test1.txt"
if (!infile)
{
cout << "Can't open test1.txt" << "\n";
exit(0);
}
outfile.open("res1.txt");
if (!outfile)
{
cout << "Can't open res1.txt" << "\n";
exit(1);
}
row = 1;
column = 1;//row and column initialization
string strToken = "";
char ch;
while (!infile.eof())
{
/* 12 cases */
ch = infile.get();
if (GetBC(ch)) strToken = "";//case 1:blank; circle until it is non-blank
else if (IsLetter(ch))//case 2:word;keyword or id
{
strToken = Concat(strToken, ch);
column++;
ch = infile.get();
while (IsLetter(ch) || IsDigit(ch))
{
column++;
strToken = Concat(strToken, ch);
ch = infile.get();
}
if (Reserve(strToken))//关键字
{
outfile << strToken << "\t" << "keyword" << "\t" << row << "\t" << column << endl;
}
else
{
outfile << strToken << "\t" << "id" << "\t" << row << "\t" << column << endl;
}
//cout << strToken << "\n";
strToken = "";
Retract(); //返回上一位
//outfile << ch << "\n";
}
else if (IsDigit(ch))//case 3:number
{
strToken = Concat(strToken, ch);
column++;
ch = infile.get();
while (IsDigit(ch))
{
column++;
strToken = Concat(strToken, ch);
ch = infile.get();
}
if (IsLetter(ch))
{
while (IsLetter(ch) || IsDigit(ch))
{
strToken = Concat(strToken, ch);
column++;
ch = infile.get();
}
cout << "[Lexcial ERROR]"<<strToken << "\t" << "[" << row << "," << column << "]" << "\t" << "Invalid id:\n";
outfile << strToken << "\t" << "id" << "\t" << row << "\t" << column << endl;
}
else outfile << strToken << "\t" << "int" << "\t" << row << "\t" << column << endl;
strToken = "";
Retract();
}
else if (ch == '<')//case 4:<; next is '>' or '=' or ""
{
column++;
ch = infile.get();
if (ch == '>')
{
column++;
outfile << "<>" << "\t" << "lop" << "\t" << row << "\t" << column << endl;
}
else if (ch == '=')
{
column++;
outfile << "<=" << "\t" << "lop" << "\t" << row << "\t" << column << endl;
}
else
{
outfile << "<" << "\t" << "lop" << "\t" << row << "\t" << column << endl;
Retract();
}
}
else if (ch == '>')//case 5:>;next is '=' or ""
{
column++;
ch = infile.get();
if (ch == '=')
{
column++;
outfile << ">=" << "\t" << "lop" << "\t" << row << "\t" << column << endl;
}
else
{
outfile << ">" << "\t" << "lop" << "\t" << row << "\t" << column << endl;
Retract();
}
}
else if (ch == ':')//case 6:next must be '='
{
column++;
ch = infile.get();
if (ch == '=')
{
column++;
outfile << ":=" << "\t" << "..." << "\t" << row << "\t" << column << endl;
}
else
{
cout << "[Lexcial ERROR]" << "[" << row << "," << column << "] " << "Missing \"=\" near the \":\" " << endl;
outfile << ":=" << "\t" << "..." << "\t" << row << "\t" << column << endl;
Retract();
}
}
else if (ch == '=')//case 7:'='
{
column++;
outfile << ch << "\t" << "lop" << "\t" << row << "\t" << column << endl;
}
else if (ch == '+' || ch == '-')//case 8:'+' or '-';
{
column++;
outfile << ch << "\t" << "aop" << "\t" << row << "\t" << column << endl;
}
else if (ch == '*' || ch == '/')//case 9:'*' or '/';
{
column++;
outfile << ch << "\t" << "mop" << "\t" << row << "\t" << column << endl;
}
else if (ch == ';')//case 10:';'
{
column++;
outfile << ch << "\t" << "..." << "\t" << row << "\t" << column << endl;
}
else if (ch == '(' || ch == ')' || ch == ',')//case 11:'(' or ')' or ',';
{
column++;
outfile << ch << "\t" << "..." << "\t" << row << "\t" << column << endl;
}
else//case 12:else
{
column++;
outfile << ch << "\t" << "UNKOWEN" << "\t" << row << "\t" << column << endl;
}
}
infile.close();//close file "test1.txt"
outfile.close();
//cout << "succeefully!" << endl;
return 0;
}