/** * Simple LL(1) parser for Homework 2 * Creation date: (09/11/2000 9:57:33 PM) * @author: Tzvetan Horozov */ import java.io.*; import java.util.*; public class hw2solution { // We first declare our tokens and give them INT value static final int NOT=0, AND=1, OR=2, AND_AND=3, OR_OR=4, TRUE=5, FALSE=6, SPACE=7, EOF=8; // Then we list all possible tokens recognized by the parser static final String[] tokens= { "!", "&", "|", "&&", "||", "true", "false", " "}; // the following is a set of global variables used in the program static Hashtable ht; static BufferedReader myReader; // Input stream static String line=null; static int pos=0; static int tok; static char t; static boolean charReceived=false; static String temp=""; static int lnum=0; // The next function reads a character from the Input stream // myReader and returns a string of length 1 character // Anytime our position counter is 0 we read new line. // then we read char by char from the line until we reach its // end and then we read new line. If we reach the end of file // we return null static public String getNextChar() { String temp=" "; try { if (pos==0) if ((line=myReader.readLine())==null) return null; else lnum+=1; try { temp=new String(line.charAt(pos)+""); } catch (IndexOutOfBoundsException e) { pos=0; return " "; } pos+=1; } catch (IOException e) { System.err.println("Error reading input file "); } return temp; } // The following routine returns the next token in our Input stream // the inportant thing here is that we always read one character // ahead. That is why after every read we check whether we have not // reached the end of the Input sream, and if so we return the EOF // token public static int getToken() { String token=""; // We first chech whether we are entering for the first time // If so then we read the first character from the Input if (!charReceived) { temp=getNextChar(); if (temp!=null) t=temp.charAt(0); charReceived=true; } // The first thing to chech when we enter getToken for the // secod time is whether the last symbol from the Input // was not null, indicating the end of the Input if (temp==null){ return EOF; } // Then we inspect the current character from the Input // Everytime we complete one of the If statements below // we have come up with a token, so we read the next // character from the Input and check it for EOF // First check for white space if (t==' ') { token+=t; temp=getNextChar(); if (temp!=null) t=temp.charAt(0); } // If t is letter then read until non letter read else if ((t>='a')&&(t<='z')) { while ((t>='a')&&(t<='z')) { token+=t; temp=getNextChar(); if (temp!=null) t=temp.charAt(0); } } // If t is ! then return NOT else if (t=='!') { token+=t; temp=getNextChar(); if (temp!=null) t=temp.charAt(0); } // If t is & then check for a second & else if (t=='&') { token+=t; temp=getNextChar(); if (temp!=null) t=temp.charAt(0); if (t=='&') { token+=t; temp=getNextChar(); if (temp!=null) t=temp.charAt(0); } } // If t is | then check for another | else if (t=='|') { token+=t; temp=getNextChar(); if (temp!=null) t=temp.charAt(0); if (t=='|') { token+=t; temp=getNextChar(); if (temp!=null) t=temp.charAt(0); } } // At the end if none of the above tokens were recognized // we simply read the next character and return -1 // indicating that no token was recognized else { if (temp==null){ return EOF; } temp=getNextChar(); if (temp==null){ return EOF; } t=temp.charAt(0); return -1; } // If some token was recognized then we check whether // we have it defined in our grammer by inspecting the // Hashtable which was initialized upon startup with // all known tokens if (ht.containsKey(token)) return new Integer(ht.get(token).toString()).intValue(); return -1; } // The next routine advances to the next token ignoring white spaces public static void advance(){ tok=getToken(); while (tok==SPACE) tok=getToken(); } // The following routine checks if the current token matches the // token t public static void eat(int t) { if (tok==t) advance(); else error(); } // This function prints a standart error message public static void error() { System.out.println(); System.out.println ("Parse error in line "+lnum); System.out.println (line); for (int i=0;i