Parse Function

 

function Parse() returns Message-Constant

Parameters:

None

Returns:

Message-Constant.. This is sent to the application.

1. If the tables were not loaded then report NOT-LOADED-ERROR
2. If parser is in "Comment Mode" then read tokens until Comment Mode ends.
3. Otherwise, parser normal

a. If there are no tokens on the stack

1) Read one and trap the error
2) End function with GPM_TokenRead

b. Otherwise, call ParseToken with the top of the stack.

1) If success, then dequeue the value
2) Loop if the token was shifted (nothing to report)

If the parse tables were not loaded

Set Result = Not-Loaded-Error

else

Set Done = False.
Loop until Done

If the Input-Stack is empty

We need to read a token from the Lexer. If the token is a normal terminal (not a comment line, comment start, comment end or whitespace) AND the system is not in comment mode, then we can send a Token-Read message ot the user.

Set Token = LexerGetToken().
Push the Token onto the Input-Stack.

If the Comment-Stack is empty and the Token is a normal terminal

Set Result = Token-Read.
Set Done = True.

end if

else if Comment-Level > 0

COMMENT MODE. The system checks for the Comment Start and Comment End terminals and ignores the rest.

Pop a Token from the front of the Input-Stack.

case the Kind of Token is

Comment-Start:

Increment the Comment-Level.

Comment-End:

Decrement the Comment-Level.

End-Of-File:

At this point, the end of the input was reached while the system is still in comment mode. There is a run away block comment.

Set Result = Message-Comment-Error.
Set Result = True.

otherwise:

Do nothing. The "comment line" symbol is ignored as well.

end case

else

NORMAL MODE. The system is in normal parse mode and tokens are available in the Token-Queue. At this point, the ParseToken() function is called. Note: the Token is NOT always removed from the Token-Queue. Whether it is discarded depends on the logic below.

Set Token = the token on the front of the Input-Stack.

case the Kind of Token is

Whitespace:

Ignore. Whitespace is discarded.

Discard the token from the front of the Input-Stack.

Comment-Start:

The Comment-Level value is incremented from 0 to 1.

Set Comment-Level = 1.
Discard the token from the front of the Token-Queue.

Comment-Line:

When a comment line token is read, the system should remove all rest of the current line. Do not discard the carriage-return or line-feed. These are required by line-based grammars.

Discard the rest of the current line in Source.   
Discard the token from the front of the Input-Stack.

Error:

Set Result = Message-Lexical-Error.
Set Done = True.

otherwise:

Finally, we can parse the token.

Set Parse-Result = ParseToken ( Token ).

case the Parse-Result is

Accept:

Set Result = Accept.
Set Done = True.

Internal-Error:

Set Result = Internal-Error.
Set Done = True.

Reduce:

Set Result = Reduction.
Set Done = True.

Shift:

ParseToken() shifted the token on the front of the Token-Queue. It now exists on the Token-Stack and must be eliminated from the queue.

Discard the token from the front of the Input-Stack.

Syntax-Error:

Set Result = Message-Syntax-Error.
Set Done = True.

otherwise:

Do nothing. This includes Shift and Trim-Reduced.

end case

end case

Normal mode block is complete

end if

next loop

end if

Return the Result.

end function