// Syner.java Oct.6, 1998 By Gregor v. Bochmann & Yaoping Wang import java.io.*; import java.util.*; class Lexer { public final int BEGIN = 1; public final int END = 2; public final int ASSIGN = 3; public final int PLUS = 4; public final int MINUS = 5; public final int IDENT = 6; public final int SEMICOLON = 7; // -------------------------------------- // // PORTION BELOW CREATED FOR ASSIGNMENT 3 // // -------------------------------------- // public final int MULTIPLY = 8; public final int DIVIDE = 9; public final int GREATER = 10; public final int GREATEQ = 11; public final int LESSER = 12; public final int LESSEQ = 13; public final int OPENB = 14; public final int CLOSEB = 15; public final int OPENC = 16; public final int CLOSEC = 17; public final int NUMBER = 18; public final int WHILE = 19; public final int EQUAL = 20; private int n; // holds next digit // -------------------------------------- // // PORTION ABOVE CREATED FOR ASSIGNMENT 3 // // -------------------------------------- // public String idName; // identifier name public int token; //holds the next token private BufferedReader input; // input file buffer private char c; //holds the next character public Lexer(String inputFile){ try{input = new BufferedReader(new FileReader(inputFile));} catch(IOException ee) {ee.printStackTrace();} } public void start() throws IOException, EndOfFileEncountered{ nextChar(); getNextToken(); } public void getNextToken()throws IOException, EndOfFileEncountered { disposeSpace(); String terminalString =""; int terminalNum = 0; // created for assignment 3 if(Character.isLetter(c)){ //first character is a letter, get whole alphanumeric string terminalString += c; nextChar(); while(Character.isLetterOrDigit(c)){terminalString += c; nextChar();} idName = terminalString; if(terminalString.equals("BEGIN")) token = BEGIN; else if(terminalString.equals("END")) token = END; else if(terminalString.equals("WHILE")) token = WHILE; //created for assignment 3 else token = IDENT; } else if (c == '+') {token = PLUS; nextChar();} else if (c == '-') {token = MINUS; nextChar();} else if (c == ';') {token = SEMICOLON; nextChar();} else if (c == ':') { //check that next char is '=' to find assignment token ":=" nextChar(); if (c == '=') {token = ASSIGN; nextChar();} else {System.out.println("lexical error: '=' expected after ':'; skip to end of program"); skipToEndOfFile();} } // -------------------------------------- // // PORTION BELOW CREATED FOR ASSIGNMENT 3 // // -------------------------------------- // else if (Character.isDigit(c)){ //first digit is a letter, get whole value terminalNum = terminalNum*10 + (Integer.valueOf(c)-48); nextChar(); while(Character.isDigit(c)) { terminalNum = terminalNum*10 + (Integer.valueOf(c)-48); nextChar(); } idName = Integer.toString(terminalNum); token = NUMBER; } else if (c == '=') {token = EQUAL; nextChar();} else if (c == '<') {//check whether or not the relop is < or <= nextChar(); if (c == '=') {token = LESSEQ; nextChar();} else {token = LESSER; nextChar();} } else if (c == '>') { nextChar(); if (c == '=') {token = GREATEQ; nextChar();} else {token = GREATER; nextChar();} } else if (c == '*') { token = MULTIPLY; nextChar();} else if (c == '/') { token = DIVIDE; nextChar();} else if (c == '(') { token = OPENB; nextChar();} else if (c == ')') { token = CLOSEB; nextChar();} else if (c == '{') { token = OPENC; nextChar();} else if (c == '}') { token = CLOSEC; nextChar();} // -------------------------------------- // // PORTION ABOVE CREATED FOR ASSIGNMENT 3 // // -------------------------------------- // else {System.out.println("invalid lexical unit; skip to end of program"); skipToEndOfFile();} } public void disposeSpace() throws IOException, EndOfFileEncountered{ //get rid of all space like \t, \n, and blank space while(Character.isWhitespace(c)) {nextChar();} } public void nextChar() throws IOException, EndOfFileEncountered{//get next character int i; if((i = input.read()) == -1)//hit the end of file, exception thrown throw new EndOfFileEncountered(); c = (char) i; System.out.print(c); } public void skipToEndOfFile() throws IOException, EndOfFileEncountered { while (true) {nextChar();} } }