goldengine.java
Class GOLDParser


java.lang.Object

  |

  +--goldengine.java.GOLDParser

All Implemented Interfaces:
ActionConstants, GPMessageConstants, ParseResultConstants, RecordIDConstants, SymbolTypeConstants

public class GOLDParser
extends java.lang.Object
implements GPMessageConstants, SymbolTypeConstants, ActionConstants, RecordIDConstants, ParseResultConstants

-------------------------------------------------------------------------------------------
Source File: GenericParser.java
Author: Devin Cook, Matthew Hawkins
Description: The main parsing engine.
-------------------------------------------------------------------------------------------
Revision List

      Author          Version         Description
      ------          -------         -----------
      MPH             1.0             First Issue
      MPH             1.1             Added the TrimReductions property and required logic
      MPH             1.2             Fixed Comment bug to ignore all tokens

-------------------------------------------------------------------------------------------
IMPORT: java.io, java.util
-------------------------------------------------------------------------------------------


Fields inherited from interface goldengine.java.GPMessageConstants
gpMsgAccept, gpMsgCommentError, gpMsgInternalError, gpMsgLexicalError, gpMsgNotLoadedError, gpMsgReduction, gpMsgSyntaxError, gpMsgTokenRead
 
Fields inherited from interface goldengine.java.SymbolTypeConstants
symbolTypeCommentEnd, symbolTypeCommentLine, symbolTypeCommentStart, symbolTypeEnd, symbolTypeError, symbolTypeNonterminal, symbolTypeTerminal, symbolTypeWhitespace
 
Fields inherited from interface goldengine.java.ActionConstants
actionAccept, actionError, actionGoto, actionReduce, actionShift
 
Fields inherited from interface goldengine.java.RecordIDConstants
recordIdCharSets, recordIdComment, recordIdDFAStates, recordIdInitial, recordIdLRTables, recordIdParameters, recordIdRules, recordIdSymbols, recordIdTableCounts
 
Fields inherited from interface goldengine.java.ParseResultConstants
parseResultAccept, parseResultInternalError, parseResultReduceEliminated, parseResultReduceNormal, parseResultShift, parseResultSyntaxError
 
Constructor Summary
GOLDParser()
          GOLDParser The constructor initiates some variables.
 
Method Summary
 void clear()
          clear This method clears every value in the parser engine.
 void closeFile()
          closeFile This method will close the source file.
 int currentLineNumber()
          currentLineNumber Returns the current source file line number.
 Reduction currentReduction()
          currentReduction This method will return the current reduction.
 Token currentToken()
          currentToken This method returns the current Token.
 Token getToken(int index)
          getToken If you require access to tokens in the stack before they are placed on the parse tree.
 boolean getTrimReductions()
          getTrimReductions #ver1.1# This method will get whether or not we should trim the reductions.
 boolean loadCompiledGrammar(java.lang.String fileName)
          loadCompiledGrammar This method will reset the GOLDParser engine before loading a new .cgt file into it.
 boolean openFile(java.lang.String fileName)
          openFile This method will open the source file for reading.
 java.lang.String parameter(java.lang.String name)
          parameter This method will return the value of a parameter that corresponds to the name passed in.
 int parse()
          parse Will parse a token.
 Token popInputToken()
          popInputToken This method should only be called if there is a lexical error and you need to pop an unexpected token out of the stack.
 void pushInputToken(Token theToken)
          pushInputToken This method should only be used if there is a syntax error.
 void reset()
          reset This method will reset the parser engine.
 int ruleTableCount()
          ruleTableCount This method returns the total number of rules in the rule table.
 Rule ruleTableEntry(int index)
          ruleTableEntry This method will return a Rule at the specified index.
 void setCurrentReduction(Reduction value)
          setCurrentReduction This method will set the current reduction to the one passed in.
 void setTrimReductions(boolean value)
          setTrimReductions #ver1.1# This method will set the whether or not the program should trim the reductions.
 int symbolTableCount()
          symbolTableCount This method returns the total number of symbols in the symbol table.
 Symbol symbolTableEntry(int index)
          symbolTableEntry This method will return a Symbol at the specified index.
 int tokenCount()
          tokenCount This method will return the number of tokens in the stack.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

GOLDParser


public GOLDParser()
GOLDParser The constructor initiates some variables.
Method Detail

currentLineNumber


public int currentLineNumber()
currentLineNumber Returns the current source file line number.
Returns:
The current source file line number

closeFile


public void closeFile()
               throws ParserException
closeFile This method will close the source file.
Throws:
ParserException - The engine parser should deal with a problem with closing the source file.

currentToken


public Token currentToken()
currentToken This method returns the current Token. The current token is the last token read by "retrieveToken".
Returns:
The current Token.

popInputToken


public Token popInputToken()
popInputToken This method should only be called if there is a lexical error and you need to pop an unexpected token out of the stack.
Returns:
The token at the top of the stack.

pushInputToken


public void pushInputToken(Token theToken)
pushInputToken This method should only be used if there is a syntax error. It will push a Token onto the top of the stack so that the parser might have a chance of finding an correctly typed token.
Parameters:
theToken - A token to push onto the stack.

getToken


public Token getToken(int index)
getToken If you require access to tokens in the stack before they are placed on the parse tree. Enter an index number and the Token will be returned, if and only if the index number is valid.
Parameters:
index - The index number.
Returns:
The Token at the index specified.

currentReduction


public Reduction currentReduction()
currentReduction This method will return the current reduction. This will only happen if the parsing engine has performed a reduction. If it has, then the reduction passed back will be the last one performed.
Returns:
The last Reduction performed.

setCurrentReduction


public void setCurrentReduction(Reduction value)
setCurrentReduction This method will set the current reduction to the one passed in.
Parameters:
value - The new Reduction to set as the current one.

setTrimReductions


public void setTrimReductions(boolean value)
setTrimReductions #ver1.1# This method will set the whether or not the program should trim the reductions.
Parameters:
value - True if we should trim reductions, false if not.

getTrimReductions


public boolean getTrimReductions()
getTrimReductions #ver1.1# This method will get whether or not we should trim the reductions.
Returns:
True if we should trim reductions, false if not.

parameter


public java.lang.String parameter(java.lang.String name)
parameter This method will return the value of a parameter that corresponds to the name passed in.
Parameters:
name - The name of the variable.
Returns:
The value of the variable.

symbolTableCount


public int symbolTableCount()
symbolTableCount This method returns the total number of symbols in the symbol table.
Returns:
The number of symbols in the table.

ruleTableCount


public int ruleTableCount()
ruleTableCount This method returns the total number of rules in the rule table.
Returns:
The number of rules in the table.

symbolTableEntry


public Symbol symbolTableEntry(int index)
symbolTableEntry This method will return a Symbol at the specified index.
Parameters:
index - The index number in the table.
Returns:
The Symbol at the specified index.

ruleTableEntry


public Rule ruleTableEntry(int index)
ruleTableEntry This method will return a Rule at the specified index.
Parameters:
index - The index number in the table.
Returns:
The Rule at the specified index.

tokenCount


public int tokenCount()
tokenCount This method will return the number of tokens in the stack.
Returns:
The number of tokens in the stack.

loadCompiledGrammar


public boolean loadCompiledGrammar(java.lang.String fileName)
                            throws ParserException
loadCompiledGrammar This method will reset the GOLDParser engine before loading a new .cgt file into it.
Parameters:
fileName - The absolute path of the .cgt file to load.
Returns:
True if the .cgt was loaded, false if not.
Throws:
ParserException - If there was a stream access problem with the file.

openFile


public boolean openFile(java.lang.String fileName)
                 throws ParserException
openFile This method will open the source file for reading. It will also reset all information from previous source files.
Parameters:
fileName - The absolute path to the source file.
Returns:
True if the file was successfully opened, false if not.
Throws:
ParserException - If there was a problem opening the file.

clear


public void clear()
clear This method clears every value in the parser engine.

reset


public void reset()
reset This method will reset the parser engine. It initalises the Error and Type End symbols, and then clears all the stacks of any tokens.

parse


public int parse()
          throws ParserException
parse Will parse a token. 1. If the tables are not setup then report GPM_NotLoadedError
2. If parser is in comment mode then read tokens until a recognized one is found and report it
3. Otherwise, parser normal
a. If there are no tokens on the stack 1) Read one and trap error 2) End function with GPM_TokenRead b. Otherwise, call ParseToken with the top of the stack. 1) If success, then Pop the value 2) Loop if the token was shifted (nothing to report)
Returns:
The result of one parse of the source file. The integer could be one of the constants defined in the interface GPMessageConstants.
Throws:
ParserException - This is thrown if there are any problems reading information from the source file.