Building Parsers with Java

sjm.examples.sling
Class SlingParser

java.lang.Object
  |
  +--sjm.examples.sling.SlingParser

public class SlingParser
extends java.lang.Object

This class provides a parser for Sling, an imperative language that plots the path of a sling stone.

The grammar this class supports is:

 
 statements    = statement statement*;
 statement     = assignment | forStatement | plotStatement;
 assignment    = variable '=' expression ';' ; 
 plotStatement = "plot" expression ';';
 forStatement  = 
     "for" '(' variable ',' expression ',' expression  ')' 
     '{' statements '}';
 
variable = Word;
expression = term (plusTerm | minusTerm)*; plusTerm = '+' term; minusTerm = '-' term; term = element (timesElement | divideElement | remainderElement)*; timesElement = '*' element; divideElement = '/' element; remainderElement = '%' element; element = '(' expression ')' | baseElement | negative;
negative = '-' baseElement;
baseElement = Num | "pi" | "random" | "s1" | "s2" | "t" | variable | oneArg("abs") | oneArg("ceil") | oneArg("cos") | oneArg("floor") | oneArg("sin") | oneArg("tan") | twoArgs("polar") | twoArgs("cartesian") | twoArgs("scale") | twoArgs("sling");
oneArg(i) = i '(' expression ')'; twoArgs(i) = i '(' expression ',' expression ')';
The following program describes about 10,000 interesting plots:
     plot sling(1, 1) + sling(s1, 100*s2);
 

The class SlingIde provides an interactive development environment for Sling.


Field Summary
protected  Alternation baseElement
           
protected  Sequence expression
           
protected  Alternation statement
           
protected  Tokenizer tokenizer
           
protected  WordOrReservedState wors
           
 
Constructor Summary
SlingParser()
           
 
Method Summary
protected  Parser assignment()
           
protected  Parser baseElement()
           
protected static Parser comma()
           
protected  Track divideElement()
           
protected  Parser element()
           
protected  Parser expression()
           
protected  Parser forStatement()
           
protected static Parser lBrace()
           
protected static Parser lParen()
           
protected  Track minusTerm()
           
protected  Parser negative()
           
protected  Parser noArgs(java.lang.String name, SlingFunction f)
           
protected  Parser num()
           
protected  Parser oneArg(java.lang.String name, SlingFunction f)
           
protected  Parser pi()
           
protected  Parser plotStatement()
           
protected  Track plusTerm()
           
protected static Parser rBrace()
           
protected  Track remainderElement()
           
protected  ReservedLiteral reserve(java.lang.String s)
           
protected static Parser rParen()
           
protected  Parser s1()
           
protected  Parser s2()
           
protected  Parser scale()
           
protected static Parser semicolon()
           
 Parser start()
           
protected  Parser statement()
           
protected  Parser statements()
           
protected  Parser term()
           
protected  Track timesElement()
           
 Tokenizer tokenizer()
          Creates a tokenizer that uses a WordOrReservedState instead of a normal WordState.
protected  Parser twoArg(java.lang.String name, SlingFunction f)
           
protected  Parser variable()
           
protected  WordOrReservedState wors()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

expression

protected Sequence expression

statement

protected Alternation statement

baseElement

protected Alternation baseElement

wors

protected WordOrReservedState wors

tokenizer

protected Tokenizer tokenizer
Constructor Detail

SlingParser

public SlingParser()
Method Detail

assignment

protected Parser assignment()

baseElement

protected Parser baseElement()

comma

protected static Parser comma()

divideElement

protected Track divideElement()

element

protected Parser element()

expression

protected Parser expression()

forStatement

protected Parser forStatement()

lBrace

protected static Parser lBrace()

lParen

protected static Parser lParen()

minusTerm

protected Track minusTerm()

negative

protected Parser negative()

noArgs

protected Parser noArgs(java.lang.String name,
                        SlingFunction f)

num

protected Parser num()

oneArg

protected Parser oneArg(java.lang.String name,
                        SlingFunction f)

pi

protected Parser pi()

plotStatement

protected Parser plotStatement()

plusTerm

protected Track plusTerm()

rBrace

protected static Parser rBrace()

remainderElement

protected Track remainderElement()

reserve

protected ReservedLiteral reserve(java.lang.String s)

rParen

protected static Parser rParen()

s1

protected Parser s1()

s2

protected Parser s2()

scale

protected Parser scale()

semicolon

protected static Parser semicolon()

start

public Parser start()

statement

protected Parser statement()

statements

protected Parser statements()

term

protected Parser term()

timesElement

protected Track timesElement()

tokenizer

public Tokenizer tokenizer()
Creates a tokenizer that uses a WordOrReservedState instead of a normal WordState.

twoArg

protected Parser twoArg(java.lang.String name,
                        SlingFunction f)

variable

protected Parser variable()

wors

protected WordOrReservedState wors()

by Steve Metsker