Parser Engine Code Skeleton: C++

 

The following contains the essential C++ code needed to interact with the GOLDParser object. This documentation also contains the code for:

 

//C++ Source Code

GOLDParser parser = new GOLDParser();
GPMessageConstants response;
bool done;

parser.LoadCompiledGrammar ("grammar.cgt");
parser.OpenFile ("Program.txt");
done = FALSE;

while (!done)
{
   response = parser.Parse();

   switch(response)
   {
      case gpMsgLexicalError:
         /* Place code here to handle a illegal or unrecognized token
            To recover, pop the token from the stack: Parser.PopInputToken */

         break;

      case gpMsgSyntaxError:
         /* This is a syntax error: the source has produced a token that was
            not expected by the LALR State Machine. The expected tokens are stored
            into the Tokens() list. To recover, push one of the
            expected tokens onto the parser's input queue (the first in this case):
            You should limit the number of times this type of recovery can take
            place. */
         Parser.PushInputToken(Parser.Tokens(0));
         break;

      case gpMsgReduction: 
         /* This message is returned when a rule was reduced by the parse engine.
            The CurrentReduction property is assigned a Reduction object
            containing the rule and its related tokens. You can reassign this
            property to your own customized class. If this is not the case,
            this message can be ignored and the Reduction object will be used
            to store the parse tree.  */

         switch(Parser.CurrentReduction.ParentRule.TableIndex)
         {
            ...
         }

         Parser.CurrentReduction = //Object you created to store the rule
         break;

      case gpMsgAccept:
         /* The program was accepted by the parsing engine */

         done = TRUE;
         break;

      case gpMsgCommentError:
         /* The end of the input was reached while reading a comment.
            This is caused by a comment that was not terminated */

         break;

      case gpMsgTokenRead:
         /* A token was read by the parser. The Token Object can be accessed through
            the CurrentToken() property:  Parser.CurrentToken */
         break;

      case gpMsgInternalError:
         /* Something horrid happened inside the parser. You cannot recover */
         done = TRUE;
         break;

      case gpMsgNotLoadedError:
         /* Load the Compiled Grammar Table file first. */
         done = TRUE;
         break;

      case gpMsgCommentBlockRead:
         /* A block comment was read by the parser. The content of the comment
            is stored in the CurrentComment() property */

         break;

      case gpMsgCommentLineRead:
         /* A block comment was read by the parser. The content of the comment
            is stored in the CurrentComment() property */

         break;
      }

}  //while