-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexer.java
More file actions
153 lines (131 loc) · 4.72 KB
/
Lexer.java
File metadata and controls
153 lines (131 loc) · 4.72 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
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StringReader;
/**
* Write a description of class Lexer here.
*
* @author (Kelvin Likollari)
* @version (04.06.2021)
*/
public class Lexer {
public static final int err = 0,
num = 1,
id = 2,
plus = 3,
minus = 4,
times = 5,
div = 6,
mod = 7,
lp = 8,
rp = 9,
semico = 10,
where = 11,
and = 12,
eq = 13;
public int token;
public char idval;
public int numval;
private String line = "";
private BufferedReader buf;
Lexer()
{ buf = new BufferedReader(new InputStreamReader(System.in)); // reads text from a character-input stream. in stands for input stream
}
Lexer (String s)
{
buf = new BufferedReader(new StringReader(s)); // reads text from an input string.
}
void init()
{
do
try
{
line = buf.readLine ().trim (); //reads a line of text and removes white space from both sides of the text
if (! line.endsWith (";")) // if the line ends with ;
line = String.format ("%s;", line); // creates the string alongside with the line requirements.
}
catch(Exception e)
{
System.out.println("Error in input"); // the line above catch(Exception e) is a block of code used to handle errors
// it basically says what to do if we encounter an error.
System.exit (1); // system.exit(1) is used for when some error occured and we must exit. We can also used -1 instead of 1.
}
while (line.length () == 0); {
return;
}
}
String getLine()
{ init();
return(line);
}
void getToken()
{ if (line.length() == 0)
token = err;
else switch (line.charAt(0))
{ case '+':
token = plus;
line = line.substring(1).trim();
break;
case '-':
token = minus;
line = line.substring(1).trim();
break;
case '*':
token = times;
line = line.substring(1).trim();
break;
case '/':
token = div;
line = line.substring(1).trim();
break;
case '%':
token = mod;
line = line.substring(1).trim();
break;
case '(':
token = lp;
line = line.substring(1).trim();
break;
case ')':
token = rp;
line = line.substring(1).trim();
break;
case '=':
token = eq;
line = line.substring(1).trim();
break;
default:
if (Character.isDigit(line.charAt(0)))
{ token = num;
numval = line.charAt(0) - '0';
int i = 1;
while (i<line.length() && Character.isDigit(line.charAt(i)))
{ numval = numval*10+line.charAt(i)-'0';
i++;
}
line = line.substring(i).trim();
}
else if (Character.isLowerCase(line.charAt(0)))
{ char c = line.charAt(0);
if (c == 'w' && line.length() >= 5 && line.charAt(1) == 'h' && line.charAt(2) == 'e' && line.charAt(3) == 'r' &&
line.charAt(4) == 'e')
{ token = where;
line = line.substring(5).trim();
}
else if (c== 'a' && line.length() >= 3 && line.charAt(1) == 'n' && line.charAt(2) == 'd')
{ token = and;
line = line.substring(3).trim();
}
else if (line.length() > 1 && Character.isLetter(line.charAt(1)))
{ token = err;
}
else
{ token = id;
idval = c;
line = line.substring(1).trim();
}
}
else
token = err;
}
}
}