CSE 321 Lab 3


Lab 3 Table of Contents


Part 1 - Table of Contents


Objective

The objective for this assignment is to implement the Program_Parse component, which is an extension to the Program component.


Overview

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.


Materials Provided

The set up instructions will provide you with a user catalog containing the following components and auxiliary items:

You will need to:

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.


Set Up

  1. Copy the lab3-part1 course catalog to your group project directory (/project/c321axnn, where "x" is your section letter and "nn" is your team number):
    cp -r /class/sce/now/321/labs/catalogs/lab3-part1 /project/c321axnn
    
  2. Make sure that the new catalog will be accessible to your partner. Perform this action now and as your last act whenever you finish a working session. Not e that 'l' in the chmod command is the letter 'l' ("el"), as in "lock":
    cd /project/c321axnn/lab3-part1
    
    chmod -R g-l,g+rwX .
    
    set-group-ID .
    
  3. Look around the new user catalog to familiarize yourself with its contents.

Getting Tokens

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.


Other Errors

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):

  1. The identifier at the end of the program must be the same as the identifier at the beginning of the program.
  2. The identifier at the end of each new instruction definition must be the same as the identifier at the beginning of the definition.
  3. The names of new user-defined instructions must be unique, i.e., there cannot be two user-defined instructions with the same name.
  4. The names of new user-defined instructions must not be the name of one of the primitive instructions, i.e., move, turnleft, turnright, infect, or skip.

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.


Using the Test Driver

The test driver for this lab is the BL parser that uses your implementation of Parse for Program. You can execute it just like the demo version (as explained in the Materials Provided section) but calling it Parser (instead of 321_Parser).


What To Submit

Once you and your partner are convinced that all implementations are working correctly, get into your group's lab3-part1 directory /project/c321axnn/lab3-part1 in an xterm window and use the rcpp-submit command to submit your solution as follows (noting that you will need to use the appropriate suffix in place of ?? in this command, as posted next to your instructor's name on the CSE 321 Home Page):
rcpp-submit c321?? lab3-part1
If 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.


Part 2 - Table of Contents


Objective

The objective for this assignment is to implement the Statement_Parse component, which is an extension to the Statement component.


Overview

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.


Materials Provided

The set up instructions will provide you with a user catalog containing the following components: You will need to: The demo test driver for this part of the lab is the same as the one for Part 1 (i.e., 321_Parser). 


Set Up

  1. Copy the lab3-part2 course catalog to your group project directory (/project/c321axnn, where "x" is your section letter and "nn" is your team number):
    cp -r /class/sce/now/321/labs/catalogs/lab3-part2 /project/c321axnn
    
  2. Make sure that the new catalog will be accessible to your partner. Perform this action now and as your last act whenever you finish a working session. Not e that 'l' in the chmod command is the letter 'l' ("el"), as in "lock":
    cd /project/c321axnn/lab3-part2
    
    chmod -R g-l,g+rwX .
    
    set-group-ID .
    
  3. Look around the new user catalog to familiarize yourself with its contents.

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 .

What To Submit

Once you and your partner are convinced that all implementations are working correctly, get into your group's lab3-part2 directory /project/c321axnn/lab3-part2 in an xterm window and use the rcpp-submit command to submit your solution as follows (noting that you will need to use the appropriate suffix in place of ?? in this command, as posted next to your instructor's name on the CSE 321 Home Page):
rcpp-submit c321?? lab3-part2
If 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.