CSE5317/4305 Project #3 (Abstract Syntax)

Due Thursday March 30 before midnight

Worth 15% of your project grade


For this project, you are asked to generate ASTs (Abstract Syntax Trees) for PCAT. The ASTs are described in pcat/src/main/scala/edu/uta/pcat/AST.scala. See the calculator example calc/src/main/scala/edu/uta/calc/calc.cup to understand how ASTs are constructed based on the classes in calc/src/main/scala/edu/uta/calc/AST.scala

Your task for this project is to add actions to your pcat.cup file to generate ASTs. See the "Complete Concrete Syntax" section in the PCAT manual and the AST.scala classes to understand the relationship between the PCAT grammar and the ASTs. You may run the solution over various test files: scala pcat-solution.jar 3 tests/*.pcat, to understand what AST you need to generate. For example, the AST of the PCAT expression (x-2)+3 is constructed as follows::

BinOpExp("plus",BinOpExp("minus",LvalExp(Var("x")),IntConst(2)),IntConst(3))
For example, the action for the PLUS expression is:
expression ::= expression:e1 PLUS expression:e2 {: RESULT = new BinOpExp("plus",e1,e2); :}
(Recall that pcat.cup uses Java, not Scala, so you must call the case class constructors with 'new'). A good way to start this project is to write a test file x.pcat outside your pcat directory that contains 1-2 lines only, such as PROGRAM IS BEGIN x:=x+1; END;, and then run the solution on this file. That way, you can look at the AST of very simple expressions and statements and reverse-engineer the solution.

Hints: If a type is optional, then, if it is missing, return NamedType("NoType"). The constant NIL has type AnyType(). To convert a list of statements into a single statement use SeqSt. To convert a VAR declaration to a VarDec with one ID only, use a loop or a map to generate one VarDec for each VAR ID. Need to improvise for defaults: eg, if the first expression of a record-init is missing, use IntConst(1). The first rule in the grammar should call PCAT.setAST(...) to store the AST of the source program because the next parser phases will use this value.

Note that you would need to pass the final AST to the rest of the program, which must be a ProcDecl of the PCAT main program. For example, the first rule may look like this:

program ::= PROGRAM IS body:b SEMI   {: PCAT.setAST(new ProcDecl("main","NoType",nil,b)); :}

You need to run your parser against all tests/*.pcat files. For example:

scala lib/pcat.jar 3 tests/hello.pcat
You also need to compare your output with that of the solution:
scala pcat-solution.jar 3 tests/hello.pcat

After ensuring that your program compiles and executes correctly, cleanup your pcat directory using mvn clean Then, zip the pcat directory as pcat.zip and submit it here:

Submit Project #3:

Last modified: 03/09/2017 by Leonidas Fegaras