CSCI 384 FALL 2021HOMEWORK 3:Towards a Parser for MiniMLExercises 1 and 2 Due: Monday, 9/20 at 3pm. In class I showed you a recursive descent parser for a calculator language that had the following grammar:
To build a deterministic parser, we developed the following unambiguous grammar, making multiplication have higher precedence than addition, and making each left associative:
I’d like you to make a few additions to the language, and then modify the parser accordingly. Each of the language extensions appear in the language Standard ML. In fact, all the language constructs we are defining, excepting the
There you can type expressions like You can obtain the parser by the link below: You will extend the code according to the exercises below. Note that there are two deadlines and there will be two submission sites. When you’ve completed the first two exercises, you should upload your modified Please work to meet the first checkpoint. Do not wait until Monday to start writing this code! Exercise 1: integer divisionLet’s extend the syntax to flesh out the “muliplicative” parts of the grammar. We’ll use syntax that we borrow from Standard ML (or just SML), one of the dialects of Robin Milner’s ML functional language. In SML there are two integer division operations
should be read as
We can just enhance our grammar above like so:
Modify the parser so that it parses instead according to this grammar. You’ll need to generate abstract syntax tree nodes (AST nodes) labeled with Alternatively, if you like, you could instead invent a new AST label
instead of
for these and the other binary operations. Exercise 2: logic and comparisonNow we’ll imagine that our calculator language also has a boolean value type. This will be in preparation for adding an We add two constants
or
etc. And they sit at the same precedence level, lower than plus and minus, so that
are interpreted as
Finally we add logical AND, OR, and NOT to the syntax. Following SML, we name these binary connectives This leads to the following changes to the grammar. We (1) replace
with
and then (2) we include the new productions
Lastly, we (3) add the productions
and have the Modify the parser so that it parses this updated grammar accordingly. Make sure you also tell me in your Python header comments what you chose to use as the AST nodes for these additions. You can either represent
or with something similar to what I did with integers
Just let me know in the header comment. Note: This is going to introduce some nonsensical expressions that we will consider syntactically correct. For example Here’s the grammar now, in full glory.
Exercise 3: pretty printingIn class I’ll show you a version of my parser that “pretty prints” (this is a standard phrase!) the AST constructed by the parser as a result of a successful parse. Mine was fairly elaborate but, roughly, indented the contents of a subtree’s node labels so that they were further right of their parent. It also put “sibling” nodes at the same indentation level. Write similar code for reporting the AST of a successful parse. It need not have node links, just the indenting. And it’s also fine to just have one each node level sit on its own line. Things to the left in the expression should be on earlier lines than things to their right. For example, a parse of
In my code, I distinguished “branching” nodes from “leaf” nodes by whether they were a list of length one that contained only a label (for example, some of you might have chosen to represent the boolean literals as [“True”] and [“False”]. The code should be recursive, and is essentially an “in order” traversal of the AST. It will have two base cases: (1) labelled subtrees that have no subtrees, that is, are lists of length one and (2) data values in the trees (i.e. non-lists). So a Python condition like
would indicate a recursive case,
would be the one base case, and
would be the other. Hint: your pretty-printer function will need to be recursive. In addition to having a parameter for the subtree to be output, it should have a second parameter descrbing, saym the length of the indentation for that subtree’s output. For a BONUS version of this exercise, you could instead try to mimic my fancier output. Mine produced these lines of text
I’m willing to give lots of hints on how to do this. Exercise 4: if and letLet’s now add two “top-level” syntactic constucts, also found in Standard ML, one for a “conditional”
and
Modify the parser to also parse these two kinds of expressions. BONUS Exercise 5: evaluationNow that we have booleans with Write a function
that is given a context
we’ll need to know that |