jj logo
Take a quick tour of JJ
Teacher Registration
Student Registration
Industry Registration
Guest Registration
Some Humor
Friends
Quotes


Name
Password
School



JJ to Java Translation Definition

This file contains the line-by-line JJ to Java translation definition.

This definition is driven by the list of JJ Commands and the translations can be done command-by-command. In other words, any JJ command can be translated to the equivalent Java statement without requiring a complex translator and symbol table. This approach is possible because the JJ to Java translation does not require state information (with the one exception of keeping track of the number of slots in a function, routine, or constructor definition...but these commands are consecutive).

HOW TO READ THIS TRANSLATION

When the translation of a production begins with the word "Java", this means that some tokens potentially found in the production require a translation from JJ to Java. For example, the production "Expr" is translated to "JavaExpr" because a few of the JJ tokens require a translation to Java, such as changing the word "and" to the characters "&&". The productions that begin with the word "Java" (JavaPrimitiveTypeCategory, JavaClassTypeCategory, JavaScalarBoolExpr, JavaLhsExpr, JavaFileIORoutineReference, RoutineReferenceOrAssignment, StringConversionAssignment) can be found on the bottom of this file.

Let the translation begin

..
----------------------------------------------------------
--
-- Box and Boxes Commands
--
----------------------------------------------------------

ClassBoxCommands :=

 JJ:   (1) 'Box' <boxname> 'ofType' PrimitiveTypeCategory 'is' Visibility
 Java:     Visibility JavaPrimitiveTypeCategory <boxname> ';'

 JJ:   (2) 'Box' <boxname> 'ofClass' ClassTypeCategory 'is' Visibility
 Java:     Visibility JavaClassTypeCategory <boxname> ';'

ClassBoxesCommands :=

 JJ:   (1) 'Boxes' <boxname> (',' <boxname>)+ 'ofType' PrimitiveTypeCategory 'are' Visibility
 Java:     Visibility JavaPrimitiveTypeCategory <boxname> (',' <boxname>)+ ';'

 JJ:   (2) 'Boxes' <boxname> (',' <boxname>)+ 'ofClass' ClassTypeCategory 'are' Visibility
 Java:     Visibility JavaClassTypeCategory <boxname> (',' <boxname>)+ ';'

LocalBoxCommands :=

 JJ:   (1) 'Box' <boxname> 'ofType' PrimitiveTypeCategory
 Java:     JavaPrimitiveTypeCategory <boxname> ';'

 JJ:   (2) 'Box' <boxname> 'ofClass' ClassTypeCategory
 Java:     JavaClassTypeCategory <boxname> ';'

LocalBoxesCommands :=

 JJ:   (1) 'Boxes' <boxname> (',' <boxname>)+ 'ofType' PrimitiveTypeCategory
 Java:     JavaPrimitiveTypeCategory <boxname> (',' <boxname>)+ ';'

 JJ:   (2) 'Boxes' <boxname> (',' <boxname>)+ 'ofClass' ClassTypeCategory
 Java:     JavaClassTypeCategory <boxname> (',' <boxname>)+ ';'

----------------------------------------------------------
--
-- Import Command
--
----------------------------------------------------------

ImportCommand
 JJ:   'Import' ImportLib
 Java: // 'Import' ImportLib
  or
       import ImportLib ';'

----------------------------------------------------------
--
-- Class and EndClass Commands
--
----------------------------------------------------------

ClassCommand := 
 JJ:   'Class' <classname>
 Java: 'class' <classname> '{'

EndClassCommand := 
 JJ:   'EndClass' <classname>
 Java: '}'

----------------------------------------------------------
--
-- Constant Command
--
----------------------------------------------------------

ConstantCommand :=
 JJ:   'Constant' <constantname> '=' Value 'ofType' PrimitiveTypeCategory 'is' Visibility
 Java: Visibility final JavaPrimitiveTypeCategory <constantname> '=' Value ';'

----------------------------------------------------------
--
-- Invariant and EndInvariant Commands
--
----------------------------------------------------------

InvariantCommand :=
 JJ:   'Invariant'
 Java: 'private' 'void' 'JJinvariant' '()' '{'

EndInvariantCommand :=
 JJ:   'EndInvariant'
 Java: '}'

----------------------------------------------------------
--
-- Check, PreCheck, PostCheck Commands
--
----------------------------------------------------------

CheckCommand :=
 JJ:   'Check' ScalarBoolExpr 'bounce' StringLiteral 
 Java: 'if' '(' JavaScalarBoolExpr ')' '{}' 'else' '{' 'jjHandleBouncedCheck' '(' StringLiteral ')' ';' '}'

PreCheckCommand :=
 JJ:   'Check' ScalarBoolExpr 'bounce' StringLiteral 
 Java: 'if' '(' JavaScalarBoolExpr ')' '{}' 'else' '{' 'jjHandleBouncedPreCheck' '(' StringLiteral ')' ';' '}'

PostCheckCommand :=
 JJ:   'Check' ScalarBoolExpr 'bounce' StringLiteral 
 Java: 'if' '(' JavaScalarBoolExpr ')' '{}' 'else' '{' 'jjHandleBouncedPostCheck' '(' StringLiteral ')' ';' '}'


----------------------------------------------------------
--
-- Constructor and EndConstructor Commands
--
----------------------------------------------------------

ConstructorCommand :=
 JJ:   'Constructor' <classname>  '(' SlotDeclList ') is public'
 Java: 'public' <classname> '(' 
       SlotDeclList  (* See translation of Slot commands *)
       ')' '{'  (* Put at the end of the translation of Slot commands *)

EndConstructorCommand := 
 JJ:   'EndConstructor' <classname>
 Java: '}'

----------------------------------------------------------
--
-- Routine and EndRoutine Commands
--
----------------------------------------------------------

RoutineCommand := 
 JJ:   'Routine' <routinename> '(' SlotDeclList ') is' Visibility
 Java: Visibility <routinename> '(' 
       SlotDeclList  (* See translation of Slot commands *)
       ')' '{'  (* Put at the end of the translation of Slot commands *)

EndRoutineCommand := 
 JJ:  'EndRoutine' <routinename>
 Java: '}'

----------------------------------------------------------
--
-- Function and EndFunction Commands
--
----------------------------------------------------------

FunctionCommand :=

 JJ:   (1) 'Function' <functionname> 'ofType' PrimitiveTypeCategory '(' SlotDeclList ') is' Visibility
 Java:     JavaPrimitiveTypeCategory <functionname> '('
           SlotDeclList  (* See translation of Slot commands *)
           ')' '{'  (* Put at the end of the translation of Slot commands *)

 JJ:   (2) 'Function' <functionname> 'ofClass' ClassTypeCategory '(' SlotDeclList ') is' Visibility
 Java:     JavaClassTypeCategory <functionname> '('
           SlotDeclList  (* See translation of Slot commands *)
           ')' '{'  (* Put at the end of the translation of Slot commands *)

EndFunctionCommand :=
 JJ:   'EndFunction' <functionname>
 Java: '}'

----------------------------------------------------------
--
-- Slot Command
--
----------------------------------------------------------

SlotCommand :=

 JJ:   (1) 'Slot' <slotname> 'ofType' PrimitiveTypeCategory
 Java:     JavaPrimitiveTypeCategory <boxname> 
            ','      (* Comma if not last slot *)
            ')' '{'  (* Close parenthesis, open curly bracket if last slot *)

 JJ:   (2) 'Slot' <slotname> 'ofClass' ClassTypeCategory
 Java:     JavaClassTypeCategory <boxname>
            ','      (* Comma if not last slot *)
            ')' '{'  (* Close parenthesis, open curly bracket if last slot *)

----------------------------------------------------------
--
-- If, ElseIf, Else and EndIf Commands
--
----------------------------------------------------------

IfCommand := 
 JJ:   'If' ScalarBoolExpr 'then'
 Java: 'if' '(' JavaScalarBoolExpr ')' '{'

ElseIfCommand :=
 JJ:   'ElseIf' ScalarBoolExpr 'then'
 Java: '}' 'else' 'if' '(' JavaScalarBoolExpr ')' '{'

ElseCommand :=
 JJ:   'Else'
 Java: '}' 'else' '{'

EndIfCommand :=
 JJ:   'EndIf'
 Java: '}'

----------------------------------------------------------
--
-- Repeat, ExitOn and EndRepeat Commands
--
----------------------------------------------------------

RepeatCommand :=
 JJ:   'Repeat'
 Java: 'for (;;) {'

ExitOnCommand :=
 JJ:   'ExitOn' ScalarBoolExpr
 Java: 'if' '(' JavaScalarBoolExpr ')' '{' 'break' ';' '}'

EndRepeatCommand :=
 JJ:   'EndRepeat'
 Java: '}'

----------------------------------------------------------
--
-- Set Command
--
----------------------------------------------------------

SetCommand :=
 JJ:   'Set' LhsExpr '=' Expr
 Java: JavaLhsExpr '=' JavaExpr ';'

----------------------------------------------------------
--
-- Call Command
--
----------------------------------------------------------

CallCommand :=

 JJ:   (1) 'Call' <routinename> ['with' '(' Expr (',' Expr)* ')']
 Java:     <routinename> '(' [JavaExpr (',' JavaExpr)*] ')'

 JJ:   (2) 'Call' <classboxname>.<routinename> ['with' '(' Expr (',' Expr)* ')']
 Java:     <classboxname>.<routinename> '(' [JavaExpr (',' JavaExpr)*] ')'

----------------------------------------------------------
--
-- NewArray Command
--
----------------------------------------------------------

NewArrayCommand :=

 JJ:   (1) 'NewArray' <arrayname> 'ofType' PrimitiveTypeCategory '[' ScalarIntExpr ']'
 Java:     <arrayname> '=' 'new' JavaPrimitiveTypeCategory '[' JavaScalarIntExpr ']'

 JJ:   (2) 'NewArray' <arrayname> 'ofClass' ClassTypeCategory '[' ScalarIntExpr ']'
 Java:     <arrayname> '=' 'new' JavaClassTypeCategory '[' JavaScalarIntExpr ']'

----------------------------------------------------------
--
-- NewArray Command
--
----------------------------------------------------------

NewCommand :=
 JJ:   'New' LhsExpr 'ofClass' ClassTypeCategory ['with' '(' Expr (',' Expr)* ')']
 Java: JavaLhsExpr '=' new JavaClassTypeCategory '(' [JavaExpr (',' JavaExpr)*] ')'

----------------------------------------------------------
--
-- Input, Output and Outputln Commands
--
----------------------------------------------------------

InputCommand :=
 JJ:   'Input' <varname>
 Java: 'JJSystemHelper.input' '(' <varname> ')'

OutputCommand :=
 JJ:   'Output' Expr
 Java: 'JJSystemHelper.output' '(' JavaExpr ')'

OutputlnCommand :=
 JJ:   'Outputln' Expr
 Java: 'JJSystemHelper.outputln' '(' JavaExpr ')'

----------------------------------------------------------
--
-- Debug and Debugln Commands
--
----------------------------------------------------------

DebugCommand :=
 JJ:   'Debug' Expr
 Java: 'System.out.print' '(' JavaExpr ')'

DebuglnCommand :=
 JJ:   'Debugln' Expr
 Java: 'System.out.println' '(' JavaExpr ')'

----------------------------------------------------------
--
-- TryCall Command
--
----------------------------------------------------------

TryCallCommand := 
 JJ:   'TryCall' FileIORoutineReference 'OnFail' RoutineReferenceOrAssignment
 Java: 'if' JavaFileIORoutineReference '{}' 'else' '{' JavaRoutineReferenceOrAssignment '}'

----------------------------------------------------------
--
-- TrySet Command
--
----------------------------------------------------------

TrySetCommand :=
 JJ:   'TrySet' StringConversionAssignment 'OnFail' RoutineReferenceOrAssignment
 Java: 'if' JavaStringConversionAssignment '{}' 'else' '{' JavaRoutineReferenceOrAssignment '}'


==========================================================
==
== Productions that are not Commands that need translation
== from JJ to Java.
==
==========================================================
----------------------------------------------------------
--
-- PrimitiveTypeCategory
--
----------------------------------------------------------

PrimitiveTypeCategory :=

 JJ:   'int'
 Java: 'int'

 JJ:   'real'
 Java: 'double'

 JJ:   'bool'
 Java: 'boolean'

 JJ:   'int[]'
 Java: 'int[]'

 JJ:   'real[]'
 Java: 'double[]'

 JJ:   'bool[]'
 Java: 'boolean[]'

----------------------------------------------------------
--
-- ClassTypeCategory
--
----------------------------------------------------------
ClassTypeCategory :=

 JJ:   'Str'
 Java: 'String'

 JJ:   <classname>
 Java: <classname>

 JJ:   'Str[]'
 Java: 'String[]'

 JJ:   <classname>'[]'
 Java: <classname>'[]'

----------------------------------------------------------
--
-- JavaScalarBoolExpr
--
----------------------------------------------------------
JavaScalarBoolExpr :=
 (* See Expr translation rules *)

----------------------------------------------------------
--
-- LhsExpr
--
----------------------------------------------------------
LhsExpr :=
 (* See Expr translation rules *)

----------------------------------------------------------
--
-- Expr
--
----------------------------------------------------------
Expr :=

 JJ:   <boxname>
 Java: <boxname>

 JJ:   <classboxname>.<boxname>
 Java: <classboxname>.<boxname>

 JJ:   'and'
 Java: '&&'

 JJ:   'or'
 Java: '||'

 JJ:   '+'
 Java: '+'

 JJ:   '-'
 Java: '-'

 JJ:   '*'
 Java: '*'

 JJ:   '/'
 Java: '/'

 JJ:   '%'
 Java: '%'

 JJ:   '<'
 Java: '<'

 JJ:   '<='
 Java: '<='

 JJ:   '>'
 Java: '>'

 JJ:   '>='
 Java: '>='

 JJ:   '=='
 Java: '=='

 JJ:   '!='
 Java: '!='

 JJ:   'not'
 Java: '!'

 JJ:   '('
 Java: '('

 JJ:   ')'
 Java: ')'

 JJ:   '['
 Java: '['

 JJ:   ']'
 Java: ']'

----------------------------------------------------------
--
-- FileIORoutineReference
--
----------------------------------------------------------
FileIORoutineReference :=
 JJ:   <classboxname>.<routinename> ['with' '(' Expr (',' Expr)* ')']
 Java: <classboxname>.<routinename> '(' [JavaExpr (',' JavaExpr)*] ')'

----------------------------------------------------------
--
-- RoutineReferenceOrAssignment
--
----------------------------------------------------------
RoutineReferenceOrAssignment :=
 JJ:   (1) <routinename> ['with' '(' Expr (',' Expr)* ')']
 Java:     <routinename> '(' [JavaExpr (',' JavaExpr)*] ')'

 JJ:   (2) <classboxname>.<routinename> ['with' '(' Expr (',' Expr)* ')']
 Java:     <classboxname>.<routinename> '(' [JavaExpr (',' JavaExpr)*] ')'

 JJ:   (3) LhsExpr '=' Expr
 Java:     JavaLhsExpr '=' JavaExpr

----------------------------------------------------------
--
-- StringConversionAssignment
--
----------------------------------------------------------
StringConversionAssignment :=
 JJ:   (1) <boxname> '=' 'StringToInt' '(' ScalarStrExpr ')'
 Java:     <boxname> '=' SomeJavaCode '(' JavaScalarStrExpr ')'

 JJ:   (2) <boxname> '=' 'StringToReal' '(' ScalarStrExpr ')'
 Java:     <boxname> '=' SomeJavaCode '(' JavaScalarStrExpr ')'
Return to
    JJ
home page
..