Class Parser

java.lang.Object
  |
  +--Parser

public class Parser
extends Object

Parse an input stream containing a D language source file. Implements a recursive descent predictive parser according to the D language grammar.


Field Summary
private  CompilerIO cio
          The CompilerIO object that we can use to write on the output stream if needed.
private  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  Scanner scan
          The Scanner that actually reads the input stream and reports the tokens that it finds.
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  Token theToken
          theToken is always the Token that we should look at next.
private  Throwable tracer
          This Throwable is used to get stack trace information
 
Constructor Summary
Parser(CompilerIO io)
          Construct a new Parser object.
 
Method Summary
private  boolean isFirst(Token t, 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.
 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 parseDeclaration()
          Parse non-terminal: declaration.
private  void parseDeclarations()
          Parse non-terminal: declarations.
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 parseFunctionDefinition()
          Parse non-terminal: functionDefinition.
private  void parseParameter()
          Parse non-terminal: parameter.
private  void parseParameters()
          Parse non-terminal: parameters.
private  void parseParametersTail()
          Parse non-terminal: parametersTail.
private  void parseProgram()
          Parse non-terminal: program.
private  void parseProgramTail()
          Parse non-terminal: programTail.
private  void parseRelExpr()
          Parse non-terminal: relExpr.
private  void parseStatement()
          Parse non-terminal: statement.
private  void parseStatements()
          Parse non-terminal: statements.
private  void parseStatementsTail()
          Parse non-terminal: statementsTail.
private  void parseTerm()
          Parse non-terminal: term.
private  void parseTermTail()
          Parse non-terminal: termTail.
private  void processSyntaxException(SyntaxException e)
          Handle the reporting and processing required by a parse error.
 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(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(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 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.


firstSets

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

Constructor Detail

Parser

public Parser(CompilerIO io)
Construct a new Parser object. Remember the CompilerIO object so that we can use it for output in the parser. Create a new Scanner and remember it so that we can scan the input stream. 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.

Parameters:
io - the CompilerIO object to use in reading and writing files.
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.
 15. program ::= functionDefinition programTail EOF
 


parseProgramTail

private void parseProgramTail()
Parse non-terminal: programTail.
 16.1 programTail ::= functionDefinition programTail
 16.2 programTail ::= epsilon
 


parseFunctionDefinition

private void parseFunctionDefinition()
Parse non-terminal: functionDefinition.
 2.1 functionDefinition ::= int id() { statements }
 2.2 functionDefinition ::= int id(parameters) { statements }
 2.3 functionDefinition ::= int id() { declarations statements }
 2.4 functionDefinition ::= int id(parameters) { declarations statements }
 


parseParameters

private void parseParameters()
Parse non-terminal: parameters.
 17. parameters ::= parameter parametersTail
 


parseParameter

private void parseParameter()
Parse non-terminal: parameter.
 4. parameter ::= int id
 


parseParametersTail

private void parseParametersTail()
Parse non-terminal: parametersTail.
 18.1 parametersTail ::= , parameter parametersTail
 18.2 parametersTail ::= epsilon
 


parseDeclarations

private void parseDeclarations()
Parse non-terminal: declarations.
 xx declarations ::= declaration (declaration)*
 


parseDeclaration

private void parseDeclaration()
Parse non-terminal: declaration.
 6. declaration ::= int id ;
 


parseStatements

private void parseStatements()
Parse non-terminal: statements.
 21. statements ::= statement statementsTail
 


parseStatementsTail

private void parseStatementsTail()
Parse non-terminal: statementsTail.
 22.1 statementsTail ::= statement statementsTail
 22.2 statementsTail ::= epsilon
 


parseStatement

private void parseStatement()
Parse non-terminal: statement.
 8.1 statement ::= { statements }
 8.2 statement ::= id = expr;
 8.3 statement ::= if (boolExpr) statement
 8.4 statement ::= if (boolExpr) statement else statement
 8.5 statement ::= while (boolExpr) statement
 8.6 statement ::= return expr;
 


parseExpr

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


parseExprTail

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


parseTerm

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


parseTermTail

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


parseExprList

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


parseExprListTail

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


parseFactor

private void parseFactor()
Parse non-terminal: factor.
 11.1 factor ::= int
 11.2 factor ::= ( expr )
 11.3 factor ::= id
 11.4 factor ::= id()
 11.5 factor ::= id(exprList)
 


parseBoolExpr

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


parseRelExpr

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


isFirst

private boolean isFirst(Token t,
                        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

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

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(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(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