slogo.parser
Class SLogoParser

java.lang.Object
  extended by slogo.parser.SLogoParser

public class SLogoParser
extends java.lang.Object

Illustrates the java.io.StreamTokenizer class and how it can be used to parse tokens for a toy example programming language (elan). For this example simple instructions are parsed, but lots of infrastructure is in place for parsing a more complete language.

Author:
Owen Astrachan
See Also:
slogo.expression, slogo.instruction, InstructionParser

Field Summary
protected  SLogoContext myContext
           
 java.lang.Object myResult
           
protected  Token myToken
           
protected  java.io.StreamTokenizer myTokenizer
           
 
Constructor Summary
SLogoParser(Turtle turtle)
           
 
Method Summary
 void error(Token t)
           
 Context getContext()
           
 java.lang.Object getMyResult()
           
 Token getToken()
          returns the current token, doesn't fetch a new one
 java.lang.Object interpret(java.io.Reader r)
          parses an expression and prints the result of evaluating the expression.
 java.lang.Object interpret(java.lang.String cmd)
           
static void main(java.lang.String[] args)
          Basic driver for SLogoParser
 Token match(Token rhs)
          try to match a token, if unsuccessful throw an exception, otherwise match succeeds, and next token is obtained and returned
protected  Token nextToken()
          get the next token and store it for retrieval by getToken()
protected  GrammarElement parseInstructionParameter()
           
protected  java.util.List<GrammarElement> parseInstructionParameters(int numParameters)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

myTokenizer

protected java.io.StreamTokenizer myTokenizer

myToken

protected Token myToken

myContext

protected SLogoContext myContext

myResult

public java.lang.Object myResult
Constructor Detail

SLogoParser

public SLogoParser(Turtle turtle)
Method Detail

interpret

public java.lang.Object interpret(java.lang.String cmd)
Parameters:
cmd - string command from text field

interpret

public java.lang.Object interpret(java.io.Reader r)
parses an expression and prints the result of evaluating the expression.

Parameters:
r - the reader that will be tokenized
Returns:
the result of executing the last command

nextToken

protected Token nextToken()
get the next token and store it for retrieval by getToken()

Returns:
the current (just read) token

getToken

public Token getToken()
returns the current token, doesn't fetch a new one

Returns:
the current token

parseInstructionParameter

protected GrammarElement parseInstructionParameter()
Returns:

parseInstructionParameters

protected java.util.List<GrammarElement> parseInstructionParameters(int numParameters)
Parameters:
numParameters -
Returns:

match

public Token match(Token rhs)
try to match a token, if unsuccessful throw an exception, otherwise match succeeds, and next token is obtained and returned

Parameters:
rhs - token being matched
Returns:
the next read token
Throws:
ParseException - if match fails

error

public void error(Token t)
Parameters:
t - token where error occurred
Throws:
a - ParseException labeled by t

getContext

public Context getContext()

getMyResult

public java.lang.Object getMyResult()

main

public static void main(java.lang.String[] args)
Basic driver for SLogoParser

Parameters:
args -