I'm new to programming and I have an assignment that asks us to make a Lexer that implements Iterator (in java). We are given Strings of equations with variable white spaces and we have to produce Lexemes for a variety of types. This is what I have so far but when I run it, I get an OutOfMemoryError: Java heap space. I have no idea what is causing this error or if I am even on the right track with my code. Any suggestions?
Thanks
public enum LexemeType {
LEFT_PAREN, RIGHT_PAREN, OPERATOR, NUMBER, VARIABLE, EQUALS, SEMICOLON, USER_INPUT;
}
import java.io.*;
import java.util.*;
public class Lexer implements Iterator<Lexeme> {
Lexeme token = null; // last token recognized
boolean eof = false; // reached end of file
private Reader reader = null; // input stream
private int lookahead = 0; // lookahead, if any
private int[] buffer = new int[100]; // lexeme buffer
private int index = 0; // length of lexeme
public Lexer(String toLex) {
this.reader = new StringReader(toLex);
}
// Extract lexeme from buffer
public String getLexeme() {
return new String(buffer,0,index);
}
// Reset state to begin new token
private void reset() throws IOException {
if (eof)
throw new IllegalStateException();
index = 0;
token = null;
if (lookahead==0)
read();
}
// Read one more char.
// Add previous char, if any, to the buffer.
private void read() throws IOException {
if (eof)
throw new IllegalStateException();
if (lookahead != 0) {
buffer[index] = lookahead;
index++;
}
lookahead = reader.read();
}
// Recognize a token
public void lex() throws IOException {
reset();
// Skip whitespace
while (Character.isWhitespace(lookahead)) {
read();
}
reset();
// Recognize (
if (lookahead == '(') {
token = new Lexeme(LexemeType.LEFT_PAREN, "(");
read();
return;
}
// Recognize )
if (lookahead == ')') {
token = new Lexeme(LexemeType.RIGHT_PAREN, "(");
read();
return;
}
// Recognize ;
if (lookahead == ';') {
token = new Lexeme(LexemeType.SEMICOLON, ";");
read();
return;
}
// Recognize =
if (lookahead == '=') {
token = new Lexeme(LexemeType.EQUALS, "=");
read();
return;
}
// Recognize ?
if (lookahead == '?') {
token = new Lexeme(LexemeType.USER_INPUT, "?");
read();
return;
}
// Recognize float
if (Character.isDigit(lookahead)) {
do {
read();
} while (Character.isDigit(lookahead));
if (lookahead=='.') {
read();
while (Character.isDigit(lookahead))
read();
}
token = new Lexeme(LexemeType.NUMBER, ("-?[0-9]+"));
return;
}
// Recognize string
if (lookahead=='"') {
do {
read();
} while (lookahead!='"');
read();
token = new Lexeme(LexemeType.VARIABLE, ("^[a-zA-Z]*$"));
return;
}
}
@Override
public boolean hasNext() {
if (token!=null)
return true;
if (eof)
return false;
try {
lex();
} catch (IOException e) {
}
return true;
}
@Override
public Lexeme next() {
if (hasNext()) {
Lexeme result = token;
token = null;
return result;
}
else
throw new IllegalStateException();
}
}