|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectParser
public class Parser
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 |
---|
private Token theToken
private Token prevToken
private CompilerIO cio
private Scanner scan
private boolean status
private java.lang.Throwable tracer
private boolean showMethods
private boolean showSymbols
private boolean showCode
private java.util.HashMap firstSets
private SymbolTable symbolTable
private static final int SC_VARIABLE
Constructor Detail |
---|
public Parser(CompilerIO io, SymbolTable t, Scanner s)
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 fileMethod Detail |
---|
public boolean parse()
private void parseProgram()
program ::= movie name { movieBody } EOF
private void parseMovieBody()
movieBody ::= prologBlock pageBlocks movieBody ::= pageBlocks
private void parsePrologBlock()
prologBlock ::= prolog { prologStatements }
private void parsePrologStatements()
prologStatements ::= prologStatement prologTail
private void parsePrologTail()
prologTail ::= prologStatement prologTail prologTail ::= epsilon
private void parsePrologStatement()
prologStatement ::= variableDeclaration
private void parseVariableDeclaration()
variableDeclaration ::= id : type ( ); variableDeclaration ::= id : type ( exprList );
private void parsePageBlocks()
pageBlocks ::= pageBlock pageBlocksTail
private void parsePageBlocksTail()
pageBlocksTail ::= pageBlock pageBlocksTail pageBlocksTail ::= epsilon
private void parsePageBlock()
pageBlock ::= show ( integer ) { pageStatements }
private void parsePageStatements()
pageStatements ::= pageStatement pageTail
private void parsePageTail()
pageTail ::= pageStatement pageTail pageTail ::= epsilon
private void parsePageStatement()
pageStatement::= { pageStatements } pageStatement::= expr; pageStatement::= id = expr; pageStatement::= if (boolExpr) pageStatement pageStatement::= if (boolExpr) pageStatement else pageStatement
private void parseExpr()
expr ::= term exprTail
private void parseExprTail()
exprTail ::= + term exprTail exprTail ::= - term exprTail exprTail ::= epsilon
private void parseTerm()
term ::= factor termTail
private void parseTermTail()
termTail ::= * factor termTail termTail ::= / factor termTail termTail ::= epsilon
private void parseExprList()
exprList ::= expr exprListTail
private void parseExprListTail()
exprListTail ::= , expr exprListTail exprListTail ::= epsilon
private void parseFactor()
factor ::= integer factor ::= real factor ::= ( expr ) factor ::= id factor ::= id() factor ::= id(exprList) factor ::= id.id() factor ::= id.id(exprList)
private void parseCallEnd(Token t)
t
- the Token.ID that started the methodCall
callEnd ::= () | (exprList) | .id() | .id(exprList)
private void parseBoolExpr()
boolExpr ::= relExpr boolExpr ::= !(relExpr)
private void parseRelExpr()
relExpr ::= expr == expr relExpr ::= expr > expr relExpr ::= expr < expr
private boolean isFirst(Token t, java.lang.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 SyntaxExceptionprivate void processIOException(java.io.IOException 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 notpublic void setShowCode(boolean b)
b
- true if code should be generated, false if not.private void generateCode(java.lang.String code)
code
- the string to emit if showCode is true.private void generateMovieStart(java.lang.String title)
title
- the title of this movieprivate void generateMovieEnd()
private void generatePrologStart()
private void generatePrologEnd()
private void traceEntry()
private void traceEntry(java.lang.String s)
s
- the name of the method we are enteringprivate void traceExit()
private void traceExit(java.lang.String s)
s
- the name of the method we are exitingprivate void traceSymbol(Symbol sym)
sym
- the Symbol to displayprivate java.lang.String operator(Token t)
t
- the Token containing the operator
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |