Thursday, 19 July 2012

LR parsing

SLR parsing is simple LR parsing, reads input from Left to write and and produces a Right most derivation. Following Java program uses the example provided at Wikipedia SLR page.


/*
* by Ali R+ SARAL
* Please indicate my name if you copy my work.
*/

package nbParse;
import com.sun.xml.internal.fastinfoset.util.CharArray;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
*
* @author Ali Riza SARAL
*/

public class LRparser {

    public enum Symbols {

      TS_ONE, // +
      TS_EOS, // $
      TS_INVALID, // invalid token

      // Non-terminal symbols:
      NTS_S, // S
      NTS_E
      }

   public enum States {
      ST_ZERO,
      ST_ONE,
      ST_TWO,
      ST_THREE
   }

   Symbols lexer(char c) {

      switch (c) {
      case '1':
      return Symbols.TS_ONE;

      case '$':
      return Symbols.TS_EOS;

      case 'E':
      return Symbols.NTS_E;

      default:
      return Symbols.TS_INVALID;
      }
   }

   public static void main(String[] args) {

      LRparser lrparser = new LRparser();
      StringBuilder p = new StringBuilder("111$"); // input buffer

      int ppointer = 0;
      int state = 0;

      //initialize the symbol stream cursor
      ppointer = 0;
      boolean reduced = false;

      for (ppointer = 0; ppointer < p.length(); ppointer++) {

         if (reduced) {
         ppointer = 0;
         reduced = false;
      }

      System.out.println("\np[" + ppointer + "]= " + p.charAt(ppointer));
      System.out.println("lexer(p(ppointer))= " + lrparser.lexer(p.charAt(ppointer)));
      System.out.println("state= " + state);
      System.out.println("p= " + p.toString());

switch (state) {

   case 0:
   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_ONE) {
   System.out.println("1. shift one state 0");
   state = 1;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_EOS) {
   System.out.println("none");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.NTS_E) {
   System.out.println("goto state 2");
   state = 2;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_INVALID) {
   System.out.println("invalid input");
   return;
   }

   break;

   case 1:
   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_ONE) {
   System.out.println("1. shift one state 1");
   state = 1;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_EOS) {
   System.out.println("reduce 2 ");
   p.deleteCharAt(ppointer - 1);
   p.insert(ppointer - 1, 'E');
   state = 0;
   ppointer = 0;
   reduced = true;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.NTS_E) {
   System.out.println("goto state 3");
   state = 3;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_INVALID) {
   System.out.println("invalid input");
   return;
   }

   break;

   case 2:
   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_ONE) {
   System.out.println("none state 2.1");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_EOS) {
   System.out.println("accept ");
   state = 0;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.NTS_E) {
   System.out.println("none state 2.3");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_INVALID) {
   System.out.println("invalid input");
   return;
   }

   break;

   case 3:
   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_ONE) {
   System.out.println("none state 3.1");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_EOS) {
   System.out.println("reduce 1 ");
   p.deleteCharAt(ppointer - 2);
   state = 0;
   ppointer = 0;
   reduced = true;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.NTS_E) {
   System.out.println("none state 3.3");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_INVALID) {
   System.out.println("invalid input");
   return;
   }

   break;
   default:
   System.out.println("Parsing table default");
   return;
   }
   }

   System.out.println("Finished parsing");
   return;
   }
}


Output:

run:
p[0]= 1
lexer(p(ppointer))= TS_ONE
state= 0
p= 111$
1. shift one state 0

p[1]= 1
lexer(p(ppointer))= TS_ONE
state= 1
p= 111$
1. shift one state 1

p[2]= 1
lexer(p(ppointer))= TS_ONE
state= 1
p= 111$
1. shift one state 1

p[3]= $
lexer(p(ppointer))= TS_EOS
state= 1
p= 111$
reduce 2

p[0]= 1
lexer(p(ppointer))= TS_ONE
state= 0
p= 11E$
1. shift one state 0

p[1]= 1
lexer(p(ppointer))= TS_ONE
state= 1
p= 11E$
1. shift one state 1

p[2]= E
lexer(p(ppointer))= NTS_E
state= 1
p= 11E$
goto state 3

p[3]= $
lexer(p(ppointer))= TS_EOS
state= 3
p= 11E$
reduce 1

p[0]= 1
lexer(p(ppointer))= TS_ONE
state= 0
p= 1E$
1. shift one state 0

p[1]= E
lexer(p(ppointer))= NTS_E
state= 1
p= 1E$
goto state 3

p[2]= $
lexer(p(ppointer))= TS_EOS
state= 3
p= 1E$
reduce 1
none state 3.3

p[0]= E
lexer(p(ppointer))= NTS_E
state= 0
p= E$
goto state 2

p[1]= $
lexer(p(ppointer))= TS_EOS
state= 2
p= E$
accept

Finished parsing
BUILD SUCCESSFUL (total time: 0 seconds)