You will be implementing the Parse operation as an extension to the Program component (see the Program section in the class notes for the specs). Your implementation must use a recursive descent algorithm based on the context-free grammar for BL programs included at the end of this handout. Note, however, that the non-terminal symbols, <program> and <new_instruction>, are not defined recursively in terms of themselves or of each other; therefore, recursion is not called for here in part 1. The non-terminal <program> is defined in terms of <new_instruction>.
In addition, your implementation must check for syntax errors. This can be done by using the assert statement to notify users of syntax errors, just as the assert statement is used to notify users of precondition violations in checking versions of components. The assert statement has two parameters: the condition to be tested and the error message to be displayed if the condition is false. For example,
assert ((x < 0), "Expecting negative integer.")
For additional examples of how to incorporate an assert statement, look at CT/XYZ/Kernel_C.h in the RESOLVE_Catalog, for a general specification, or at any of the checking versions of specific RESOLVE components.
Note: in implementing the Parse operation for Program in Part 1 of this lab assignment, you will use the Parse_Block operation for Statement that is provided for you in the Bugs_Catalog. In Part 2 of this lab assignment, you will complete your implementation of the parser by implementing also the Parse_Block operation (as well as the Parse operation) for the Statement component.
The set up instructions will provide you with a user catalog containing the following components and auxiliary items:
The test driver for this lab is a BL parser that reads in a BL program and, if no syntax errors are found, outputs a pretty-printed version of the original program. To use a demonstration version of the parser, just execute the command 321_Parser < BL_source_program. If you want to save the output to a file you can use the command 321_Parser < BL_source_program > output_file.
cp -r /class/sce/now/321/labs/catalogs/lab3-part1 /project/c321axnn
cd /project/c321axnn/lab3-part1 chmod -R g-l,g+rwX . set-group-ID .
Here is an example call to the
Get_Next_Non_Separator_Token
operation:
m.Get_Next_Non_Separator_Token (str, token_text, token_kind)where m is of type Tokenizing_Machine, str is of type Character_IStream, token_text is of type Text, and token_kind is of type Integer. The return value of token_kind will be from the following enumeration of Integer constants, which appears in the component BL_Tokenizing_Machine_Kernel:
enumeration Token_Types
{
KEYWORD,
IDENTIFIER,
CONDITION,
WHITE_SPACE,
COMMENT,
ERROR
};
Note that Get_Next_Non_Separator_Token returns the next non-separator token, i.e., it skips WHITE_SPACE and COMMENT tokens and only returns KEYWORD, IDENTIFIER, CONDITION, and ERROR tokens.
In addition to being responsible for catching any syntax errors in the input BL source program (i.e., determining whether the source program is in the language generated by the context-free grammar for the BL language), your parser is also responsible for checking the following conditions (and if they are not satisfied, an error message should be produced):
The parser is not responsible for making sure that no undefined instruction is called in the program nor that no recursion is present in the program. These conditions can be tested much more easily in the code generation phase, so that's where they will be checked.
Parser (instead
of 321_Parser).
rcpp-submit c321?? lab3-part1If you get an error message rather than a confirmation of success, please read the message; it contains useful information! Do not just run the same command again. Save the e-mail you get as a receipt of submission, just in case.
You will be implementing the Parse operation as an extension to the Statement component (see the Statement section in the class notes for the specs). Your implementation must use a recursive descent algorithm based on the context-free grammar for BL programs included at the end of this handout. It must also check for syntax errors using assert statements, just like in Part 1 of this lab assignment.
cp -r /class/sce/now/321/labs/catalogs/lab3-part2 /project/c321axnn
cd /project/c321axnn/lab3-part2 chmod -R g-l,g+rwX . set-group-ID .
By default, the parser in the lab3-part2 catalog will use the implementation of Parse_Program from the Bugs_Catalog. If you would rather use your own implementation from Part 1, you have to copy the following components from your lab3-part1 catalog into your lab3-part2 catalog:
This can be accomplished with the following commands:
cd /project/c321axnn/lab3-part2
cp -r /project/c321axnn/lab3-part1/AT/Program AT
cp -r /project/c321axnn/lab3-part1/CT/Program CT
chmod -R g-l,g+rwX .
set-group-ID .
rcpp-submit c321?? lab3-part2If you get an error message rather than a confirmation of success, please read the message; it contains useful information! Do not just run the same command again. Save the e-mail you get as a receipt of submission, just in case.