CPS710

Assignment 2

Toronto Metropolitan University University

Parser - Grammar Recognizer

Due: October 20


SUBMISSION INSTRUCTIONS UNDER CONSTRUCTION - REMAINDER IS READY

Objectives

In this course you will be developing an interpreter for a high-level language, called HL, which manipulates lists and strings.

In this assignment you will be using JavaCC to build the component of HL's parser that recognises syntactically correct HL programs.

This should familiarize you with the use of compiler generators and the recursive descent parsing process, as well as the manipulation of grammars into an LL(k) form.

The learning objectives addressed in this assignment are 4Cb, 3Bd (this will include 3De), 3Be, 4Bb, 3Ad, 4Aa.

The B and A parser learning objectives are related to the grammar learning objectives because they involve using JavaCC to overcome some of the problems with the grammar of HL.

Some of these learning objectives will be identified in the description of this assignment, and some in the tests.

Assignment Stages

In this assignment you will manipulate the Grammar for the HL language, shown here in a variation of Backus-Naur form (BNF), to try to make it LL(k) while still accepting the same language. This means that it must be unambiguous, not left recursive, and with at most k lookaheads. K should be as small as possible.

Since you will ultimately build an interpreter for HL rather than a compiler, the starting non-terminal for this grammar, called start, is either a statement, an expression, or a condition rather than an entire program. You can see this in the productions for start.

It is recommended that you do this assignment in two stages:

  1. First manipulate the BNF grammar directly to try to make it as close to LL(k) as possible.
  2. Then convert this grammar to a recursive descent parser HL.jj in the format used by JavaCC and continue manipulating it with the support of JavaCC.
You will need to document all the changes you have made to the grammar (in both stages). These explanations should be recorded as comments in HL.jj as explained underneath.

As you work on this assignment, you will need to decide when to stop working on the BNF grammar and switch to working on the JavaCC parser instead.

Pros and cons of BNF grammar manipulation vs. JavaCC parser manipulation

It is up to you to decide when you want to switch from BNF manipulation to JavaCC manipulation. Here are the issues related to this decision:

Assignment Preparation

Just like you did for Assignment 1, you should
  1. Create a directory on the moons where you will do your work, with a subdirectory called Tests, for example:
    mkdir 710A2
    cd 710A2
    mkdir Tests
    
  2. Copy all the files in the Handouts directory into your working directory. These files are located on the moons at ~cps710/public_html/doc/F2024/A2/Handouts:
    cp ~cps710/public_html/doc/F2024/A2/Handouts/* .
    cp ~cps710/public_html/doc/F2024/A2/Handouts/src/* .
    
  3. Make the two testing scripts t and runtests executable by giving them user x perms:
    chmod u+x t runtests
    
Notice that solutions to A1 are provided in the Handouts/src directory: HL.jj, Token.java, and the other Token java files. These solutions are only for the B and C learning objectives of the scanner. These solutions are provided to you for the sole purpose of integrating them in your cps710 assignments this semester, and are copyrighted accordingly. If you prefer, you can use your own solution to A1 as a starting point for A2. Whatever you decide, be aware that the testing scripts for the rest of the assignments this semester will be using the java files to handle tokens handed out here.

Assignment

Grammar (Module 3) Learning Objectives

To meet these learning objectives, you will need to manipulate your grammar to remove ambiguities and handle components of the grammar that are not left factored. You will also need to explain what you have done. Here is the documentation that you need to include in your HL.jj file:

Parsing (Module 4) Learning Objectives

To meet these learning objectives, you will need to use JavaCC to parse HL programs. Modify the HL.jj program you developed in your first assignment to make it accept the HL grammar that you have been modifying. Note that it is extremely unlikely that your HL.jj will compile properly the first time you try.

You will meet the module 4 learning objectives when your program passes the tests for these learning objectives.

Here are additional requirements for the parsing learning objectives:

Debugging your assignment

JavaCC has a built-in tracing functionality that you may find useful for debugging your work. It is invoked by setting:
  DEBUG_LOOKAHEAD =true;
  DEBUG_PARSER=true;
In the options section of HL.jj program right at the top where you have also specified the case.

Testing your assignment

Your assignment will be assessed both manually and by running tests: We will look at your code to see how you have manipulated the grammar, and we will read your comments to get an understanding of the decisions you made. Your assignment will also be tested on the scs moons as described here:
  1. We will first retrieve your submission, unzip it, and, using the makefile in the Handouts directory compile it along with the five java source files provided in that directory: TestHL.java, Token.java, IntegerToken.java, IdentifierToken.java, and StringToken.java.

  2. We will then run the runtest script in the Handouts directory. This script is structured similarly to the runtest script in assignment1. The tests and their answers are in the Tests directory. As for assignment 1, the name of each test indicates which learning objectives it is associated with.

  3. We may run some additional tests which are not posted. However, the test files provided here provide a very thorough start. You are strongly encouraged to perform your own testing following the above procedure before submitting your assignment.

Submitting the assignment - UNDER CONSTRUCTION

As for assignment 1, we are using a script to grade you. Therefore please use exact names listed below, including the name of the zip file

References


This page is maintained by Sophie Quigley (cps710@cs.torontomu.ca)
Last modified Monday, 07-Oct-2024 11:28:23 EDT