0

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();
}
}
Elana
  • 1
  • Did you try to give the jvm more memory? see http://stackoverflow.com/questions/37335/how-to-deal-with-java-lang-outofmemoryerror-java-heap-space-error-64mb-heap or http://stackoverflow.com/questions/1596009/java-lang-outofmemoryerror-java-heap-space?rq=1 –  Apr 11 '16 at 05:26
  • Hard to say without knowing what input you're feeding it and how you're calling it, but have you tried stepping through your code with a debugger? That'll help you a) track down where your code is looping (sounds like an infinite loop if you're running out of memory), and b) understand how code is executed in general. Some general advice: 1) don't have empty catch-blocks (you should want to know when exceptions occur); 2) try to limit the scope of variables to where they need to be used (you've got a whole bunch sitting at class level and it's hard to tell at a glance what modifies what. – Amos M. Carpenter Apr 11 '16 at 06:04

0 Answers0