编译器代写 | COMP4403/COMP7402 – Compilers and Interpreters Assignment 3

本次澳洲代写主要为pl0编译器相关的assignment

COMP4403/COMP7402 – Compilers and Interpreters Assignment 3

Assignment Compiler files

All sources for the assignment PL0 compiler are available as a3.zip (below). Please be sure to use the version for this assignment and not the one used for the tutorials or another assignment. There are differences (like the lexical tokens you need for this assignment are only defined in this version). Note that the symbol table Scope and SymEntry and Type implementations have been extended already to provide support for procedure parameters.

  • zipSave this .zip file and follow the instructions for setting up a compiler project in IntelliJ
  • Setting up and running PL0 in IntellliJ
  • Brief documentation on assignment 3 files

Please pay attention to course Blackboard announcments, and ensure you follow the course discussion board (https://edstem.org/) for any updates and further information on the assignment.

  • Do not use imports for external packages other than those in util.*. Note that IntelliJ may offer the option of importing an external package to resolve an issue; please avoid taking this option because it will often add an erroneous import that you will not need. Such imports lead to the compilation failing in the environment in which your compiler will be assessed because that environment may not include the external libraries. Please check you are not importing external libraries before submitted.
  • You must onlymodify the files that must be submitted (see below).
  • You must notmodify any other files because we will be testing your implementation using the existing other files with your submitted files.
  • Please do notreformat the files because we would like to just print the differences between the originals and the versions you hand in.
  • Please keep the length of lines in your files below 100characters, so that we can print them sensibly.
  • Please avoid using non-standardcharacters, e.g. Chinese characters, including in the comments. Non-standard characters are not accepted by some Java compilers and all comments should be readable by the person assessing your assignment.
  • Your implementation should be in Java 8. Set the IntelliJ preferences for the Java compiler to 1.8 (or use the “-source 1.8” option to the Java compiler).
  • Please remove any debugging output before your assignment is submitted because debugging output will cause your program to fail our automated testing of your assignment.
  • Either avoid using tabs or set your tabs stops to 4 spaces (this is the default for IntelliJ) so that your files will print sensibly.

Read the fine print in detail before you start! And, most important, when you have finished implementing the assignment, come back and reread the fine print again.

Procedures with value and reference parameters

The declaration of a procedure includes a list of formal parameters. The type of each formal parameter is declared and it optionally has a default expression to be used when the parameter is not supplied in the call. Formal reference parameters are preceded by the keyword “var”. For example,

// A procedure fact with  // a formal value parameter n of type int with a default value of 0 and  // a formal reference parameter r of type int with no default  procedure fact( n: int <- 0, var r: int ) =   begin    if n = 0 then      r := 1    else     begin      // The formal parameter n for the recursive call gets the value of the      // local n minus 1, where the n within n-1 is the value of the formal      // parameter n for this (calling) instance of fact.      // The parameter r of the call is the formal parameter r of this      // (calling) instance of fact.      call fact( n <- n-1, r <- r);      r := n * r     end   end // fact

The above procedure returns the value of factorial n via r, e.g., the call

call fact( r<-f, n<-4 )

assigns f the value of factorial 4, i.e., 4*3*2*1 = 24. Note that as the parameters must be explicitly named in the call, they can be in any order. The following uses the default value for n, i.e. 0.

call fact( r<-f )

It assigns f the value of factorial 0, i.e. 1.

Syntax Changes

The following lexical tokens have been added to the scanner module of PL0 already.

COMMA “,”
GETS “<-“

A procedure may have any number of formal value and reference parameters (zero or more). Each formal parameter has a type and an optional default expression. The syntax for a procedure declaration follows. It is given in Extended BNF, but you won’t be able to use the EBNF directly in the parser generator Java-CUP (see below).

ProcedureDef → ProcedureHead ‘=’ Block ‘;’
ProcedureHead → ‘procedure’ IDENTIFIER ‘(‘ [ FormalParams ] ‘)’
FormalParams → FormalParam { ‘,’ FormalParam }
FormalParam → IDENTIFIER ‘:’ TypeIdentifier [ ‘<-‘ Condition ]
FormalParam → ‘var’ IDENTIFIER ‘:’ TypeIdentifier [ ‘<-‘ LValue ]

A procedure call has a list of actual parameters, each of which assigns an expression (Condition really (to allow for boolean expressions)) to a formal parameter.

Statement → ‘call’ IDENTIFIER ‘(‘ [ ActualParams ] ‘)’
ActualParams → ActualParam { ‘,’ ActualParam }
ActualParam → IDENTIFIER ‘<-‘ Condition

Note that all actual parameter expressions are Conditions in order to allow for boolean value parameters. Type checking in the static checker is used to determine whether the parameter expression is valid, in particular for a reference parameter.

You are expected to provide some syntax error recovery using Java CUP’s “error” symbol, in particular for formal and actual parameters. Note that you must handle semantic errors appropriately, including handling the situation when there is a syntax error, i.e., your compiler should not crash because of a syntax error. Your handling of syntax errors does not have to exactly match that used for the sample solutions.

The Parser Generator Java-CUP

The parser specification for the compiler is specified in the file PL0.cup. You will need to add productions (and their associated actions) to the specification and then run the parser generator Java-CUP (using run configuration “Run CUP”) to generate the files CUPParser.java and CUPToken.java. Do not modify these two Java files directly (even if you think you understand them (do you really?)) – remake them from PL0.cup. You should make the compiler before you change anything just to see what forms of messages to expect. When you make the compiler (before you modify it) there will be some warning messages about the terminal symbols like ILLEGAL being declared but never used; these are to be expected at this stage. Any new warning/error messages will be significant. There is HTML documentation for Java-CUP available from the class web page (with the assignment).