Created
April 3, 2011 23:29
-
-
Save geekgonecrazy/900919 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
This program demonstrates a parser | |
*/ | |
import java.io.*; | |
import java.io.IOException; | |
import javax.swing.*; | |
import java.util.*; | |
import java.io.File; | |
import java.io.FileNotFoundException; | |
import java.lang.*; | |
import java.util.NoSuchElementException; | |
public class Stackparser | |
{ | |
public static void main(String[] args)throws Exception | |
{ | |
PrintWriter outpt; | |
//Now equate the internal name to an external file | |
outpt = new PrintWriter(new File("JavaStackParser.txt")); | |
GenericManagerStacks<Integer> fstack = new GenericManagerStacks<Integer>(); | |
int i, num, ivalu; | |
//Let's create a stack | |
//Now lets push three numbers on the stack | |
System.out.println("Pushing 37"); | |
fstack.pushnode(37); | |
System.out.println("Pushing 44"); | |
fstack.pushnode(44); | |
System.out.println("Pushing 23"); | |
fstack.pushnode(23); | |
//Now print some stack statistics | |
for(i=0;i<=2;i++) | |
{ | |
System.out.println("We have the following stack number"+fstack.getnumber()); | |
System.out.println("peek"+fstack.peeknode()); | |
System.out.println("pop node"+fstack.popnode()); | |
} | |
//Now add more to the stack | |
System.out.println("the stack is empty, we hope let's check number is"+fstack.getnumber()); | |
System.out.println("let's add more to the stack"); | |
fstack.pushnode(45); | |
fstack.pushnode(23); | |
fstack.pushnode(17); | |
fstack.pushnode(21); | |
fstack.pushnode(76); | |
for(i=0;i<=4;i++) | |
{ | |
System.out.println("We have the following stack number"+fstack.getnumber()); | |
System.out.println("peek"+fstack.peeknode()); | |
System.out.println("pop node"+fstack.popnode()); | |
} | |
System.out.println("This is the end of the new Generic Manager Stacks Test"); | |
outpt.close(); | |
//**Stack Test Complete**// | |
//Rules here | |
//Now create the variable array | |
char [] vart={'A','B','C','D','E','0','1','2','3','4','5','6','7','8','9'}; | |
//variable int table | |
//A=7;B=6;C=-2;D=3;E=1; | |
int [] ivalue={7,6,-2,3,1,0,1,2,3,4,5,6,7,8,9}; | |
//Operator Tables | |
char [] opert={'*','/','+','-',')','(','#'}; | |
//Now create evaluation. The higher priority the higher the number in the table | |
int [] intvalp={2,2,1,1,99,-99,-100}; | |
//**Creating stacks for the operands and the operators** | |
//Operand stack | |
GenericManagerStacks<Integer> opnd = new GenericManagerStacks<Integer>(); | |
//Operator stack | |
GenericManagerStacks<Opertobj> oper = new GenericManagerStacks<Opertobj>(); | |
//We must initialize the operator stack so the first operator can be pushed on. | |
System.out.println("pushing operator #with priority - 100"); | |
//Create the operator object and push it on the stack. | |
Opertobj pnode1 = new Opertobj('#',-100); | |
oper.pushnode(pnode1); | |
int oprior,exvalue; | |
//Done with table and stack creation | |
//A*3*(B-C)/(A-C)+(A*B*(9+E))# | |
char [] express={'A','*','3','*','(','B','-','C',')','/','(','A','-','C',')','+','(','A','*','B','*','(','9','+','E',')',')','#'}; | |
i=0; | |
while(express[i]!='#') | |
{ | |
System.out.println("parsing"+express[i]); | |
if(((express[i]>='0')&&(express[i]<='9'))||((express[i]>='A')&&(express[i]<='Z'))) | |
{ | |
//Check to see if this character is a variable or an operator. | |
//We have a variable or a constant | |
System.out.println("this is and operand"+express[i]); | |
//Find the character in the vart table that corresponds with the value | |
ivalu=findval(express[i],vart,ivalue,14); | |
if(ivalu==-99) | |
{ | |
System.out.println("were pushing it on the operand stack"+ivalu); | |
opnd.pushnode(ivalu); | |
}//end of variable stack | |
else | |
{ | |
//we are an operator | |
System.out.println("this is an operator"+express[i]); | |
if(express[i]=='(') | |
{ | |
//this is a left parenthesis, push it to the stack | |
System.out.println("pushing on operator stack"+express[i]); | |
//Create node to push on stack | |
Opertobj pnodeo = new Opertobj(express[i],-99); | |
oper.pushnode(pnodeo); | |
} | |
else | |
{ | |
if(express[i]==')') | |
{ | |
//This is a right parenthesis, we must begin to pop operands and operators | |
//until we find a left parenthesis | |
while((oper.peeknode()).operator!='(') | |
{ | |
//must pop and evaluate the stuff on operand and operator stack | |
popevalandpush(oper,opnd); | |
} | |
//now pop the ( node | |
oper.popnode(); | |
}//end of this is a right parenthesis | |
else | |
{ | |
//this is not either ( or ) it is another operator | |
oprior=findval(express[i],opert,intvalp,5); | |
System.out.println("peeking at top of stack"+(oper.peeknode()).priority); | |
//oprior must be stricktly greater than before we can put it on the stack. | |
while(oprior<=(oper.peeknode()).priority) | |
{ | |
popevalandpush(oper,opnd); | |
} | |
// now push this operator on the stack | |
System.out.println("Pushing operator"+express[i]+"with priority"+oprior); | |
Opertobj pnodeo = new Opertobj(express[i],oprior); | |
oper.pushnode(pnodeo); | |
}//this is the end of this not () operator | |
}//end of on operator stack | |
i++; | |
} | |
} | |
//we have found the # in the evaluation now we must evaluate the operator stack | |
while((oper.peeknode()).operator!='#') | |
{ | |
//we are finishing up operator stack | |
popevalandpush(oper,opnd); | |
}//End of finishing up operator stack | |
//we're done, get value of opnd stack and print | |
exvalue=opnd.popnode(); | |
System.out.println("The value for this expression is"+exvalue); | |
ivalu=findval(express[i],vart,ivalue,14); | |
} | |
}//end of main | |
public static int IntEval(int oper1, char oper, int oper2) | |
{ | |
//this is an evaluator for binary operators operating on integers. | |
int result; | |
switch(oper) | |
{ | |
case '+' : result=oper1+oper2; | |
System.out.println("**eval"+oper1+oper+oper2+"*result"+result); | |
return result; | |
case '*' : result=oper2*oper1; | |
System.out.println("**eval"+oper1+oper+oper2+"*result"+result); | |
return result; | |
case '-' : result=oper2-oper1; | |
System.out.println("**eval"+oper1+oper+oper2+"*result"+result); | |
return result; | |
case '/' : if(oper2!=0){ | |
result=oper2/oper1; | |
System.out.println("**eval"+oper1+oper+oper2+"*result"+result); | |
return result; | |
} | |
else | |
{ | |
System.out.println("Attempted to divide by zero not allowed"); | |
return -99; | |
} | |
default: System.out.println("bad operator"+oper); | |
return -99; | |
}//end of switch | |
}//end of IntEval | |
public static int findval(char x, char [] vtab, int [] valtb, int last) | |
{ | |
int i, vreturn=-99; | |
//this finds the character x in the value table vtab and returns the | |
//corresponding integer value table from valtb | |
for(i=0;i<=last; i++) | |
if(vtab[i]==x)vreturn=valtb[i]; | |
System.out.println("found this char"+x+"value is "+vreturn); | |
return vreturn; | |
}//end of findval; | |
public static void popevalandpush(GenericManagerStacks<Opertobj> x, GenericManagerStacks<Integer> y) | |
{ | |
//this is the start of pop and push | |
int a,b,c; | |
char operandx; | |
operandx=(x.popnode()).Getopert(); | |
a=y.popnode(); | |
b=y.popnode(); | |
System.out.println("in popeval"+b+operandx+a); | |
c=IntEval(b,operandx,a); | |
//now push the value back on the stack for integers | |
y.pushnode(c); | |
return; | |
}//this is the end of popevalandpus | |
}//this is the end of stackparser | |
class GenericManagerStacks<T> | |
{ | |
protected ArrayList<T> mystack; | |
protected int number; | |
public GenericManagerStacks() | |
{ | |
//this is the gneric constructor | |
number=0;//number is the next avaialbe value in array | |
mystack=new ArrayList<T>(100);//creates an initial arraylist of 100 | |
} | |
public int getnumber(){return number;} | |
public int pushnode(T x) | |
{ | |
System.out.println("in pushnode"+number+"x is"+x); | |
//this pushes a node on the stack. it will always add to the front(top) of the stack | |
mystack.add(number,x); | |
number++; | |
System.out.println("leaving pushnode"); | |
return number; | |
} | |
public T popnode() | |
{ | |
//this function returns the first node in the list | |
T nodeval; //this is the value in the node to be popped | |
//find the node at the head of the list | |
nodeval=mystack.get(number-1); | |
//now pop the node by taking it off the list and moving head | |
mystack.remove(number-1); | |
number--; | |
//now return the value of this node. | |
return nodeval; | |
}//this is the end of popnode | |
public T peeknode() | |
{ | |
//this function retuns the contents of the top of the stack it does not | |
//pop the node, just allows the user to look(peek) at the contents of | |
//first node on he stack. | |
T nodeval; | |
// this is the value to be peeked | |
nodeval=mystack.get(number-1); | |
return nodeval; | |
}//this is the end of peeknode | |
boolean stackempty(){if(number==0)return true; | |
else return false;} | |
}//end of GenericManagerStacks | |
class Opertobj | |
{ | |
//this is an operator class it will hold a character operator and its stack | |
protected char operator; | |
protected int priority; | |
public Opertobj(char oprt, int pri) | |
{ | |
//this is the constructor for the operator object | |
operator=oprt; | |
priority=pri; | |
} | |
public int Getprior(){return priority;} | |
public char Getopert(){return operator;}; | |
}//this is the end of the operator class | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment