Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Help me find the infinite loop in this code

$
0
0
I'm having trouble with my Lexical Analyzer and my project is completed, yet with errors. The most noticeable is an infinite loop in the class JordansLexicalAnalyzer. The other classes are specified in my project folder, but anyways, here's the code I did, modifying from an example given to me from my professor:

import java.io.FileNotFoundException;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class JordansLexicalAnalyzer {

	private List<Token> tokens;
	
	public JordansLexicalAnalyzer(String filename) throws LexException, FileNotFoundException {
		if(filename == null){
			throw new IllegalArgumentException("null string argument");
		}
		tokens = new LinkedList<Token>();
		Scanner input = new Scanner(new File(filename));
		int linenum = 0;
		while(input.hasNext()){
			String line = input.nextLine();
			linenum++;
			processLine(line, linenum);
		}
		
		tokens.add(new Token (TokenType.EOS, "$", linenum + 1, 1));
		tokens.add(new Token (TokenType.EOLN, "$", linenum + 1, 1));
		input.close();
	}

	/**
	 * @param args
	 */
	private void processLine(String line, int rowNum) throws LexException{
		assert line != null;
		assert rowNum > 0;
		int index = 0;
		boolean done = false;
		
		do
		{
			index = skipWhiteSpace(line, index);
			if (index == line.length()){
				done = true;
			}
			else {
				int columnNum = index + 1;
				String lexeme = null;
				TokenType tok = null;
				int lineNum = index + 10;
				if (Character.isLetter(line.charAt(index))){
					int i = index;
					while(i < line.length() && (Character.isLetter(line.charAt(i)) || Character.isDigit(line.charAt(i))))
						i++;
					lexeme = line.substring(index, i);
					index = i;
					tok = TokenType.IDENT;
				}
				else if (Character.isDigit(line.charAt(index))){
					int i = index;
					while(i < line.length() && Character.isDigit(line.charAt(i)))
						i++;
					lexeme = line.substring(index, i);
					index = i;
					tok = TokenType.INT_LIT;
				}
				else if (line.contains(">=")){
					tok = TokenType.GREATER_THAN_OR_EQUALS_OP;
				}
				else if (line.contains("<=")){
					tok = TokenType.LESS_THAN_OR_EQUALS_OP;
				}
				else if (line.contains("==")){
					tok = TokenType.EQUALS_OP;
				}
				else if (line.contains("<>")){
					tok = TokenType.NOT_EQUAL_OP;
				}
				else if (line.contains("LEFT")){
					tok = TokenType.LEFT;
				}
				else if (line.contains("PRINT")){
					tok = TokenType.PRINT;
				}
				else if (line.contains("STOP")){
					tok = TokenType.STOP;
				}
				else if (line.contains("IF")){
					tok = TokenType.IF;
				}
				else if (line.contains("GOTO")){
					tok = TokenType.GOTO;
				}
				else {
					switch (line.charAt(index)){
						case '+':
							tok = TokenType.ADD_OP;
							break;
						case '-':
							tok = TokenType.SUB_OP;
							break;
						case '*':
							tok = TokenType.MULT_OP;
							break;
						case '/':
							tok = TokenType.DIV_OP;
							break;
						case '>':
							tok = TokenType.GREATER_THAN_OP;
							break;
						case '<':
							tok = TokenType.LESS_THAN_OP;
							break;
						case '=':
							tok = TokenType.EQUALS_OP;
							break;
						default:
							throw new LexException ("invalid lexeme", rowNum, columnNum);
					}
					lexeme = (new Character (line.charAt(index))).toString();
					index++;
				}
				Token t = new Token(tok, lexeme, lineNum, columnNum);
				tokens.add(t);
			}
		}
		while(!done);
		System.out.println("Hello");
	}
	
	private int skipWhiteSpace(String line, int index){
		while (index < line.length() && Character.isWhitespace(line.charAt(index))){
			index++;
		}
		return index;
	}
	public Token getNextToken()
	{
		if (tokens.isEmpty())
			throw new RuntimeException ("no more tokens");
		return tokens.remove(0);
	}
	public Token getLookaheadToken()
	{
		if (tokens.isEmpty())
			throw new RuntimeException ("no more tokens");
		return tokens.get(0);
	}
	public boolean moreTokens ()
	{
		return !tokens.isEmpty();
	}
	
	public static void main(String[] args) throws FileNotFoundException, LexException {
		JordansLexicalAnalyzer test = new JordansLexicalAnalyzer("test3.bas");
		System.out.println(test);
	}
}



May you please help me spot the infinite loop? I keep getting errors on line 21, line 122, and line 155, I seem to believe.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>