Practical Assignment 7
Assessment Overview
Weighting: 80 Points (8% of course grade)
Due date: Friday 28 Sunday 30 Oct Thursday 3 Nov 11:59 pm (SWOT Week)
Gradescope open now
Task
description:
Develop a Parser to convert high-level programming language into a
parse tree. Doing so should help you to:
Practice applying grammar rules
Understand how complex and nested code structures can be
broken down to their component parts.
Understand the basics of Recursive Descent Parsing.
Please post your questions on Piazza or ask during your workshop.
Academic
Integrity
Checklist
Do
Discuss/compare high level approaches
Discuss/compare program output/errors
Regularly submit your work as you progress
Be careful
Using online resources to find the solutions rather than
understanding them yourself won't help you learn.
Do NOT
Submit code not solely authored by you.
Use a public GitHub repository (use a private one instead).
Post/share complete VM/Assembly/Machine code in
Piazza/Discord or elsewhere on the Internet etc.
Give/show your code to others
Your Task
Your task for this practical assignment is to write a parser to convert high-level language
programs into a parse tree that can be later converted to VM Code.
1. Complete the Parser as described and as outlined below.
Submit your work regularly to Gradescope as you progress.
Additional resources and help will be available during your workshop sessions.
2. Test your code.
We're know that things are tight at the end of semester, so we've kept this assignment short
(and hopefully simple).
Part 1 - Recursive Descent Parser (80 points)
We've seen VM Code and how that can be translated to Assembly and Machine code, but
these languages are represented as basic sequences of instructions -- how do we handle the
nested and varied structures of high-level programming languages?
Using your preferred programming language (Python, C++ or Java) implement the
CompilerParser as described below.
This practical assignment follows a similar approach to the Nand2Tetris Compilation Engine.
Template files are provided for each of these programming languages.
Download the Python version HERE
(https://myuni.adelaide.edu.au/courses/72399/files/11771254?wrap=1)
(https://myuni.adelaide.edu.au/courses/72399/files/11771254/download?
download_frd=1) .
Download the Java version HERE
(https://myuni.adelaide.edu.au/courses/72399/files/11771151?wrap=1)
(https://myuni.adelaide.edu.au/courses/72399/files/11771151/download?
download_frd=1) .
Download the C++ version HERE
(https://myuni.adelaide.edu.au/courses/72399/files/11772199?wrap=1)
(https://myuni.adelaide.edu.au/courses/72399/files/11772199/download?
download_frd=1) .
You will need to complete the methods provided in the CompilerParser class.
The provided ParseTree & Token classes should not be modified.
Only submit files for 1 programming language.
Getting Started
1. Start by reviewing chapter 10 of the textbook.
2. Each of the methods listed below needs to apply the corresponding set of grammar rules
to the series of tokens given.
For each set of these grammar rules:
A new parse tree is created.
The tokens are processed 1-by-1.
Tokens matching the grammar rule are added to a ParseTree for that rule.
If the rules are broken (i.e. the sequence of tokens does not match the rules), a
ParseException should be thrown/raised.
Otherwise the ParseTree data structure is returned.
Some of the sets grammar rules require other sets of grammar rules.
For example, the whileStatement rule requires the rules for expression and
statements.
These rule sets should be applied recursively.
3. A ParseTree data structure is returned
Tokens
Each token has a type and corresponding value.
Tokens can have the following types and possible values:
Token Type Value
keyword
symbol
integerConstant A decimal integer in the range 0..32767
stringConstant A sequence of characters not including double quote or newline
identifier A sequence of letters, digits, and underscore ( ), not starting with a digit.
We can read the type of the token with the Token.getType() method, and its value with
Token.getValue()
Parse Trees
Each node in the ParseTree has a type, a value, and a list of children (parse trees nested
inside this tree).
When creating a ParseTree, we set the type and value in the constructor. We can then add
parse trees via the ParseTree.addChild(ParseTree) method. If needed, we can read the type of
the ParseTree with the ParseTree.getType() method, and its value with ParseTree.getValue() .
To review the structure of a ParseTree object, it can be printed; this will output a human
readable representation.
ParseTrees can have the following types which correspond with a set of grammar rules:
Parse Tree
Type
Grammar Rule
class
classVarDec
subroutine
parameterList
subroutineBody
varDec
statements where statement matches the following rule:
letStatement
ifStatement
whileStatement
doStatement
returnStatement
expression
Note the addition of the skip keyword
term
expressionList
Which match the methods we're implementing.
They can also have the same types as listed above for Tokens (and Tokens can be added
as children to ParseTrees via typecasting)
You may have noticed that some grammar elements shown above and in the Jack Grammar
are missing from this list. These rules are listed below. They should be used as part of the
rules above, but are not themselves ParseTree types:
Grammar
Element
Grammar Rule
className
varName
subroutineName
type
op
unaryOp
keywordConstant
subroutineCall
Suggested Approach
A suggested approach is outlined in section 10.1.4 of the Text book.
This involves writing a process(token) method which:
Checks if the next token in the list of tokens matches an expected token
If the token matches, add it to the ParseTree
If the token does not match, throw/raise a ParseError
Advances to the next token (if needed)
This can be done by removing/popping the token from the list
Task 1.1 - Program Structure (40 points)
Complete the program structure related methods:
compileProgram
Jack Code Tokens
Returned ParseTree
Structure
class Main {
}
keyword class
identifier Main
symbol {
symbol }
class
keyword class
identifier Main
symbol {
symbol }
static int a ;
keyword static
keyword int
identifier a
symbol ;
ParseError (the program doesn't
begin with a class)
compileClass
Example Jack Code Tokens
Returned ParseTree
Structure
class Main {
static int a ;
}
keyword class
identifier Main
symbol {
keyword static
keyword int
identifier a
symbol ;
symbol }
class
keyword class
identifier Main
symbol {
classVarDec
...
see
classVarDec
below
symbol }
compileClassVarDec
Example Jack Code Tokens
Returned ParseTree
Structure
static int a ;
keyword static
keyword int
identifier a
symbol ;
classVarDec
keyword static
keyword int
identifier a
symbol ;
compileSubroutine
Example Jack Code Tokens
Returned ParseTree
Structure
function void myFunc ( i
nt a ) {
var int a ;
let a = 1 ;
}
keyword function
keyword void
identifier myFunc
symbol (
keyword int
identifier a
symbol )
symbol {
keyword var
keyword int
identifier a
symbol ;
keyword let
identifier a
symbol =
integerConstant 1
symbol ;
}
subroutine
keyword function
keyword void
identifier myFunc
symbol (
parameterList
...
(see
parameterList
below)
symbol )
subroutineBody
...
see
subroutineBody
below
compileParameterList
Example Jack Code Tokens
Returned ParseTree
Structure
int a, char b
keyword int
identifier a
symbol ,
keyword char
identifier b
parameterList
keyword int
identifier a
symbol ,
keyword char
identifier b
compileSubroutineBody
Example Jack Code Tokens
Returned ParseTree
Structure
{
var int a ;
let a = 1 ;
}
symbol {
keyword var
keyword int
identifier a
symbol ;
keyword let
identifier a
symbol =
integerConstant 1
symbol ;
}
subroutineBody
symbol {
varDec
...
(see varDec
below)
statements
...
(see
statements
below)
symbol }
compileVarDec
Example Jack Code Tokens
Returned ParseTree
Structure
var int a ;
keyword var
keyword int
identifier a
symbol ;
varDec
keyword var
keyword int
identifier a
symbol ;
Task 1.2 - Statements (40 points)
Complete the statement related methods:
compileStatements
Example Jack Code Tokens
Returned ParseTree
Structure
let a = skip ;
do skip ;
return ;
keyword let
identifier a
symbol =
keyword skip
symbol ;
keyword do
keyword skip
symbol ;
keyword return
symbol ;
statements
letStatement
...
(see
letStatement
below)
doStatement
...
(see
doStatement
below)
returnStatement
...
(see
doStatement
below)
compileLet
Example Jack Code Tokens
Returned ParseTree
Structure
let a = skip ;
keyword let
identifier a
symbol =
keyword skip
symbol ;
letStatement
keyword let
identifier a
symbol =
expression
...
see
expression
below
symbol ;
compileIf
Example Jack Code Tokens
Returned ParseTree
Structure
if ( skip )
compileReturn
Example Jack Code
Tokens
Returned ParseTree
Structure
return skip ;
keyword return
keyword skip
symbol ;
returnStatement
keyword return
expression
...
see
expression
below
symbol ;
For some of the above methods, you will also need to partially implement the
compileExpression method below.
At this stage, implement the compileExpression to match the grammar rule .
Task 1.3 - Expressions (Optional - up to 20 BONUS points)
Complete the expression related methods:
This section is optional and is worth Bonus Points
compileExpression
Example Jack Code Tokens
Returned ParseTree
Structure
skip keyword skip
expression
keyword skip
1 + ( a - b )
integerConstant 1
symbol +
symbol (
identifier a
symbol -
identifier b
symbol )
expression
term
...
see term
below
symbol +
term
...
see term
below
compileTerm
Example Jack Code Tokens
Returned ParseTree
Structure
1
integerConstant 1
term
integerConstant 1
( a - b ) symbol (
identifier a
symbol -
identifier b
symbol )
term
symbol (
expression
term
identifier
a
symbol -
term
You're done!
Submit your work to Gradescope using the button below.
You may submit via file upload or GitHub.
If using GitHub, ensure your repository is private.
Your files should either be:
In the root of your submission (i.e. no subdirectory)
~ or ~
In a directory named prac7
Be sure to submit all files with each submission.
Additional Resources
The following resources may help you complete this assignment:
identifier
b
symbol )
compileExpressionList
Example Jack Code Tokens
Returned ParseTree
Structure
1 , a - b
integerConstant 1
symbol ,
identifier a
symbol -
identifier b
expressionList
expression
...
see
expression
above
symbol ,
expression
...
see
expression
above
Examples
See above
This tool needs to be loaded in a new browser window
Chapter 10 of the Text Book
(https://myuni.adelaide.edu.au/courses/72399/external_tools/1284) for Compiler
Implementation
Section 10.1.4 includes basics of a suggested approach.
Week 11 & 12 Workshops
Guide to Testing and Writing Test Cases
(https://myuni.adelaide.edu.au/courses/72399/pages/guide-to-testing-and-writing-test-cases)
Figure 10.5 on page 201 of the Text Book
(https://myuni.adelaide.edu.au/courses/72399/external_tools/1284) for specification of the
Jack Grammar.
Further resources will be added over the coming days.
Gradescope Link Temporarily Broken - Log In directly using "School Credentials"
https://www.gradescope.com/ (https://www.gradescope.com/)
See Pinned post in Piazza if having issues with the above.
Load Practical Assignment 7 in a new window
版权所有:编程辅导网 2021 All Rights Reserved 联系方式:QQ:99515681 微信:codinghelp 电子信箱:99515681@qq.com
免责声明:本站部分内容从网络整理而来,只供参考!如有版权问题可联系本站删除。