Parser Engine Code Skeleton: Visual Basic 6
The following contains the essential Visual Basic 6 code needed to interact with the
GOLDParser object. This documentation also contains the code for:
'Visual Basic
6 source code
Dim Parser As New GOLDParser
Dim Response As
GPMessageConstants
Dim Done As Boolean
Parser.LoadCompiledGrammar "grammar.cgt"
Parser.OpenFile "Program.txt"
Done = False
Do Until Done
Response = Parser.Parse()
Select Case Response
Case
gpMsgLexicalError
'Place code here to handle a illegal or unrecognized token
'To recover, pop the token from the stack: Parser.PopInputToken
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):
Parser.PushInputToken Parser.Tokens(0)
'You should limit the number of times this type of recovery can take
'place.
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.
Select Case Parser.CurrentReduction.ParentRule.TableIndex
...
End Select
Set Parser.CurrentReduction = 'Object you
created to store the rule
Case gpMsgAccept
'The program was accepted by the parsing engine.
Done = True
Case gpMsgTokenRead
'A token was read by the parser. The Token Object can be accessed through
'the CurrentToken() property: Parser.CurrentToken
Case
gpMsgCommentError
'The end of the input was reached while reading a comment.
'This is caused by a comment that was not terminated.
Case gpMsgInternalError
'Something horrid happened inside the parser. You cannot recover.
Done = True
Case
gpMsgNotLoadedError
'Load the Compiled Grammar Table file first.
Done = True
Case gpMsgCommentBlockRead
'A block comment was read by the parser. The content of the comment
'is stored in the CurrentComment() property
Case gpMsgCommentLineRead
'A line comment was read by the parser. The content of the comment
'is stored in the CurrentComment() property
End Select
Loop
|