CS 325            Project            Part I

 

You are to write a lexical scanner for SIMPL.  The scanner will have a function getToken which will return the next token.  The token should contain token type and the string value of the token if required.  The token should also contain the numeric value (0 or 1) for the boolean constants.

 

The following table gives the token types in the SIMPL language. (Note: a \ indicates that a meta-character is being used as a literal.)

token

! | ? | \| | : | <- | \( | \) | if | while | > | = | \* | / | \+ | - | ‘ | bool |  id | num| boolean

id

letter (letter | digit | _)*

num

digit+

letter

a | b | c ... | z | A | B | C ... | Z

digit

0 | 1 | 2 ... | 9

boolean

 .T | .F

 

 

Your scanner should use the following driver program to test your code.

 

public static void main(String[] args) {

      try{

          System.out.println(args[0]);

          scanner s = new scanner(new FileInputStream(args[0]));

          token t;

          do{

            t = s.getToken();

            System.out.println(t.type);

            System.out.println(t.name);

            System.out.println();

          }while(t.type != EOF);

      }catch(FileNotFoundException e){System.err.println("Bad File");}

    }

 

The program takes an argument which is the name of the test file.  You may run the java application in Eclipse by creating a new launch configuration using the Run… menu selection.  You can supply the program arguments by selecting the Arguments tab and typing the arguments in the program arguments window.  The arguments are Strings so you must type them in quotes.

 

The scanner constructor takes an InputStream i as an argument.  I suggest that the scanner creates a PushbackInputStream as follows:

 

            in = new PushbackInputStream(i);

 

You can read from the PushbackInputStream with

 

      char readchar() {

            int ch;

            try {

                  ch = in.read();

                  if (ch == -1) return ‘\0’;

                  else return (char)ch;

            } catch(IOException e) {

                  System.err.println(“IO exception in readchar”);

                  return ‘\0’;

            }

      }

 

      void unread(char c) {

            try {

                  in.unread((int)c);

            } catch (IOException e)

System.err.println(“Can not unread”);

      }

 

Your scanner needs to be able to handle errors.  If the scanner encounters an unexpected character, no error message should be printed by the scanner, but the current incomplete token, together with the unexpected character should be returned as an error token. 

 

Email your completed project folder to me in zipped format for grading