Implement a third component of the compiler

Assignment Help JAVA Programming
Reference no: EM13317335

You will implement a third component of the compiler that checks the static semantics of the program being compiled. You will use the visitor design pattern to walk the AST to check anything that can be verified (statically) at compile-time.

Note that this is the third component for the VC compiler since the recogniser, once extended into a parser, will no longer be used.

Revision Log

1. Specification

You are to implement a semantic or contextual analyser that checks that the program conforms to the source language's context-sensitive constraints (i.e., static semantics) according to the VC Language Definition. This part of the compilation process is referred to as the semantic or contextual analysis.

There are two types of context-sensitive constraints:

• Scope rules: These are the rules governing declarations (defined occurrences of identifiers) and applied occurrences of identifiers.
• Type rules: These are the rules that allow us to infer the types of language constructs and to decide whether each construct has a valid type.

Therefore, the semantic analysis consists of two subphases:
• Identification: applying the scope rules to relate each applied occurrence of an identifier to its declaration, if any.
• Type checking: applying the type rules to infer the type of each construct and comparing that type with the expected type in the context.

This assignment involves developing a visitor class (named Checker) that implements the set of visitor methods in the interface VC.ASTs.Visitor.java. Your semantic analyser will be a visitor object that performs both identification and type checking in one pass by visiting the AST for the program being compiled in the depth-first traversal.

In the case of ill-typed constructs, appropriate error messages as specified below must be reported.

As before, if no lexical, syntactic or semantic error is found, your compiler should announce success by printing:

Compilation was successful.

Otherwise, the following message should be printed:

Compilation was unsuccessful.

2. Identification

This subphase of the semantic analyser has been implemented for you. Identification relates each applied occurrence of an identifier to its declaration, if any, by applying the VC's scope rules. The standard method of implementing this subphase is to employ a symbol table that associates identifiers with their attributes. In the VC compiler, the attribute for an identifier is represented by a pointer (an inherited attribute) to the subtree that represents the declaration (GlobalVarDecl, LocalVarDecl or FuncDecl) of the identifier. This attribute is represented by the instance variable decl in VC.ASTs.Ident.java:

package VC.ASTs;
importVC.Scanner.SourcePosition;
public class Ident extends Terminal {
public AST decl;
publicIdent(String value , SourcePosition position) {
super (value, position);
decl = null;
}
public Object visit(Visitor v, Object o) {
returnv.visitIdent(this, o);
}
}

There is only one symbol table organised as a stack for storing the identifiers in all scopes. Two classes are used:
• VC.Checker.IdEntry.java: defining what a symbol table entry looks like.
• VC.Checker.SymbolTable.java: defining all methods required for symbol table management.

The symbol table methods are called at the following visitor methods of the classChecker:

• visitGlobalVarDecl, visitLocalVarDecland andvisitFuncDecl:whenever the semantic analyser visits the declaration at a subtree, it will callinsertto enter the identifier, its scope level and a pointer to the subtree into the symbol table.

To detect duplicate declarations using the same identifier, you call the method retrieveOneLevel. This method returns a pointer to the identifier entry if the identifier was declared before in the current scope and null otherwise.

• visitIdent:whenever the semantic analyser visits an applied occurrence of an identifierI, it will callretrieve with the identifierIand thus retrieves the pointer to the subtree representing its declaration. It will then decorate the identifier node for Iby establishing a link to this declaration. This link is null if no declaration is found. This fact will be used by you to detect undeclared variables.

• visitCompoundStmt:whenever the semantic analyser visits a block, it calls openScopeat the start of the block to open a new scope andcloseScopeat the end of the block to close the current scope.


Attachment:- ASSIGNMENT.rar

Reference no: EM13317335

Questions Cloud

Determine the specific heat of the metal : 0.45 kg of a metal at 90degreesC is added to 0.40 kg of whater at 20.0degreesC. If the final temp of the mixture is 26.0degreesC, what is the specific heat of the metal
Employees take responsibility for operating : For wages to increase, what must grow at least at the same rate? Employees take responsibility for operating which of the following, in addition to bargaining with the employer?
Determine the magnetic energy density in the field : The magnetic fild inside a superconducting solenoid is 4.50 T. The solenoid has an inner diameter of 6.20 cm and a length of 26.0 cm. Determind the magnetic energy density in the field
Implement a third component of the compiler : To detect duplicate declarations using the same identifier, you call the method retrieveOneLevel. This method returns a pointer to the identifier entry if the identifier was declared before in the current scope and null otherwise.
Find the rms amplitude of the magnetic field : An electromagnetic wave produced by a dipole antenna is traveling in free space and has an rms electric field amplitude of 17 N/C. find the rms amplitude of the magnetic field
Find the wavelength of the same electromagnetic wave : he wavelength of an electromagnetic wave inside water (nwater = 1.33) is ? = 660 nm. find the wavelength of the same electromagnetic wave
What is the index of refraction of the material : The frequency of an electromagnetic wave is f = 3.75 x 1014 Hz, What is the index of refraction n of the material
Railway labor act? : What is the third purpose of the Railway Labor Act? Eliminating any restrictions on joining a union Guaranteeing the freedom of employees in any matter of self-organization Avoiding service interruptions Providing for prompt dispute settlement

Reviews

Write a Review

JAVA Programming Questions & Answers

  Write a simple java program that will read an array of info

write a simple java program that will read an array of information containing the information (Name, offence [between arson, assault or theft] and year of offence

  Identify the first character of the name and lengthof length

Write a Java application that stores the names of your family and friends in a one-dimensional array of Strings. The program should show all names in upper case and lower case, identify the first character of the name, and the lengths of the names.

  Creating the gui for the game interface

A GUI-based application that allows a user to play a simple trivia game

  Program produces 1000 integer ranging

Then your program produces 1000 integer ranging from 1to 10000 in the array and then sort them in ascending order and then print the result into data.txt file. (You may list integers separated by space or new line)

  Method to store the product of the two arrays in third array

Write a method to store the product of the two arrays in the third array

  Productexception class whose constructor

Create a ProductException class whose constructor receives a String that consists of a product number and price. Save the file as ProductException.java. Create a Product class with two fields, productNum and price. The Product constructor requires va..

  Consider an array of integers

Consider an array of integers as below: int[] a = {5, 2, -4, 3, 0, -5, 7, 11, 6, 13} Complete the method named count(int[] a) in the class Count. The method should return the number of positive numbers in the array

  Develop class which implements interface

Let the ADT called SquareMatrix. (The matrix can be represented by 2-D array of ints w/ n rows and n columns.) Write specification for ADT as Java interface. Develop the class which implements interface.

  Create javascript program to find gross pay for employees

Create JavaScript program which will find out the gross pay for each of three employees. Company pays "straight time" for first 40 hours worked by each employee and pays "time and a half".

  Demonstrates conditions under which the compareto() method

Write an application that demonstrates conditions under which the compareTo() method retrun a positive number, a negative number, and a zero when used to compare two Strings, Save the file as CompareStringValues.java. Lab Assignment

  Java script program for song

Construct a JavaScript program that declares and prints an array that includes at least twenty of your favorite songs.

  Create java application which creates random phone number

Create and implement Java application which creates and prints a random phone number of the form xxx-xxx-xxxx. Include the dashes in the output.

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd