CS代考 CSCI 3366 Programming Languages

CSCI 3366 Programming Languages
Problem Set 4: Grammars & Parsing
Due: Tuesday, March 1, 2022, 6PM
(12 Points)

Copyright By PowCoder代写 加微信 powcoder

Part 1 (5 points): Re-associating Expressions
We saw in class an examole of a parser for anthmetic exoressions where every operation
was parsed in a night associative way. That is, 2 – 2 – 2 was parsed as 2 – (2 – 2).
However, for many operations, the standard mathematical convention is for the operation
to so loft associative. For example minus should on left associative so that 2 – 7-
ought to be parsed as (2 – 2) – 2.
However. we saw that rant associativity is easer to parse ov detault in a recursive
descent stvie. In the next part of this assignment. you will write a parser that handles left
associativity by transforming the grammar a version that is suited to recursive descent
In this part we consider an alternate strategy: re-associating a parse tree so that the
operations will be left-associative. For this exercise, we will consider the following simple
ASI Ivoe found in reassoc-src/bin/main.e
Wont of int
: ast: op2
where the rarens constructor represents parentheses around an expression
(Parentheses are usually omitted during the act of parsing, but since we are going to
change the groupings of operations as part of reassociating, it makes sense to include
In reassoc-sre/bin/main.al vou must wrie a funcion leftassociate: ast -2 ast
that takes as input an AST and returns a version in which the Plus and Minus
operations have been left-associated. For examole, on an inout tree of the form

Your code should return the AST corresponding to
That is vou would convert Plus ( 001 = In° 1: 002 = plus 001 = Int 2: 002
Int 3 )) into Plus op1 = Plus op1 = Int 1; op2 = Int 2 ): op2 = Int 3 )
Note that our code should not rearrange

Once you begin coding, your work will largely be confined to the file parser.at. in
vicular, you’re required (o Implement the single runction specieo in parser.ml.
al oarser : Token. t list -> Ast. t
Leads un! The Token module has a lot of tokens that are not used in Mercury This
is because the Token module will be used for other mini- programming languages.
The tokens that appear in well-formed Mercury programs are
PLUS MINUS TIRES DIV ROD LPAR RPAR INTEGER or int 00I
To compile and run your implementation of the Mercury REPL:
> dune exec bin/sain. exe
To compile and run the unit tests for this part, run:
2 co kercury-or
The Parser
Let 1 denote an integer token. Then the concrete syntax for Mercury is given by the
following context-free grammar where E is the start symbol
This grammar uses left-recursion to make all operators associate to the left and uses
layering to give

where we have introduced a new symbol c
that represents the “tail” of an E string
You will need to similarly transtorm the rules for T as appropnate.
Once you’ve left-factored the grammar, you can use it as a quide to write a set of
mutually recursive funciions, each responsible for parsing a porton or he lanuage..
course, each will need to build an ast as defined in ast.ml, and return a list of pending
tokens that remain to be parsed, as in the example parsers we looked akin class, In the
sequence of tokens don’t give rise to a well-formed Mercury program, your code can
raise an excepuon using falwith
There’s one new trick we need to apply in construcuing such a parser compared o ine
example we saw in class. If we think about how to handle parsing E and E’ in the
transtormed crammar above, the funckon for t will first call the funchon for I , whicn
will return an AST, call it ast1. Then, it will call the function for E’ . In the right
associalive arammar parsers we saw in class. we wou d nave olen kne Asl nadE
returns, say ast2, and then returned an AST like:

and similariv it the next token we found was – instead. If we find nethera + or a
then we can just return the ast argument that we’ve been accumulating. If you simulate
afew steps of this process in your mind or on paper, you can see that the tree we will
auld un this way will he left aceciated
You will use a similar trick in the function for parsing
T after applying the left factoring
Abstract Syntax
The grammar above specifies the concrere sylloxv
The abstract syntax is
You’ll note there that we are treating the familiar infix operators as though the were
ordinary orefix functions. for examole, the concrete syntax “2 + 3” would be
reoresented ov the application:
The value of the exoression above would be represented by Dynasic.Literal

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com