Download JAVA SYNTAX EXERCISES

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
JAVA SYNTAX EXERCISES
The purpose of this 2nd set of exercises is to get familiar with a bigger SDF grammar. In both exercises you must extend the
Java grammar and parse terms using the modified grammar. Extract the syntax exercise archive in your directory. The
extracted directories contain a syntax definition of Java. This syntax definition is slightly different from the one you can
find in the library. Furthermore, the directory contains a subdirectory with test terms.
You must hand in via PEACH only the modified modules. Please clearly indicate in the module where you modified it. You
can use %% to write comments in an SDF file.
Exercise 1
Static Imports allows static members to be used without class qualification. This is similar to regular import declarations
that allow classes to be used without package qualification.
The qualified members java.lang.Math.sin and java.lang.Math.PI in the following Java fragment:
public class Test
{
static public void main(String args[])
{
double x = java.lang.Math.sin(java.lang.Math.PI);
}
}
can be represented with static imports as shown in the following Java fragment:
import static java.lang.Math.sin;
import static java.lang.Math.PI;
public class Test
{
static public void main(String args[])
{
double x = sin(PI);
}
}
The syntax for Static Imports is described in chapter 7.5 of the Java Language Specification 3rd Edition1 (JLS3).

Download the Java Language Specification 3rd Edition and lookup the syntax for Static Imports.
Part of the syntax for Static Imports is repeated here:
ImportDeclaration:
SingleTypeImportDeclaration
TypeImportOnDemandDeclaration
SingleStaticImportDeclaration
StaticImportOnDemandDeclaration
SingleStaticImportDeclaration:
import static TypeName . Identifier;
StaticImportOnDemandDeclaration:
import static TypeName . * ;
Start the Meta Environment.
Load the module languages/java/syntax/Java.sdf.
The module represents a Java SDF Syntax that corresponds (mostly) with Java 1.4 or with the Syntax described in the Java
Language Specification 2nd Edition.
The purpose of exercise 1 is to add the Java Syntax for Static Imports.
1

Update the Java.sdf module and add the sorts SingleStaticImportDeclaration and
StaticImportOnDemandDeclaration.

Test the Static Import syntax by loading the term file TestStaticImport.trm file. There should be no
parsing errors.
Java Language Specification 3rd Edition, http://java.sun.com/docs/books/jls/index.html
Exercise 2
Java Annotations allows adding meta data in the class itself through the use of annotation types. These annotations can be
used by tools and libraries. Typically used for code generation.
Java Annotations consists of syntax for declaring annotation types and syntax for annotating declarations.
An example of annotating declarations is given in the following Java fragment:
@TestAnnotationCharlie()
public class TestAnnotation
{
@TestAnnotationCharlie( )
public int value;
@TestAnnotationCharlie()
@TestAnnotationAlpha()
@TestAnnotationBeta(id = 1, FirstName = "Inu", LastName = "Yasha")
public
TestAnnotation()
{
// do nothing
}
}
An example of an annotation type (a special interface) is given in the following Java fragment:
public @interface TestAnnotationAlpha
{
String value() default "Kurapika Kuruta";
}
The purpose of exercise 2 is to extend the Java Syntax with Annotations. Only change the files that are indicated.
Annotation
The syntax for the Annotation is described in chapter 9.7 of JLS3. The relevant part of the syntax is given here:
Annotation:
NormalAnnotation
MarkerAnnotation
SingleElementAnnotation
NormalAnnotation:
@ TypeName ( ElementValuePairsopt )
MarkerAnnotation:
@ TypeName
SingleElementAnnotation:
@ TypeName ( ElementValue )
ElementValuePairs:
ElementValuePair
ElementValuePairs , ElementValuePair
ElementValuePair:
Identifier = ElementValue
ElementValue:
ConditionalExpression
Annotation
ElementValueArrayInitializer
ElementValueArrayInitializer:
{ ElementValuesopt ,opt }
ElementValues:
ElementValue
ElementValues , ElementValue

Update the Modifiers.sdf module and add the sorts and context-free syntax for Annotation,
NormalAnnotation, MarkerAnnotation, SingleElementAnnotation, ElementValuePair,
ElementValue and ElementValueArrayInitializer.
Hint:
There is no need to specify the sort ElementValuePairs. Use {ElementValuePair ","}+ instead.
Hint:
The following declaration can be used for the sort ElementValueArrayInitializer:
"{" {ElementValue ","}* ","? "}" -> ElementValueArrayInitializer
Hint:
It may be necessary to add the import the languages/java/syntax/Expressions module.
The sort Annotation is used in declarations throughout the Java syntax. See for example: chapter 7.4.1 Package Declaration,
chapter 8.1.1 Class Declaration, chapter 8.3.1 Field Declaration, chapter 8.4.1 and 8.4.3 Method Declaration, chapter 8.8.3
Constructor Declaration, and so on.
The typical usage of the sort Annotation is in the specification of the modifiers. For example, chapter 8.4 and 8.4.3
describes the MethodModifiers part of which is given here:
MethodDeclaration:
MethodHeader MethodBody
MethodHeader:
MethodModifiersopt TypeParametersopt ResultType MethodDeclarator Throwsopt
MethodModifiers:
MethodModifier
MethodModifiers MethodModifier
MethodModifier: one of
Annotation public protected private abstract static final synchronized native strictfp
The Annotation is consistently used as part of a modifier. The SDF Java syntax uses a generic Modifiers sort for all
declarations.

Update the Modifiers.sdf module and add the Annotation to the Modifier syntax.

Test the Annotation Declarations by loading the term file TestAnnotation.trm. There should be no parsing
errors.
Annotation Types
The syntax for the Annotation Types is described in chapter 9.1 and 9.6 of JLS3. Part of the syntax is given here:
InterfaceDeclaration:
NormalInterfaceDeclaration
AnnotationTypeDeclaration
AnnotationTypeDeclaration:
InterfaceModifiersopt @ interface Identifier AnnotationTypeBody
AnnotationTypeBody:
{ AnnotationTypeElementDeclarationsopt }
AnnotationTypeElementDeclarations:
AnnotationTypeElementDeclaration
AnnotationTypeElementDeclarations AnnotationTypeElementDeclaration
AnnotationTypeElementDeclaration:
AbstractMethodModifiersopt Type Identifier ( ) DefaultValueopt ;
ConstantDeclaration
ClassDeclaration
InterfaceDeclaration
EnumDeclaration
AnnotationTypeDeclaration
;
DefaultValue:
default ElementValue

Update the Interfaces.sdf module and add the sorts and context-free syntax for
AnnotationTypeDeclaration, AnnotationTypeBody, AnnotationTypeElementDeclaration
and DefaultValue.

Test the Annotation Type by loading the term files TestAnnotationAlpha.trm,
TestAnnotationbeta.trm and TestAnnotationCharlie.trm. There should be no parsing errors.
Hint:
The SDF Java Syntax uses a generic Modifiers sort instead of a separate modifier type for each declaration. Use
Modifiers instead of InterfaceModifiers.
Hint:
There is no need to specify the sort AnnotationTypeElementDeclarations. Just use:
"{" AnnotationTypeElementDeclaration* "}" -> AnnotationTypeBody