|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--Parser
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 |
private Token theToken
private Token prevToken
private CompilerIO cio
private Scanner scan
private boolean status
private Throwable tracer
private boolean showMethods
private boolean showSymbols
private HashMap firstSets
Constructor Detail |
public Parser(CompilerIO io)
io
- the CompilerIO object to use in reading and writing files.Method Detail |
public boolean parse()
private void parseProgram()
15. program ::= functionDefinition programTail EOF
private void parseProgramTail()
16.1 programTail ::= functionDefinition programTail 16.2 programTail ::= epsilon
private void parseFunctionDefinition()
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 }
private void parseParameters()
17. parameters ::= parameter parametersTail
private void parseParameter()
4. parameter ::= int id
private void parseParametersTail()
18.1 parametersTail ::= , parameter parametersTail 18.2 parametersTail ::= epsilon
private void parseDeclarations()
xx declarations ::= declaration (declaration)*
private void parseDeclaration()
6. declaration ::= int id ;
private void parseStatements()
21. statements ::= statement statementsTail
private void parseStatementsTail()
22.1 statementsTail ::= statement statementsTail 22.2 statementsTail ::= epsilon
private void parseStatement()
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;
private void parseExpr()
23. expr ::= term exprTail
private void parseExprTail()
24.1 exprTail ::= + term exprTail 24.2 exprTail ::= - term exprTail 24.3 exprTail ::= epsilon
private void parseTerm()
25. term ::= factor termTail
private void parseTermTail()
26.1 termTail ::= * factor termTail 26.2 termTail ::= epsilon
private void parseExprList()
27. exprList ::= expr exprListTail
private void parseExprListTail()
28.1 exprListTail ::= , expr exprListTail 28.2 exprListTail ::= epsilon
private void parseFactor()
11.1 factor ::= int 11.2 factor ::= ( expr ) 11.3 factor ::= id 11.4 factor ::= id() 11.5 factor ::= id(exprList)
private void parseBoolExpr()
13.1 boolExpr ::= relExpr 13.2 boolExpr ::= !(relExpr)
private void parseRelExpr()
14.1 relExpr ::= expr==expr 14.2 relExpr ::= expr>expr
private boolean isFirst(Token t, String nonterm)
t
- the Token to check to see if its type is in the FIRST setnonterm
- the String name of the non-terminal whose FIRST set
is to be checked
private void matchToken(int type) throws SyntaxException
type
- the type of the Token, as defined by the class
variables in Token.
SyntaxException
- if theToken is not of the expected
type.private void matchTokenArray(int[] type) throws SyntaxException
type
- an array of valid Token types, as defined
by the class variables in Token.
SyntaxException
- if theToken is not one of the expected
types.private void processSyntaxException(SyntaxException e)
e
- the SyntaxExceptionpublic void setShowMethods(boolean b)
b
- true if trace output is desired, false if notpublic void setShowSymbols(boolean b)
b
- true if Symbol output is desired, false if notprivate void traceEntry()
private void traceEntry(String s)
s
- the name of the method we are enteringprivate void traceExit()
private void traceExit(String s)
s
- the name of the method we are exitingprivate void traceSymbol(Symbol sym)
sym
- the Symbol to display
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |