I have started a java program and I have one problem that I can't
overcome. I have commented in my code as to what I would like to do
but am unable to do correctly. Your help is greatly appreciated. The
guidelines for the code is the following url address:
http://www.cs.uiowa.edu/~slonnegr/plc/ScanWrenHW.pdf
code:
import java.io.*;
class Token
{
static final int IDENTIFIER=0, NUMERAL=2, RESERVED=3, RELATION=4,
OP=5, OTHER=6, EOF=7, ERROR=9;
private int type;
private String value;
Token(int t, String v){ type =t; value=v; }
int getType(){ return type; }
String getValue(){ return value; }
}
public class Wrenreader
{
private char currChar=' ';
private BufferedReader br;
Wrenreader(Reader input)
{ br=new BufferedReader(input); }
Token getToken()
{
while(currChar==' ')
currChar=getChar();
if(Character.isDigit(currChar))
{
int numval=0;
while(Character.isDigit(currChar))
{
numval=10*numval+currChar-'0';
currChar=getChar();
}
return new Token(Token.NUMERAL, Integer.toString(numval));
}
else
{
/*I need to do something here to get the token for the remaining
//types but I've hit a road block and I can't seem to come up with
//an answer to the problem. I'm not sure how to create a switch
statement
//I would like to do something like:
//int tok;
switch (currChar) // recognize a special symbol
{
case 'program'||'is' : tok = Token.LPAREN; break;
default : tok = Token.ERROR; break;
}
Token token = new Token(tok, "" + currChar);
if (tok != Token.EOL)
currChar = getChar();
return token;*/
}
char getChar()
{
try
{
int line=br.read();
if(line == -1) line='\n';
return (char)line;
}
catch(IOException ioe)
{ return '\n'; }
}
void scan()
{
Token token;
currChar=getChar();
do
{
token=getToken();
switch(token.getType())
{
case Token.IDENTIFIER:
System.out.println("IDENTIFIER: " + token.getValue()); break;
case Token.NUMERAL:
System.out.println("NUMERAL: " + token.getValue()); break;
case Token.RESERVED:
System.out.println("RESERVED: " + token.getValue()); break;
case Token.RELATION:
System.out.println("RELATION: " + token.getValue()); break;
case Token.OP:
System.out.println("OP: " + token.getValue()); break;
case Token.OTHER:
System.out.println("OTHER: " + token.getValue()); break;
case Token.EOF:
System.out.println("EOF"); break;
case Token.ERROR:
System.out.println("ERROR: " + token.getValue()); break;
}
}
while(token.getType() != Token.EOF);
}
public static void main(String[]args)
{
String filename="";
try
{
Wrenreader wr=new Wrenreader(new FileReader(filename));
wr.scan();
}
catch(Exception e)
{ }
}
}
end of code