CSC448: Scanning/Parsing: Homework [20/22] Previous pageContentsNext page

We will do lab 3 from the CAC website. See the link on the course homepage.

I have posted a video on getting started with the homework.

The video implements this FSA:

lab3_simple_fsa

It uses the following code.

package lab3;

import java.util.Enumeration;

public class Fsa {
 static final int X = -1;

 static int GOTO[][] = {
/*     B    D    S    O    C    S    T    N    S    W    O */
/*     l    e    e    r    o    t    e    o    t    i    t */
/*     a    f    m         m    r    r    n    a    t    h */
/*     n    i    i         m         m         r    h    e */
/*     k    n              a         i         t         r */
/*          e                        n                     */
/*                                   a                     */
/*                                   l                     */
/*                                                         */
{      0,   X,   X,   X,   X,   X,   1,   0,   X,   X,   X} /* 0 */,
{      1,   X,   X,   X,   X,   2,   X,   X,   X,   X,   X} /* 1 */,
{      2,   X,   X,   X,   X,   3,   X,   X,   X,   X,   X} /* 2 */,
{      3,   X,   0,   X,   4,   X,   X,   X,   X,   X,   X} /* 3 */,
{      4,   X,   X,   X,   X,   3,   X,   X,   X,   X,   X} /* 4 */,
};

 static int ACTION[][] = {
/*     B    D    S    O    C    S    T    N    S    W    O */
/*     l    e    e    r    o    t    e    o    t    i    t */
/*     a    f    m         m    r    r    n    a    t    h */
/*     n    i    i         m         m         r    h    e */
/*     k    n              a         i         t         r */
/*          e                        n                     */
/*                                   a                     */
/*                                   l                     */
/*                                                         */
{      0,   X,   X,   X,   X,   X,   0,   0,   X,   X,   X} /* 0 */,
{      0,   X,   X,   X,   X,   0,   X,   X,   X,   X,   X} /* 1 */,
{      0,   X,   X,   X,   X,   2,   X,   X,   X,   X,   X} /* 2 */,
{      0,   X,   0,   X,   0,   X,   X,   X,   X,   X,   X} /* 3 */,
{      0,   X,   X,   X,   X,   2,   X,   X,   X,   X,   X} /* 4 */,
};

   public Fsa(Enumeration e) {
      // Uncomment the line below and each token processed will be echoed.
      //((TokenEnum)e).setDebug(true);

      SymbolTable symboltable = new SymbolTable();

      int state = 0;

      String 
         lhs     = "", 
         term    = "", 
         nonterm = "$FINAL$";

      while (e.hasMoreElements()) {
         Token t = (Token)e.nextElement();

         System.out.println("   Read token type " + t.type() + ": " + t);

         int action = ACTION[state][t.type()];
         int newstate = GOTO[state][t.type()];

         //System.out.println("State " + state + " Performing action " + action + " and going to " + newstate);

         switch (action) {
         case  X: /* error */
             System.out.println(" ABORT ");    
             System.exit(0);
         case  0: /* do nothing */
             break;    
         case  2: /* terminal */
       	     symboltable.enterTerminal(t.strValue());
       	     System.out.println("Found terminal " + t.strValue());
             break;
         }

         state = newstate;
      }
      if (state != 0) oops("End in bad state: " + state);
   }

   void oops(String s) {
      System.err.println("Error: " + s);
      System.out.println("ABORT");
      System.exit(-1);
   }
}

Previous pageContentsNext page