-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLexer.java
More file actions
181 lines (163 loc) · 5.53 KB
/
Lexer.java
File metadata and controls
181 lines (163 loc) · 5.53 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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.io.*;
@SuppressWarnings("unused")
public class Lexer {
static final String[][] types = {
{"Type", "(int|float|void)"},
{"Keyword", "(if|else|while|for|return)"},
{"Hex", "(0x[0-9a-fA-F]+)"},
{"Id", "([a-zA-Z][0-9a-zA-Z_]*)"},
{"Relop", "(<=|>=|<|>|==|!=)"},
{"Logicalop", "(\\&\\&|\\|\\|)"},
{"Unaryop", "(!|\\&)"},
{"Mulop", "(/|%)"},
{"Assignop", "(=|\\+=|-=)"},
{"Postfixop", "(\\+\\+|--)"},
{"Plusop", "(\\+)"},
{"Minusop", "(-)"},
{"Punctuation", "(\\(|\\)|\\[|\\]|\\{|\\}|;|,)"},
{"Real", "([0-9]+\\.[0-9]+[eE][+-]?[0-9]+)"},
{"Real", "([0-9]+\\.[0-9]+)"},
{"Real", "(\\.[0-9]+[eE][+-]?[0-9]+)"},
{"Real", "(\\.[0-9]+)"},
{"Real", "([0-9]+[eE][+-]?[0-9]+)"},
{"Int", "([0-9]+)"}, //maximum of 2147483648, add exception?
{"Starop", "(\\*)"},
{"Comment", "(\\/\\*.*\\*\\/)"},
{"Commenterror", "(\\/\\*.*)"},
{"Newline", "(\n|\r|\f)"},
//Catch-all for errors.
{"Error", "(^[a-zA-z].+)"}
};
Pattern pattern;
String input = "";
Matcher m;
Token currentToken;
Token nextToken;
// Keeps track of what line we're on for better error messages.
int currline = 1;
public Lexer(File f) throws IOException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
FileReader reader = new FileReader(f);
int x = reader.read();
while(x != -1) {
input += (char)x;
x = reader.read();
}
System.out.println("input is " + input);
//Regular expression string of all entries in types array, to be passed into the matcher.
String regExprString = types[0][1];
for(int i = 1 ; i < types.length; i ++) {
regExprString += "|" + types[i][1];
}
pattern = Pattern.compile(regExprString);
m = pattern.matcher(input);
// To set the first token as nextToken before reading anything in.
getNextToken();
}
// Returns a Token object of the next token in the input string.
// Sets nextToken to the next token if one exists. Returns currentToken.
public Token getNextToken() throws IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
currentToken = nextToken;
if(m.find()) {
for(int i = 1 ; i <= types.length; i++) {
if(m.group(i) != null) {
if (types[i-1][0] == "Error") {
nextToken = new Error(m.group(i), currline);
i = types.length + 1;
} else {
nextToken = (Token)Class.forName( types[i-1][0] )
.getConstructor(String.class)
.newInstance(m.group(i));
nextToken.currline = currline;
i = types.length + 1;
}
}
}
}
else {
nextToken = null;
}
// Increment our current line if we've just read a newline.
if ((currentToken != null) && currentToken.getClass() == Newline.class) {
currline++;
}
// Make sure we're on the right line
if ((currentToken != null) && currentToken.getClass() == Error.class) {
currentToken.setCurrline(currline);
}
return currentToken;
}
// Returns a Token object of the next token in the input string.
public Token peekNextToken() {
return nextToken;
}
// Returns true if we have another token, false otherwise.
public boolean hasNextToken() {
return (nextToken!= null);
}
// Returns the current line.
public int getCurrLine() {
return currline;
}
public static void main(String []args) throws IOException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, ClassNotFoundException {
Lexer lex;
if (args.length !=0) {
lex = new Lexer(new File(args[0]));
System.out.println(lex.pattern);}
/* This would allow the user to enter lines in the console until CTRL-D, that are then written to a file that is passed to the lexer.
else {
//Scanner console = new Scanner(System.in);
//File stdin = new File("input");
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintStream stream = new PrintStream("input.txt");
String testLine = null;
//String line = "";
while((testLine = reader.readLine()) != null) {
//line = line + "" + testLine;
stream.println(testLine);
stream.flush();
}
stream.close();
}
catch(IOException e){
System.out.println("Error during reading/writing");
}
lex = new Lexer(new File("input.txt"));
}*/
else {
System.out.println("Please enter the name of the file to read from: ");
Scanner console = new Scanner(System.in);
String filename = console.nextLine();
lex = new Lexer(new File(filename));
System.out.println(lex.pattern);
}
while(lex.hasNextToken()){
Token returnToken = lex.getNextToken();
if(returnToken.getClass()==(Error.class)) {
System.out.println(returnToken.toString());
return;
}
else if (returnToken.getClass()==(Int.class)){
try{
Integer.parseInt(returnToken.name);
System.out.println(returnToken.toString() + " ");
}
catch(NumberFormatException e){
Token errorToken = new Error("Out of bounds integer", returnToken.currline);
System.out.println(errorToken.toString());
}
}
else {
System.out.print(returnToken.toString() + " ");
}
}
}
}