-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.cc
198 lines (174 loc) · 4.89 KB
/
lexer.cc
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
/*
* Copyright (C) Rida Bazzi, 2017
*
* Do not share this file with anyone
*/
#include <iostream>
#include <istream>
#include <vector>
#include <string>
#include <cctype>
#include "lexer.h"
#include "inputbuf.h"
using namespace std;
// Lexer modified for FIRST & FOLLOW project
string reserved[] = { "END_OF_FILE", "ARROW", "STAR", "HASH", "ID", "ERROR" };
void Token::Print()
{
cout << "{" << this->lexeme << " , "
<< reserved[(int) this->token_type] << " , "
<< this->line_no << "}\n";
}
LexicalAnalyzer::LexicalAnalyzer()
{
this->line_no = 1;
tmp.lexeme = "";
tmp.line_no = 1;
tmp.token_type = ERROR;
Token token = GetTokenMain();
index = 0;
while (token.token_type != END_OF_FILE)
{
tokenList.push_back(token); // push token into internal list
token = GetTokenMain(); // and get next token from standatd input
}
// pushes END_OF_FILE is not pushed on the token list
}
bool LexicalAnalyzer::SkipSpace()
{
char c;
bool space_encountered = false;
input.GetChar(c);
line_no += (c == '\n');
while (!input.EndOfInput() && isspace(c)) {
space_encountered = true;
input.GetChar(c);
line_no += (c == '\n');
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
return space_encountered;
}
Token LexicalAnalyzer::ScanId()
{
char c;
input.GetChar(c);
if (isalpha(c)) {
tmp.lexeme = "";
while (!input.EndOfInput() && isalnum(c)) {
tmp.lexeme += c;
input.GetChar(c);
}
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.line_no = line_no;
tmp.token_type = ID;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.lexeme = "";
tmp.token_type = ERROR;
}
return tmp;
}
// GetToken() accesses tokens from the tokenList that is populated when a
// lexer object is instantiated
Token LexicalAnalyzer::GetToken()
{
Token token;
if (index == tokenList.size()){ // return end of file if
token.lexeme = ""; // index is too large
token.line_no = line_no;
token.token_type = END_OF_FILE;
}
else{
token = tokenList[index];
index = index + 1;
}
return token;
}
// UngetToken() resets the index back by a amount equal to its argument
// "howMany". "howMany" should be positive and not larger than the
// actual number of valid tokens that were obtained using GetToken()
//
// NOTE 1: UngetToken() unget actual tokens. So, if you call GetToken() twice
// and for both call you get END_OF_FILE UngetToken(2) will return the last
// two actual tokens (not END_OF_FILE). This might make the use of UngetToken()
// awkward and potentially error-prone (see NOTE 2)
//
// NOTE 2: UngetToken() will not be needed if you use GetToken() and peek()
// judiciously
void LexicalAnalyzer::UngetToken(int howMany)
{
if (howMany <= 0)
{
cout << "LexicalAnalyzer:UngetToken:Error: non positive argument\n";
exit(-1);
}
index = index - howMany; // update index
if (index < 0) // and panic if resulting index is negative
{
cout << "LexicalAnalyzer:UngetToken:Error: large argument\n";
exit(-1);
}
}
// peek requires that the argument "howFar" be positive.
Token LexicalAnalyzer::peek(int howFar)
{
if (howFar <= 0) { // peeking backward or in place is not allowed
cout << "LexicalAnalyzer:peek:Error: non positive argument\n";
exit(-1);
}
int peekIndex = index + howFar - 1;
if (peekIndex > (tokenList.size()-1)) { // if peeking too far
Token token; // return END_OF_FILE
token.lexeme = "";
token.line_no = line_no;
token.token_type = END_OF_FILE;
return token;
} else
return tokenList[peekIndex];
}
Token LexicalAnalyzer::GetTokenMain()
{
char c;
SkipSpace();
tmp.lexeme = "";
tmp.line_no = line_no;
tmp.token_type = END_OF_FILE;
if (!input.EndOfInput())
input.GetChar(c);
else
return tmp;
switch (c) {
case '-':
input.GetChar(c);
if (c == '>') {
tmp.token_type = ARROW;
} else {
if (!input.EndOfInput()) {
input.UngetChar(c);
}
tmp.token_type = ERROR;
}
return tmp;
case '#':
tmp.token_type = HASH;
return tmp;
case '*':
tmp.token_type = STAR;
return tmp;
default:
if (isalpha(c)) {
input.UngetChar(c);
return ScanId();
} else if (input.EndOfInput())
tmp.token_type = END_OF_FILE;
else
tmp.token_type = ERROR;
return tmp;
}
}