Class Parser

java.lang.Object
  extended by Parser

public class Parser
extends java.lang.Object

Parse an input stream containing an FM language source file and optionally generate postscript code to implement the movie. Implements a recursive descent predictive parser according to the FM language grammar.
$Id$


Field Summary
private  CompilerIO cio
          The CompilerIO object that we can use to write on the output stream if needed.
private  java.util.HashMap firstSets
          HashMap containing int arrays of Token types, keyed by the non-terminal whose FIRST set each array represents.
private  Token prevToken
          prevToken is always the last Token that has been matched.
private static int SC_VARIABLE
          Define the symbol tables that we use in the parser.
private  Scanner scan
          The Scanner that actually reads the input stream and reports the tokens that it finds.
private  boolean showCode
          Enable / disable code generation.
private  boolean showMethods
          Enable / disable method entry/exit tracing.
private  boolean showSymbols
          Enable / disable Symbol entry display.
private  boolean status
          This status variable is set to true before the parse begins.
private  SymbolTable symbolTable
          Symbol table manager.
private  Token theToken
          theToken is always the Token that we should look at next.
private  java.lang.Throwable tracer
          This Throwable is used to get stack trace information
 
Constructor Summary
Parser(CompilerIO io, SymbolTable t, Scanner s)
          Construct a new Parser object.
 
Method Summary
private  void generateCode(java.lang.String code)
          Emit a code string.
private  void generateMovieEnd()
          Format and emit the postscript trailer
private  void generateMovieStart(java.lang.String title)
          Format and emit the postscript title block section
private  void generatePrologEnd()
          Emit the end of the postscript prolog section
private  void generatePrologStart()
          Emit the beginning of the postscript prolog section
private  boolean isFirst(Token t, java.lang.String nonterm)
          Check to see if tokens of the given type are in the FIRST set of the given non-terminal.
private  void matchToken(int type)
          Check that the current Token is of the expected type, and advance past it if it is okay.
private  void matchTokenArray(int[] type)
          Check that the current Token is one of the expected types, and advance past it if it is okay.
private  java.lang.String operator(Token t)
          Convert an operator token into the appropriate postscript name
 boolean parse()
          Starting with the first Token in the input stream, try to derive a parse tree for the entire program.
private  void parseBoolExpr()
          Parse non-terminal: boolExpr.
private  void parseCallEnd(Token t)
          Parse non-terminal: callEnd
private  void parseExpr()
          Parse non-terminal: expr.
private  void parseExprList()
          Parse non-terminal: exprList.
private  void parseExprListTail()
          Parse non-terminal: exprListTail.
private  void parseExprTail()
          Parse non-terminal: exprTail.
private  void parseFactor()
          Parse non-terminal: factor.
private  void parseMovieBody()
          Parse non-terminal: movieBody.
private  void parsePageBlock()
          Parse non-terminal: pageBlock.
private  void parsePageBlocks()
          Parse non-terminal: pageBlocks.
private  void parsePageBlocksTail()
          Parse non-terminal: pageBlocksTail.
private  void parsePageStatement()
          Parse non-terminal: pageStatement.
private  void parsePageStatements()
          Parse non-terminal: pageStatements.
private  void parsePageTail()
          Parse non-terminal: pageTail.
private  void parseProgram()
          Parse non-terminal: program.
private  void parsePrologBlock()
          Parse non-terminal: prologBlock.
private  void parsePrologStatement()
          Parse non-terminal: prologStatement.
private  void parsePrologStatements()
          Parse non-terminal: prologStatements.
private  void parsePrologTail()
          Parse non-terminal: prologTail.
private  void parseRelExpr()
          Parse non-terminal: relExpr.
private  void parseTerm()
          Parse non-terminal: term.
private  void parseTermTail()
          Parse non-terminal: termTail.
private  void parseVariableDeclaration()
          Parse non-terminal: variableDeclaration.
private  void processIOException(java.io.IOException e)
          Handle the reporting and processing required by an IO error.
private  void processSyntaxException(SyntaxException e)
          Handle the reporting and processing required by a parse error.
 void setShowCode(boolean b)
          Mutator method to set the state of code generation
 void setShowMethods(boolean b)
          Mutator method to set the state of method entry/exit tracing.
 void setShowSymbols(boolean b)
          Mutator method to set the state of Symbol entry display.
private  void traceEntry()
          Optionally print a message that we are entering a method.
private  void traceEntry(java.lang.String s)
          Optionally print a message that we are entering a method.
private  void traceExit()
          Optionally print a message that we are exiting a method.
private  void traceExit(java.lang.String s)
          Optionally print a message that we are exiting a method.
private  void traceSymbol(Symbol sym)
          Optionally display a Symbol
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

theToken

private Token theToken
theToken is always the Token that we should look at next. When a parse method returns, theToken contains a reference to the token that follows the end of the non-terminal that was just parsed.


prevToken

private Token prevToken
prevToken is always the last Token that has been matched. This means that when matchToken succeeds, prevToken contains a reference to the Token that was just matched.


cio

private CompilerIO cio
The CompilerIO object that we can use to write on the output stream if needed.


scan

private Scanner scan
The Scanner that actually reads the input stream and reports the tokens that it finds.


status

private boolean status
This status variable is set to true before the parse begins. If any of the parse methods encounter a problem, they set this variable to false. After the parse is complete, the status is returned to the caller.


tracer

private java.lang.Throwable tracer
This Throwable is used to get stack trace information


showMethods

private boolean showMethods
Enable / disable method entry/exit tracing.


showSymbols

private boolean showSymbols
Enable / disable Symbol entry display.


showCode

private boolean showCode
Enable / disable code generation.


firstSets

private java.util.HashMap firstSets
HashMap containing int arrays of Token types, keyed by the non-terminal whose FIRST set each array represents.


symbolTable

private SymbolTable symbolTable
Symbol table manager.


SC_VARIABLE

private static final int SC_VARIABLE
Define the symbol tables that we use in the parser. This number is the symbol table index.

See Also:
Constant Field Values
Constructor Detail

Parser

public Parser(CompilerIO io,
              SymbolTable t,
              Scanner s)
Construct a new Parser object. Remember the CompilerIO object so that we can use it for output in the parser. Remember the SymbolTable object so that we can use it to store information about program symbols. Remember the Scanner object that supplies us with a Token stream to parse. Initialize the HashMap firstSets to hold int[] objects containing the Token identifier values for the FIRST sets of various non-terminals. The name of the associated non-terminal is used as the key. The pageNumber variable is declared and initialized to 0 in the SymbolTable.

Parameters:
io - the CompilerIO object to use in reading and writing files.
t - the SymbolTable manager. Table 0 is the reserved words table, table 1 is used for program symbols.
s - the lexical scanner that actually reads the input file
Method Detail

parse

public boolean parse()
Starting with the first Token in the input stream, try to derive a parse tree for the entire program.

Returns:
true if parsed correctly, false if error

parseProgram

private void parseProgram()
Parse non-terminal: program.
 program ::= movie name { movieBody } EOF
 


parseMovieBody

private void parseMovieBody()
Parse non-terminal: movieBody.
 movieBody ::= prologBlock pageBlocks
 movieBody ::= pageBlocks
 


parsePrologBlock

private void parsePrologBlock()
Parse non-terminal: prologBlock.
 prologBlock ::= prolog { prologStatements }
 


parsePrologStatements

private void parsePrologStatements()
Parse non-terminal: prologStatements.
 prologStatements ::= prologStatement prologTail
 


parsePrologTail

private void parsePrologTail()
Parse non-terminal: prologTail.
 prologTail ::= prologStatement prologTail
 prologTail ::= epsilon
 


parsePrologStatement

private void parsePrologStatement()
Parse non-terminal: prologStatement.
 prologStatement ::= variableDeclaration
 


parseVariableDeclaration

private void parseVariableDeclaration()
Parse non-terminal: variableDeclaration.
 variableDeclaration ::= id : type ( );
 variableDeclaration ::= id : type ( exprList );
 


parsePageBlocks

private void parsePageBlocks()
Parse non-terminal: pageBlocks.
 pageBlocks ::= pageBlock pageBlocksTail
 


parsePageBlocksTail

private void parsePageBlocksTail()
Parse non-terminal: pageBlocksTail.
 pageBlocksTail ::= pageBlock pageBlocksTail
 pageBlocksTail ::= epsilon
 


parsePageBlock

private void parsePageBlock()
Parse non-terminal: pageBlock.
 pageBlock ::= show ( integer ) { pageStatements }
 


parsePageStatements

private void parsePageStatements()
Parse non-terminal: pageStatements.
 pageStatements ::= pageStatement pageTail
 


parsePageTail

private void parsePageTail()
Parse non-terminal: pageTail.
 pageTail ::= pageStatement pageTail
 pageTail ::= epsilon
 


parsePageStatement

private void parsePageStatement()
Parse non-terminal: pageStatement.
 pageStatement::= { pageStatements }
 pageStatement::= expr;
 pageStatement::= id = expr;
 pageStatement::= if (boolExpr) pageStatement
 pageStatement::= if (boolExpr) pageStatement else pageStatement
 


parseExpr

private void parseExpr()
Parse non-terminal: expr.
 expr ::= term exprTail
 


parseExprTail

private void parseExprTail()
Parse non-terminal: exprTail.
 exprTail ::= + term exprTail
 exprTail ::= - term exprTail
 exprTail ::= epsilon
 


parseTerm

private void parseTerm()
Parse non-terminal: term.
 term ::= factor termTail
 


parseTermTail

private void parseTermTail()
Parse non-terminal: termTail.
 termTail ::= * factor termTail
 termTail ::= / factor termTail
 termTail ::= epsilon
 


parseExprList

private void parseExprList()
Parse non-terminal: exprList.
 exprList ::= expr exprListTail
 


parseExprListTail

private void parseExprListTail()
Parse non-terminal: exprListTail.
 exprListTail ::= , expr exprListTail
 exprListTail ::= epsilon
 


parseFactor

private void parseFactor()
Parse non-terminal: factor.
 factor ::= integer
 factor ::= real
 factor ::= ( expr )
 factor ::= id
 factor ::= id()
 factor ::= id(exprList)
 factor ::= id.id()
 factor ::= id.id(exprList)
 


parseCallEnd

private void parseCallEnd(Token t)
Parse non-terminal: callEnd

Parameters:
t - the Token.ID that started the methodCall
 callEnd ::= () | (exprList) | .id() | .id(exprList)
 

parseBoolExpr

private void parseBoolExpr()
Parse non-terminal: boolExpr.
 boolExpr ::= relExpr
 boolExpr ::= !(relExpr)
 


parseRelExpr

private void parseRelExpr()
Parse non-terminal: relExpr.
 relExpr ::= expr == expr
 relExpr ::= expr > expr
 relExpr ::= expr < expr
 


isFirst

private boolean isFirst(Token t,
                        java.lang.String nonterm)
Check to see if tokens of the given type are in the FIRST set of the given non-terminal.

Parameters:
t - the Token to check to see if its type is in the FIRST set
nonterm - the String name of the non-terminal whose FIRST set is to be checked
Returns:
true if the given Token can be the first Token in a string generated by this non-terminal, false if it cannot be the first Token.

matchToken

private void matchToken(int type)
                 throws SyntaxException
Check that the current Token is of the expected type, and advance past it if it is okay.

Parameters:
type - the type of the Token, as defined by the class variables in Token.
Throws:
SyntaxException - if theToken is not of the expected type.

matchTokenArray

private void matchTokenArray(int[] type)
                      throws SyntaxException
Check that the current Token is one of the expected types, and advance past it if it is okay.

Parameters:
type - an array of valid Token types, as defined by the class variables in Token.
Throws:
SyntaxException - if theToken is not one of the expected types.

processSyntaxException

private void processSyntaxException(SyntaxException e)
Handle the reporting and processing required by a parse error. This method uses the Throwable method getStackTrace to get information about the call stack and extracts the name of the calling method. This capability is not available in versions of Java prior to 1.4.

Parameters:
e - the SyntaxException

processIOException

private void processIOException(java.io.IOException e)
Handle the reporting and processing required by an IO error. This method uses the Throwable method getStackTrace to get information about the call stack and extracts the name of the calling method. This capability is not available in versions of Java prior to 1.4.

Parameters:
e - the SyntaxException

setShowMethods

public void setShowMethods(boolean b)
Mutator method to set the state of method entry/exit tracing.

Parameters:
b - true if trace output is desired, false if not

setShowSymbols

public void setShowSymbols(boolean b)
Mutator method to set the state of Symbol entry display.

Parameters:
b - true if Symbol output is desired, false if not

setShowCode

public void setShowCode(boolean b)
Mutator method to set the state of code generation

Parameters:
b - true if code should be generated, false if not.

generateCode

private void generateCode(java.lang.String code)
Emit a code string.

Parameters:
code - the string to emit if showCode is true.

generateMovieStart

private void generateMovieStart(java.lang.String title)
Format and emit the postscript title block section

Parameters:
title - the title of this movie

generateMovieEnd

private void generateMovieEnd()
Format and emit the postscript trailer


generatePrologStart

private void generatePrologStart()
Emit the beginning of the postscript prolog section


generatePrologEnd

private void generatePrologEnd()
Emit the end of the postscript prolog section


traceEntry

private void traceEntry()
Optionally print a message that we are entering a method. This method uses the Throwable method getStackTrace to get information about the call stack and extracts the name of the calling method. This capability is not available in versions of Java prior to 1.4. This method assumes that there is an instance variable named "tracer" that has been initialized with a reference to a Throwable object.


traceEntry

private void traceEntry(java.lang.String s)
Optionally print a message that we are entering a method.

Parameters:
s - the name of the method we are entering

traceExit

private void traceExit()
Optionally print a message that we are exiting a method. This method uses the Throwable method getStackTrace to get information about the call stack and extracts the name of the calling method. This capability is not available in versions of Java prior to 1.4. This method assumes that there is an instance variable named "tracer" that has been initialized with a reference to a Throwable object.


traceExit

private void traceExit(java.lang.String s)
Optionally print a message that we are exiting a method.

Parameters:
s - the name of the method we are exiting

traceSymbol

private void traceSymbol(Symbol sym)
Optionally display a Symbol

Parameters:
sym - the Symbol to display

operator

private java.lang.String operator(Token t)
Convert an operator token into the appropriate postscript name

Parameters:
t - the Token containing the operator
Returns:
the equivalent postscript operator