REJO grammar

This pagepresensents the REJO grammar which is compound by 7 seccions. They are:

  1. Lexical analiser: Defines the tokens used by Java and REJO. In the case of Java it defines the tokens: if, for, while, etc.
  2. Java program structure: Defines.
  3. Java expressions: Defines.
  4. Java instructions: Defines.
  5. REJO program structure: Defines.
  6. REJO expressions: Defines.
  7. REJO instructions: Defines.

options {
JAVA_UNICODE_ESCAPE = true;
}


PARSER_BEGIN(JavaParser)
package ros.rejo;
import java.io.*;
import java.util.*;
public class JavaParser
{
//-----------------------------------------------------------------------------------------
// Parser Vars
//-----------------------------------------------------------------------------------------
static String Rinst; // Les instructions reactives
static String HandTermin; // Les instructions reactives du handler Termin
static String HandFreeze; // Les instructions reactives du handler Freeze
static String HandWarmUp; // Les instructions reactives du handler WarmUp
static boolean bTermin=false; // Les instructions reactives du handler Termin
static boolean bFreeze=false; // Les instructions reactives du handler Freeze
static boolean bWarmUp=false; // Les instructions reactives du handler WarmUp
static String ClaOfVar; // La class pour les var de la func react et les param
static String NameRMet; // Le nom de la class precedent. Ex "_rmain"
static String NameRClass; // Le nom de la class precedent. Ex "_rmain"
static int ClassId=0; // For identifying methodes overcharged.
static boolean rmainRos=false; // For identifying the rmain method with one parameter: String[]
static String ConfigVar=""; // Pour les conditions en Wait, When, Until.
static String StrGlob1=""; // Pour utiliser Expressions dans les inst reactives If, Gen, etc.
static boolean isAtom = false; // pour savoir si on est dans une inst atomique et mettre le prefix adequat
static int Reac = 0; // Pour savoir qu'est-ce qu'on parse.
static boolean parsPar = false; // Pour utiliser les meme functions de types.
static boolean asig = false; // pour ne pas prendre un ";" quand il n'y a pas asignation
static String LisTypes = ""; // String with the types of the method that is beeing parsed.
static String LisParen = ""; // String with the types of the method that is beeing parsed.
static int lp = 0;
static String LisNames = ""; // String with the names of the method that is beeing parsed.
static String LisVarMain = ""; // String with the names of vars in rmain.
static String AgentName; // For putting the agent name in function of the first class name.
static int ver; // For knowing which code we gonna generate SC or Junior code.
static int ros=0; // For knowing if we gonna add Agent.txt file and Handlers.
static int nAtoms, nHand;
static Hashtable strAtms = new Hashtable();
static boolean RejoClass=false; // For adding Agent.txt and ... in the correct class.
static boolean NestClass=false; // For avoiding putting Agent.txt in Nested classes.
static JavaParser JP; // Pointer to the parser for telling it when print control characters like \n.
static boolean cond=false; // Pour generer un erreur lors qu'on utilise un eve value en Wait
static boolean cte=true; // For not generating a new atomic class when ctes are used.
static String prefAtom = "Jrer.Atom(new Action(){ public void execute(Environment env)\n{\n";
static String spaAtom = " ";
static int inden=0;

 

//-----------------------------------------------------------------------------------------
// Parser Methods
//-----------------------------------------------------------------------------------------

 

public static void main(String args[])
{
 JavaParser parser;

  if (args.length >= 1 && args.length <= 3 ) { // f.rejo -sc -ag
    try {
      if(args.length >= 2){
        ver=1; // ver = Junior,
        ros=0;
        if(args[1].equals("-sc")) ver=2;
        if(args[1].equals("-ros")) ros=1;
        if(args.length == 3){
          if(args[2].equals("-sc")) ver=2;
          if(args[2].equals("-ros")) ros=1;
        }
        parser = new JavaParser(new java.io.FileInputStream( new File(args[0]) ));
        parser.CompilationUnit();
        parser.JP = parser;
        System.exit(0);}
      else{
          ver=1;
          ros=0;
          parser = new JavaParser(new java.io.FileInputStream( new  File(args[0]) ));
          parser.CompilationUnit();
          parser.JP = parser;
          System.exit(0);
      }
    } catch (ParseException e) {
        System.err.println(e.getMessage());
        System.exit(3);
    } catch (java.io.FileNotFoundException e) {
        System.out.println(e.getMessage());
        System.out.println("Java Parser Version 1.1: File " + args[0] + " not found.");
        System.exit(1);
    }
  } else {
      System.out.println("Java Parser Version 1.1");
      System.out.println(" SYNTAX> java JavaParser inputfile");
      System.exit(2);
  }
}

 

static void action(int i)
{
 Token t=getToken(0);
 String aux;

   switch(i){
      case -1:
              aux = " "+t.image;
              break;
      case 1:
              aux = t.image+" ";
              break;
      default:
              aux = t.image;
   }

   switch(Reac){
      case 1:
              Rinst += aux;
              break;
      case 2:
              StrGlob1 += aux;
              break;
      default:
              System.out.print(aux);
   }
}

 

static void PutVarsMet()
{
 Reader in;
 String s="";
 int ch;

   if(ros==0)
     return;

   try{
     s = System.getProperty("user.home")+"/ros/kernel/AgentImpl1.txt";
     in = new FileReader(s);
     while ( (ch=in.read()) != -1)
       System.out.print( (char)ch );
   } catch(IOException e) {
      System.out.println("Not found: AgentImpl1.txt");
      System.out.println("path = "+ s);
   }
}

 

static void PutHandlers()
{
  . . .
}

 

static int testAtom(int i, StringBuffer Raux1, StringBuffer Raux2)
{
  if( isAtom ){
    isAtom=false;
    Rinst = Atom(StrGlob1);

    i++;
    if(i==1)
       Raux1.replace(0, Raux1.length(), Rinst);
    else if(i==2){
       Raux2.replace(0, Raux2.length(), "Jrer.Seq(" + Raux1.toString() + "," );
       Raux1.replace(0, Raux1.length(), Rinst);
    }else{
       Raux2.append( "Jrer.Seq(" + inden(inden) + Raux1.toString() + "," );
       Raux1.replace(0, Raux1.length(), Rinst);
    }
    Rinst="";
  }
  return i;
}

 

  static String Atom(String javaCode)
  {
    nAtoms++;
    strAtms.put( nAtoms + "", javaCode);
    return "Jrer.Atom(new Actions(locVar, "+ nAtoms +"))";
  }

 

  static String AtomHand(String javaCode)
  {
    return AtomInLine(javaCode);
  }

 

  static String AtomInLine(String javaCode)
  {
    return "Jrer.Atom(new Action(){ public void execute(Environment env)\n"+
            "{\n"+ javaCode + "}})";
  }

 

  static String inden(int spa)
  {
    String s="\n";
    int lim=spa*8;
    for(int i=1; i<=lim; i++)
      s += " ";
    return s;
  }
}
PARSER_END(JavaParser)


/*-------------------------------------------------------------------------------------------------------------
   Lexical analiser
-------------------------------------------------------------------------------------------------------------*/
TOKEN_MGR_DECLS:
{
  static int reac=0;
  static boolean copy=false;
  static boolean japar=false;
  static JavaParser JP;

 

  static void set(int r)
  {
    reac=r;
  }

 

  static void setJaPar(boolean r)
  {
    japar=r;
  }

 

  static void setCopy(boolean r, JavaParser jp )
  {
    copy=r;
    JP=jp;
  }
}

 

/* WHITE SPACE */
SKIP :
{
" " { if(reac == 0 ) System.out.print(" "); if(copy == true) JP.Rinst += " "; if(japar == true) JP.StrGlob1 += " "; }
| "\t" { if(reac == 0 ) System.out.print("\t"); if(copy == true) JP.Rinst += "\t"; if(japar == true) JP.StrGlob1 += "\t"; }
| "\n" { if(reac == 0 ) System.out.print("\n"); if(copy == true) JP.Rinst += "\n"; if(japar == true) JP.StrGlob1 += "\n"; }
| "\r" { if(reac == 0 ) System.out.print("\r"); if(copy == true) JP.Rinst += "\r"; if(japar == true) JP.StrGlob1 += "\r"; }
| "\f" { if(reac == 0 ) System.out.print("\f"); if(copy == true) JP.Rinst += "\f"; if(japar == true) JP.StrGlob1 += "\f"; }
}

 

/* COMMENTS */
MORE :
{
"//" : IN_SINGLE_LINE_COMMENT
|
<"/**" ~["/"]> { input_stream.backup(1); } : IN_FORMAL_COMMENT
|
"/*" : IN_MULTI_LINE_COMMENT
}
<IN_SINGLE_LINE_COMMENT>

 

SPECIAL_TOKEN :
{
<SINGLE_LINE_COMMENT: "\n" | "\r" | "\r\n" > : DEFAULT
}
<IN_FORMAL_COMMENT>

 

SPECIAL_TOKEN :
{
<FORMAL_COMMENT: "*/" > : DEFAULT
}
<IN_MULTI_LINE_COMMENT>

 

SPECIAL_TOKEN :
{
<MULTI_LINE_COMMENT: "*/" > : DEFAULT
}
<IN_SINGLE_LINE_COMMENT,IN_FORMAL_COMMENT,IN_MULTI_LINE_COMMENT>

 

MORE :
{
< ~[] >
}

 

/* RESERVED WORDS AND LITERALS */
TOKEN :
{
< ABSTRACT: "abstract" >
| < BOOLEAN: "boolean" >
| < BREAK: "break" >
| < BYTE: "byte" >
| < CASE: "case" >
| < CATCH: "catch" >
| < CHAR: "char" >
| < CLASS: "class" >
| < CONST: "const" >
| < CONTINUE: "continue" >
| < _DEFAULT: "default" >
| < DO: "do" >
| < DOUBLE: "double" >
| < ELSE: "else" >
| < EXTENDS: "extends" >
| < FALSE: "false" >
| < FINAL: "final" >
| < FINALLY: "finally" >
| < FLOAT: "float" >
| < FOR: "for" >
| < GOTO: "goto" >
| < IF: "if" >

 

| < WHEN: "when" > //----->
| < LOOP: "loop" >
| < REPEAT: "repeat" >
| < FREEZ: "freezable" >
| < UNTIL: "until" >
| < HAND: "handler" >
| < PAR: "par" >
| < LOCAL: "local" >
| < LINK: "link" >
| < CTRL: "control" >
| < STOP: "stop" >
| < MIGRATE: "migrate" >
| < GEN: "gen" >
| < GENERATE: "generate" >
| < WAIT: "wait" >
| < PRNATM: "print" >
| < PRNLATM: "println" >
| < CALL: "call" >
| < RUN: "run" >
| < ATOM: "atom" >
| < CURVAL: "currentValues" >
| < PREVAL: "previousValues" >
| < BEHAVIOR: "behavior" >
| < REAC: "reactive" > //----->
| < IMPLEMENTS: "implements" >
| < IMPORT: "import" >
| < INSTANCEOF: "instanceof" >
| < INT: "int" >
| < INTERFACE: "interface" >
| < LONG: "long" >
| < NATIVE: "native" >
| < NEW: "new" >
| < NULL: "null" >
| < PACKAGE: "package">
| < PRIVATE: "private" >
| < PROTECTED: "protected" >
| < PUBLIC: "public" >
| < RETURN: "return" >
| < SHORT: "short" >
| < STATIC: "static" >
| < SUPER: "super" >
| < SWITCH: "switch" >
| < SYNCHRONIZED: "synchronized" >
| < THIS: "this" >
| < THROW: "throw" >
| < THROWS: "throws" >
| < TRANSIENT: "transient" >
| < TRUE: "true" >
| < TRY: "try" >
| < VOID: "void" >
| < VOLATILE: "volatile" >
| < WHILE: "while" >
}

 

/* LITERALS */
TOKEN :
{
< INTEGER_LITERAL:
<DECIMAL_LITERAL> (["l","L"])?
| <HEX_LITERAL> (["l","L"])?
| <OCTAL_LITERAL> (["l","L"])?
>
|
< #DECIMAL_LITERAL: ["1"-"9"] (["0"-"9"])* >
|
< #HEX_LITERAL: "0" ["x","X"] (["0"-"9","a"-"f","A"-"F"])+ >
|
< #OCTAL_LITERAL: "0" (["0"-"7"])* >
|
< FLOATING_POINT_LITERAL:
(["0"-"9"])+ "." (["0"-"9"])* (<EXPONENT>)? (["f","F","d","D"])?
| "." (["0"-"9"])+ (<EXPONENT>)? (["f","F","d","D"])?
| (["0"-"9"])+ <EXPONENT> (["f","F","d","D"])?
| (["0"-"9"])+ (<EXPONENT>)? ["f","F","d","D"]
>
|
< #EXPONENT: ["e","E"] (["+","-"])? (["0"-"9"])+ >
|
< CHARACTER_LITERAL:
"'"
( (~["'","\\","\n","\r"])
| ("\\"
( ["n","t","b","r","f","\\","'","\""]
| ["0"-"7"] ( ["0"-"7"] )?
| ["0"-"3"] ["0"-"7"] ["0"-"7"]
)
)
)
"'"
>
|
< STRING_LITERAL:
"\""
( (~["\"","\\","\n","\r"])
| ("\\"
( ["n","t","b","r","f","\\","'","\""]
| ["0"-"7"] ( ["0"-"7"] )?
| ["0"-"3"] ["0"-"7"] ["0"-"7"]
)
)
)*
"\""
>
}

 

/* IDENTIFIERS */
TOKEN :
{
< IDENTIFIER: <LETTER> (<LETTER>|<DIGIT>)* >
// {System.out.print(image.toString());}
|
< #LETTER:
[
"\u0024",
"\u0041"-"\u005a",
"\u005f",
"\u0061"-"\u007a",
"\u00c0"-"\u00d6",
"\u00d8"-"\u00f6",
"\u00f8"-"\u00ff",
"\u0100"-"\u1fff",
"\u3040"-"\u318f",
"\u3300"-"\u337f",
"\u3400"-"\u3d2d",
"\u4e00"-"\u9fff",
"\uf900"-"\ufaff"
]
>
|
< #DIGIT:
[
"\u0030"-"\u0039",
"\u0660"-"\u0669",
"\u06f0"-"\u06f9",
"\u0966"-"\u096f",
"\u09e6"-"\u09ef",
"\u0a66"-"\u0a6f",
"\u0ae6"-"\u0aef",
"\u0b66"-"\u0b6f",
"\u0be7"-"\u0bef",
"\u0c66"-"\u0c6f",
"\u0ce6"-"\u0cef",
"\u0d66"-"\u0d6f",
"\u0e50"-"\u0e59",
"\u0ed0"-"\u0ed9",
"\u1040"-"\u1049"
]
>
}

 

/* SEPARATORS */
TOKEN :
{
< LPAREN: "(" >
| < RPAREN: ")" >
| < LBRACE: "{" >
| < RBRACE: "}" >
| < LBRACKET: "[" >
| < RBRACKET: "]" >
| < SEMICOLON: ";" >
| < COMMA: "," >
| < DOT: "." >
}

 

/* OPERATORS */
TOKEN :
{
< ASSIGN: "=" >
| < GT: ">" >
| < LT: "<" >
| < BANG: "!" >
| < TILDE: "~" >
| < HOOK: "?" >
| < COLON: ":" >
| < EQ: "==" >
| < LE: "<=" >
| < GE: ">=" >
| < NE: "!=" >
| < SC_OR: "||" >
| < SC_AND: "&&" >
| < INCR: "++" >
| < DECR: "--" >
| < PLUS: "+" >
| < MINUS: "-" >
| < STAR: "*" >
| < SLASH: "/" >
| < BIT_AND: "&" >
| < BIT_OR: "|" >
| < XOR: "^" >
| < REM: "%" >
| < LSHIFT: "<<" >
| < RSIGNEDSHIFT: ">>" >
| < RUNSIGNEDSHIFT: ">>>" >
| < PLUSASSIGN: "+=" >
| < MINUSASSIGN: "-=" >
| < STARASSIGN: "*=" >
| < SLASHASSIGN: "/=" >
| < ANDASSIGN: "&=" >
| < ORASSIGN: "|=" >
| < XORASSIGN: "^=" >
| < REMASSIGN: "%=" >
| < LSHIFTASSIGN: "<<=" >
| < RSIGNEDSHIFTASSIGN: ">>=" >
| < RUNSIGNEDSHIFTASSIGN: ">>>=" >
}


/*--------------------------------------------------------------------------------------
     Program structuring syntax follows.
--------------------------------------------------------------------------------------*/

 

void CompilationUnit() :
{}
{
  [ PackageDeclaration() ]
  ( ImportDeclaration() )* {
                             System.out.println("import ros.rejo.*;");
                             System.out.println("import junior.*;");
                             System.out.println("import java.util.Hashtable;\n");
                           }
  ( TypeDeclaration() )*
  <EOF>
}

 

void PackageDeclaration() :
{}
{
  <PACKAGE>   { action(0); }
    Name()
  <SEMICOLON> { action(0); }
}

 

void ImportDeclaration() :
{}
{
  <IMPORT>      { action(0); }
    Name()
  [ <DOT>       { action(0); }
   <STAR>       { action(0); }
  ] <SEMICOLON> { action(0); }
}

 

void TypeDeclaration() :
{}
{
  LOOKAHEAD( ( "abstract" | "final" | "public" )* "class" )
    ClassDeclaration()
  | InterfaceDeclaration()
  | <SEMICOLON>           { action(0); }
}

 

/*----------------------------------------------------------------------------
Declaration syntax follows.
----------------------------------------------------------------------------*/

 

void ClassDeclaration() :
{}
{
  ( <ABSTRACT> { action(1); }
  | <FINAL>    { action(1); }
  | <PUBLIC>   { RejoClass = true; action(1); }
  )*
  UnmodifiedClassDeclaration() { RejoClass = false; }
}

 

void UnmodifiedClassDeclaration() :
{}
{
  <CLASS>        { action(0); }
  <IDENTIFIER>   {
                   if(RejoClass && !NestClass ){
                     AgentName=getToken(0).image;
                   }
                   action(0);
                 }
  [ <EXTENDS>    { action(0); }
    Name() ]
  [ <IMPLEMENTS> { action(0); }
    NameList()
  ] ClassBody(1)
}

 

void ClassBody(int i) :
{}
{
  <LBRACE> { action(0); }
  ( ClassBodyDeclaration() )*
  <RBRACE> { if(i==1){
               if(RejoClass && !NestClass ){
                 PutVarsMet();
                 PutHandlers();
               }
             }
             action(0);
           }
}

 

void NestedClassDeclaration() :
{}
{
  ( <STATIC>    { action(1); }
  | <ABSTRACT>  { action(1); }
  | <FINAL>     { action(1); }
  | <PUBLIC>    { action(1); }
  | <PROTECTED> { action(1); }
  | <PRIVATE>   { action(1); }
  )*            { NestClass=true; }
  UnmodifiedClassDeclaration() { NestClass=false; }
}

 

void ClassBodyDeclaration() :
{}
{
LOOKAHEAD(2)
Initializer()
|
LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* "class" )
NestedClassDeclaration()
|
LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* "interface" )
NestedInterfaceDeclaration()
|
LOOKAHEAD( [ "public" | "protected" | "private" ] Name() "(" )
ConstructorDeclaration()
|
LOOKAHEAD( MethodDeclarationLookahead() )
MethodDeclaration()
|
LOOKAHEAD( RMethodDeclarationLookahead() )
RMethodDeclaration()
|
FieldDeclaration()
}
void MethodDeclarationLookahead() :
{}
{
( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" )*
ResultType() (<IDENTIFIER> | <STOP> | <ATOM>
) "("
}

 

void RMethodDeclarationLookahead() :
{}
{
  ( "public" | "protected" | "private" | "static" | "abstract" | "final" | "native" | "synchronized" )*
  <REAC>
}

 

void InterfaceDeclaration() :
{}
{
  ( <ABSTRACT> { action(1); }
  | <PUBLIC>   { action(1); }
  )* UnmodifiedInterfaceDeclaration()
}

 

void NestedInterfaceDeclaration() :
{}
{
  ( <STATIC>    { action(1); }
  | <ABSTRACT>  { action(1); }
  | <FINAL>     { action(1); }
  | <PUBLIC>    { action(1); }
  | <PROTECTED> { action(1); }
  | <PRIVATE>   { action(1); }
  )*
  UnmodifiedInterfaceDeclaration()
}

 

void UnmodifiedInterfaceDeclaration() :
{}
{
  <INTERFACE>  { action(0); }
  <IDENTIFIER> { action(0); }
  [
    "extends"  { action(0); }
    NameList() ]
  <LBRACE>     { action(0); }
  ( InterfaceMemberDeclaration() )*
  <RBRACE>     { action(0); }
}

 

void InterfaceMemberDeclaration() :
{}
{
LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* "class" )
NestedClassDeclaration()
|
LOOKAHEAD( ( "static" | "abstract" | "final" | "public" | "protected" | "private" )* "interface" )
NestedInterfaceDeclaration()
|
LOOKAHEAD( MethodDeclarationLookahead() )
MethodDeclaration()
|
FieldDeclaration()
}

 

void FieldDeclaration() :
{}
{
  ( <PUBLIC>    { action(1); }
  | <PROTECTED> { action(1); }
  | <PRIVATE>   { action(1); }
  | <STATIC>    { action(1); }
  | <FINAL>     { action(1); }
  | <TRANSIENT> { action(1); }
  | <VOLATILE>  { action(1); }
  )*
  Type() VariableDeclarator() (
  <COMMA>       { action(0); }
  VariableDeclarator() )*
  <SEMICOLON>   { action(0); }
}

 

void VariableDeclarator() :
{}
{
  VariableDeclaratorId() [
  <ASSIGN> { action(0); }
  VariableInitializer() ]
}

 

void VariableDeclaratorId() :
{}
{
(<IDENTIFIER> |<WHEN>|<REPEAT>|<LOOP>|
<UNTIL>|<IF>|<LOCAL>|<STOP>|<PAR>|
<PRNATM>|<PRNLATM>|<CTRL>|<CALL>|
<REAC>|<GEN>|<GENERATE>|<WAIT>|<ATOM>|
<FREEZ>
) { action(0); }
(
<LBRACKET> { action(0); }
<RBRACKET> { action(0); }
)*
}

 

void VariableInitializer() :
{}
{
    ArrayInitializer()
  | Expression()
}

 

void ArrayInitializer() :
{}
{
   <LBRACE>  { action(0); }
  [ VariableInitializer() ( LOOKAHEAD(2)
   ","       { action(0); }
   VariableInitializer() )* ] [
     <COMMA> { action(0); }
   ]
  <RBRACE>   { action(0); }
}

 

void MethodDeclaration() :
{}
{
( <PUBLIC> { action(1); }
| <PROTECTED> { action(1); }
| <PRIVATE> { action(1); }
| <STATIC> { action(1); }
| <ABSTRACT> { action(1); }
| <FINAL> { action(1); }
| <NATIVE> { action(1); }
| <SYNCHRONIZED> { action(1); }
)*
ResultType() MethodDeclarator() [
<THROWS> { action(1); }
NameList() ] ( Block() |
<SEMICOLON> { action(0); }
)
}

 

void MethodDeclarator() :
{}
{
( <IDENTIFIER> | <STOP> | <ATOM> )
{ Token t=getToken(0);
action(0);
if(t.image.equals("Termin"))
bTermin=true;
else if(t.image.equals("Freeze"))
bFreeze=true;
else if(t.image.equals("WarmUp"))
bWarmUp=true;
}
FormalParameters() (
<LBRACKET> { action(0); }
<RBRACKET> { action(0); }
)*
}

 

void FormalParameters() :
{}
{
  <LPAREN> { action(0); }
  [ FormalParameter() (
  <COMMA>  { action(0); }
  FormalParameter() )* ]
  <RPAREN> { action(0); }
}

 

void FormalParameter() :
{}
{
  [ <FINAL> { action(0); }
  ] Type() VariableDeclaratorId()
}

 

void ConstructorDeclaration() :
{ }
{
[ (<PRIVATE> | <PROTECTED> | <PUBLIC>) { action(1); }
] <IDENTIFIER> { action(0); }
FormalParameters() [ "throws" NameList() ]
<LBRACE> { action(0); }
[ LOOKAHEAD(ExplicitConstructorInvocation())
ExplicitConstructorInvocation()
]
( BlockStatement() )*
<RBRACE> { action(0); }
}

 

void ExplicitConstructorInvocation() :
{}
{
LOOKAHEAD("this" Arguments() ";")
"this" { action(0); }
Arguments()
";" { action(0); }
| [ LOOKAHEAD(2)
PrimaryExpression()
"." { action(0); }
]
"super" { action(0); }
Arguments()
";" { action(0); }
}

 

void Initializer() :
{}
{
  [ <STATIC> { action(0); }
  ] Block()
}

 

/*------------------------------------------------------------------------------------------
Type, name
-------------------------------------------------------------------------------------------*/

 

void Type() :
{ }
{
  ( PrimitiveType() | Name() { System.out.print(" ");} )
  (
    <LBRACKET> { action(0); }
    <RBRACKET> { action(0); }
  )*
}

 

void PrimitiveType() :
{}
{
  try {
     <BOOLEAN> | <CHAR> | <BYTE> | <SHORT>
   | <INT> | <LONG> | <FLOAT> | <DOUBLE>
  } finally { action(1); }
}

 

void ResultType() :
{}
{
    <VOID> { action(1); }
  | Type()
}

 

void Name() :
{ }
{
  ( <IDENTIFIER> | <STOP> | <ATOM> )
     { cte=false;
       Token t;
       t=getToken(0);

       if(Reac == 1 ){
          Rinst += t.image;
       }else if( Reac == 2 ){
          StrGlob1 += t.image;
       }else
          System.out.print(t.image);
       }
  ( LOOKAHEAD(3)
    <DOT> { action(0); }
    ( <IDENTIFIER> |<WHEN>|<REPEAT>|<LOOP>|
      <UNTIL>|<IF>|<LOCAL>|<STOP>|<PAR>|
      <PRNATM>|<PRNLATM>|<CTRL>|<CALL>|
      <REAC>|<GEN>|<GENERATE>|<WAIT>|<ATOM>|<FREEZ>
    )
      { action(0); }
  )*
}

 

void NameList() :
{}
{
  Name() (
  <COMMA> { action(0); }
  Name())*
}

 

/*------------------------------------------------------------------------------------------
     Java Expression Syntax
-------------------------------------------------------------------------------------------*/
void Expression() :

{ }
{
   ConditionalExpression()
  [ AssignmentOperator() Expression() ]
}

void AssignmentOperator() :
{}
{
  try
  {   "=" | "*=" | "/=" | "%=" | "+="
    | "-=" | "<<=" | ">>=" | ">>>=" | "&="
    | "^=" | "|="
  } finally { action(0); }
}

 

void ConditionalExpression() :
{}
{
  ConditionalOrExpression() [
  <HOOK> { action(0); }
  Expression()
  <COLON> { action(0); }
  ConditionalExpression() ]
}

 

void ConditionalOrExpression() :
{}
{
  ConditionalAndExpression() (
  <SC_OR> { action(0); }
  ConditionalAndExpression() )*
}

 

void ConditionalAndExpression() :
{}
{
  InclusiveOrExpression() (
  <SC_AND> { action(0); }
  InclusiveOrExpression() )*
}

 

void InclusiveOrExpression() :
{}
{
  ExclusiveOrExpression() (
  <BIT_OR> { action(0); }
  ExclusiveOrExpression() )*
}

 

void ExclusiveOrExpression() :
{}
{
  AndExpression() (
  <XOR> { action(0); }
  AndExpression() )*
}

 

void AndExpression() :
{}
{
  EqualityExpression() (
  <BIT_AND> { action(0); }
  EqualityExpression() )*
}

 

void EqualityExpression() :
{}
{
    InstanceOfExpression() ( (
    <EQ> { action(0); }
  | <NE> { action(0); }
  ) InstanceOfExpression() )*
}

 

void InstanceOfExpression() :
{}
{
  RelationalExpression() [
  <INSTANCEOF> { action(-1); }
  Type() ]
}

 

void RelationalExpression() :
{}
{
  ShiftExpression() ( (
    <LT> { action(0); }
  | <GT> { action(0); }
  | <LE> { action(0); }
  | <GE> { action(0); }
  ) ShiftExpression() )*
}

 

void ShiftExpression() :
{}
{
  AdditiveExpression() ( (
    <LSHIFT> { action(0); }
  | <RSIGNEDSHIFT> { action(0); }
  | <RUNSIGNEDSHIFT> { action(0); }
  ) AdditiveExpression() )*
}

 

void AdditiveExpression() :
{}
{
   MultiplicativeExpression() ( (
    <PLUS> { action(0); }
  | <MINUS> { action(0); }
   ) MultiplicativeExpression() )*
}

 

void MultiplicativeExpression() :
{}
{
   UnaryExpression() ( (
    <STAR> { action(0); }
  | <SLASH> { action(0); }
  | <REM> { action(0); }
  ) UnaryExpression() )*
}

 

void UnaryExpression() :
{}
{
  (
    <PLUS> { action(0); }
  | <MINUS> { action(0); }
  ) UnaryExpression()
  | PreIncrementExpression()
  | PreDecrementExpression()
  | UnaryExpressionNotPlusMinus()
}

 

void PreIncrementExpression() :
{}
{
  <INCR> { action(0); }
  PrimaryExpression()
}

 

void PreDecrementExpression() :
{}
{
  <DECR> { action(0); }
  PrimaryExpression()
}

 

void UnaryExpressionNotPlusMinus() :
{}
{
  (
    <TILDE> { action(0); }
  | <BANG> { action(0); }
  ) UnaryExpression()
  | LOOKAHEAD( CastLookahead() )
    CastExpression()
  | PostfixExpression()
}

 

void CastLookahead() :
{}
{
  LOOKAHEAD(2)
    "(" PrimitiveType()
  | LOOKAHEAD("(" Name() "[")
    "(" Name() "[" "]"
  | "(" Name() ")" ( "~" | "!" | "(" |
    <IDENTIFIER>
  | "this" | "super" | "new" | Literal() )
}

 

void PostfixExpression() :
{}
{
   PrimaryExpression()
 [
   <INCR> { action(0); }
 | <DECR> { action(0); }
 ]
}

 

void CastExpression() :
{}
{
  LOOKAHEAD("(" PrimitiveType())
    <LPAREN> { action(0); }
    Type()
    <RPAREN> { action(0); }
    UnaryExpression()
  |
    <LPAREN> { action(0); }
    Type()
    <RPAREN> { action(0); }
    UnaryExpressionNotPlusMinus()
}

 

void PrimaryExpression() :
{}
{
  PrimaryPrefix() ( LOOKAHEAD(2) PrimarySuffix() )*
}

 

void PrimaryPrefix() :
{}
{
    Literal()
  | <THIS>       { action(0); }
  | <SUPER>      { action(0); }
    <DOT>        { action(0); }
    <IDENTIFIER> { action(0); }
  | <LPAREN>     { action(0); }
    Expression()
    <RPAREN>     { action(0); }
  | AllocationExpression()
  | LOOKAHEAD( ResultType() "." "class" )
    ResultType()
    <DOT>        { action(0); }
    <CLASS>      { action(0); }
  | Name()
}

 

void PrimarySuffix() :
{ }
{
  LOOKAHEAD(2)
    <DOT>        { action(0); }
    <THIS>       { action(0); }
 | LOOKAHEAD(2)
   "."           { action(0); }
   AllocationExpression()
 | <LBRACKET>    { action(0); }
    Expression()
   <RBRACKET>    { action(0); }
 | <DOT>         { action(0); }
    <IDENTIFIER> { action(0); }
 | Arguments()
}

 

void Literal() :
{ }
{
  try {
    <INTEGER_LITERAL>
  | <FLOATING_POINT_LITERAL>
  | <CHARACTER_LITERAL>
  | <STRING_LITERAL>
  | BooleanLiteral()
  | NullLiteral()
  }finally { action(0); }
}

 

void BooleanLiteral() :
{}
{
  "true" | "false"
}

 

void NullLiteral() :
{}
{
  "null"
}

 

void Arguments() :
{}
{
  <LPAREN>   { action(0); }
  [ ArgumentList() ]
    <RPAREN> { action(0); }
}

 

void ArgumentList() :
{}
{
  Expression() (
  <COMMA> { action(0); }
  Expression() )*
}

 

void AllocationExpression() :
{}
{
    <NEW> { action(1); }
    PrimitiveType() ArrayDimsAndInits()
  | <NEW> { action(1); }
    Name() ( ArrayDimsAndInits() |
    Arguments() [ ClassBody(0) ] )
}

 

void ArrayDimsAndInits() :
{}
{
    LOOKAHEAD(2)
  ( LOOKAHEAD(2)
    <LBRACKET> { action(0); }
    Expression()
    <RBRACKET> { action(0); }
  )+
  ( LOOKAHEAD(2)
    <LBRACKET> { action(0); }
    <RBRACKET> { action(0); }
  )*
 |
  ( <LBRACKET> { action(0); }
    <RBRACKET> { action(0); }
  )+ ArrayInitializer()
}

 

/*----------------------------------------------------------------------------------------------
Statement syntax follows.
---------------------------------------------------------------------------------------------*/

 

void Statement() :
{}
{
  LOOKAHEAD(2)
    LabeledStatement()
  | Block()
  | EmptyStatement()
  | StatementExpression() <SEMICOLON> { action(0); }
  | SwitchStatement()
  | ifStatement()
  | WhileStatement()
  | DoStatement()
  | ForStatement()
  | BreakStatement()
  | ContinueStatement()
  | ReturnStatement()
  | ThrowStatement()
  | SynchronizedStatement()
  | TryStatement()
}

 

void LabeledStatement() :
{}
{
  <IDENTIFIER> { action(0); }
  <COLON>      { action(0); }
  Statement()
}

 

void Block() :
{}
{
  <LBRACE> { action(0); }
  ( BlockStatement() )*
  <RBRACE> { action(0); }
}

 

void BlockStatement() :
{}
{
LOOKAHEAD([ "final" ] Type() <IDENTIFIER>)
LocalVariableDeclaration()
<SEMICOLON> { action(0); }
| Statement()
| UnmodifiedClassDeclaration()
| UnmodifiedInterfaceDeclaration()
}

 

void LocalVariableDeclaration() :
{}
{
  [ "final" { action(1); }
  ] Type() VariableDeclarator() (
  ","       { action(0); }
  VariableDeclarator() )*
}

 

void EmptyStatement() :
{}
{
  <SEMICOLON> { action(0); }
}

 

void StatementExpression() :
{ }
{
    PreIncrementExpression()
  | PreDecrementExpression()
  | PrimaryExpression()
  [ <INCR> { action(0); }
  | <DECR> { action(0); }
  | AssignmentOperator() Expression()
  ]
}

 

/*------------------------------------------------------------------------------------
   Java Instructions
--------------------------------------------------------------------------------------*/

 

void SwitchStatement() :
{}
{
  <SWITCH>      { action(0); }
  <LPAREN>      { action(0); }
    Expression()
  <RPAREN>      { action(0); }
  <LBRACE>      { action(0); }
  ( SwitchLabel() ( BlockStatement() )* )*
  <RBRACE>      { action(0); }
}

 

void SwitchLabel() :
{}
{
   <CASE>      { action(0); }
    Expression()
   <COLON>     { action(0); }
 | <_DEFAULT>  { action(0); }
   <COLON>     { action(0); }
}

 

void ifStatement() :
{}
{
  <IF>           { action(0); }
  <LPAREN>       { action(0); }
    Expression()
  <RPAREN>       { action(0); }
    Statement()
  [ LOOKAHEAD(1)
  <ELSE>         { action(0); }
    Statement() ]
}

 

void WhileStatement() :
{}
{
  <WHILE>       { action(0); }
  <LPAREN>      { action(0); }
    Expression()
  <RPAREN>      { action(0); }
  Statement()
}

 

void DoStatement() :
{}
{
  <DO>         { action(0); }
   Statement()
  <WHILE>      { action(0); }
  <LPAREN>     { action(0); }
    Expression()
  <RPAREN>     { action(0); }
  <SEMICOLON>  { action(0); }
}

 

void ForStatement() :
{}
{
  <FOR>          { action(0); }
  <LPAREN>       { action(0); }
  [ ForInit()]
  <SEMICOLON>    { action(0); }
  [ Expression()]
  <SEMICOLON>    { action(0); }
  [ ForUpdate()]
  <RPAREN>       { action(0); }
  Statement()
}

 

void ForInit() :
{}
{
  LOOKAHEAD( [ "final" ] Type() <IDENTIFIER> )
    LocalVariableDeclaration()
  |
    StatementExpressionList()
}

 

void StatementExpressionList() :
{}
{
  StatementExpression() (
  <COMMA> { action(0); }
  StatementExpression() )*
}

 

void ForUpdate() :
{}
{
  StatementExpressionList()
}

 

void BreakStatement() :
{}
{
  <BREAK>        { action(0); }
  [ <IDENTIFIER> { action(0); } ]
  <SEMICOLON>    { action(0); }
}

 

void ContinueStatement() :
{}
{
  <CONTINUE>  { action(0); }
  [ <IDENTIFIER> { action(0); } ]
  <SEMICOLON> { action(0); }
}

 

void ReturnStatement() :
{}
{
  <RETURN>    { action(0); }
  [ Expression() ]
  <SEMICOLON> { action(0); }
}

 

void ThrowStatement() :
{}
{
  <THROW>     { action(0); }
  [ Expression() ]
  <SEMICOLON> { action(0); }
}

 

void SynchronizedStatement() :
{}
{
  <SYNCHRONIZED> { action(0); }
  <LPAREN>       { action(0); }
    Expression()
  <RPAREN>       { action(0); }
    Block()
}

 

void TryStatement() :
{}
{
  <TRY>       { action(0); }
   Block()(
  <CATCH>     { action(0); }
  <LPAREN>    { action(0); }
    FormalParameter()
  <RPAREN>    { action(0); }
    Block())*
 [ <FINALLY>  { action(0); }
   Block() ]
}

 

/*------------------------------------------------------------------------------
  
REJO structure
-------------------------------------------------------------------------------*/

 

void RMethodDeclaration() :
{
nAtoms=0;
inden=0;
}
{
  (
    <PUBLIC>       { action(1); }
  | <PROTECTED>    { action(1); }
  | <PRIVATE>      { action(1); }
  | <STATIC>       { action(1); }
  | <ABSTRACT>     { action(1); }
  | <FINAL>        { action(1); }
  | <NATIVE>       { action(1); }
  | <SYNCHRONIZED> { action(1); }
  )*
  <REAC> { System.out.print("Program "); }
   RMethodDeclarator()
                     { Reac = 1;
                       JavaParserTokenManager.set(Reac);
                     }
   RMBlock()         { Reac = 0;
                       JavaParserTokenManager.set(Reac);
                     }
}

 

void RMethodDeclarator() :
{String aux=""; }
{
<IDENTIFIER> { aux = getToken(0).image;
System.out.print(aux);
NameRMet = aux;
ClassId++;
NameRClass = "_"+ NameRMet +"_"+ ClassId +"_";
ClaOfVar = "";
rmainRos=false;
}
RFormalParameters() (
<LBRACKET>
<RBRACKET> { System.out.print("[]");}
)*
}

 

void RFormalParameters() :
{}
{
<LPAREN> {
lp = 0;
parsPar=true;
LisTypes = "";
LisNames = "";
LisParen = "";
System.out.print("(");
}
[ RFormalParameter() {
LisParen += lp+" ";
// For ROS ---
int i=0;
StringTokenizer tok = new StringTokenizer(ClaOfVar);
String stok1="", stok2="";
while( tok.hasMoreTokens() ){
i++;
if( i==1 )
stok1 = tok.nextToken();
else if( i==2 )
stok2 = tok.nextToken();
else
tok.nextToken();
}

if( NameRMet.equals("rmain") && i==2 )
if( stok1.equals("String[]") || (stok1.equals("String") && stok2.endsWith("[]")) ){
rmainRos=true;
}
// -----------
}
( <COMMA> {
System.out.print(",");
ClaOfVar += ";\n";
lp = 0;
rmainRos=false;
}
RFormalParameter() { LisParen += lp+" "; }
)* ]
<RPAREN> {
parsPar=false;
ClaOfVar += ";\n";
System.out.print(")");
}
}

 

void RFormalParameter() :
{}
{
RType()
[
<FINAL> {
ClaOfVar += "final";
if( parsPar )
System.out.print("final");
}
]
RVariableDeclaratorId()
}

 

void RType() :
{ }
{
( RPrimitiveType() | RName())
(
<LBRACKET>
<RBRACKET> {
ClaOfVar += "[]";
if( parsPar )
System.out.print("[]");
lp++;
}
)*
}

 

void RPrimitiveType() :
{ String aux=""; }
{
try {
<BOOLEAN> | <CHAR> | <BYTE> | <SHORT>
| <INT> | <LONG> | <FLOAT> | <DOUBLE>
}
finally {
aux = getToken(0).image;
ClaOfVar += " " + aux;
if( parsPar ){
System.out.print(aux);
LisTypes += aux + " ";
}
}
}

 

void RName() :
{ String aux=""; }
{
<IDENTIFIER> {
aux = getToken(0).image;
ClaOfVar += " " + aux;
if( parsPar ){
System.out.print(aux);
LisTypes += aux + " ";
}
}
(
<DOT> <IDENTIFIER> {
aux = getToken(0).image;
ClaOfVar += "." + aux;
if( parsPar )
System.out.print(aux);
}
)*
}

 

String RVariableDeclaratorId() :
{ String aux=""; }
{
<IDENTIFIER> {
aux = getToken(0).image;
ClaOfVar += " " + aux;
if( parsPar ){
System.out.print(aux);
LisVarMain = " "+aux;
LisNames += aux + " ";
}
}
(
<LBRACKET>
<RBRACKET> {
ClaOfVar += "[]";
if( parsPar )
System.out.print("[]");
lp++;
}
)* { return aux; }
}

 

void RMBlock() :
{}
{
<LBRACE> {
System.out.println("{");
Rinst="";
}
SeqStatements()
<RBRACE>
{
String s=null;
String allvars="", allasig="";
s = " class "+ NameRClass +" implements WrappersAndActions \n";
s += " { \n";
s += ClaOfVar;
s += " \n";
s += " "+ NameRClass +"(";
// Now, we put the parameters in the reactive java method and the class
// This code gets the parameters of the reactive methode
StringTokenizer tok1 = new StringTokenizer(LisTypes);
StringTokenizer tok2 = new StringTokenizer(LisNames);
StringTokenizer tok3 = new StringTokenizer(LisParen);
String stok1, stok2, stok3, nPar;
int p=0;
while( tok1.hasMoreTokens() ){
stok1 = tok1.nextToken();
stok2 = tok2.nextToken();
stok3 = tok3.nextToken();
nPar = "";
if(!stok3.equals("0")){
int n = Integer.parseInt(stok3);
for(int j=1; j<=n; j++)
nPar += "[]";
}
allasig += " this."+stok2+"="+stok2+";\n";
p++;
if(p==1){
s += stok1+ nPar +" "+stok2;
allvars += stok2;
} else{
s += ", "+stok1+ nPar +" "+stok2;
allvars += ", "+stok2;
}
}
s += ")\n";
s += " { \n";
s += allasig;
s += " } \n";
s += " \n";
s += " long getLong=0; \n";
s += " public long getLong(){ return getLong; } \n";
s += " \n";
s += " public Object actions(Environment env, int _i_) \n";
s += " { \n";
s += " Object o=this; \n";
if( nAtoms >0 ){
s += " switch(_i_){ \n";
for(int j=1; j<=nAtoms; j++){
s += " case "+j+": \n";
s += (String) strAtms.get(j+"");
s += " break; \n";
}
s += " } \n";
}
s += " return o; \n";
s += " } \n";
s += " } \n";
if(NameRMet.equals("rmain") && ros==1 && rmainRos )
System.out.println(" dataAg.setOrgName(\""+ AgentName +"\"); ");
System.out.println(" "+ NameRClass +" locVar = new "+ NameRClass +"("+ allvars +");\n");
System.out.println(" return //--- Begin Body ---\n"+ Rinst +"\n; //--- End Body ---\n");
System.out.println(" }\n"); // Finir la method reactive
System.out.println( s );
}
}

 

void RStatementS() :
{}
{
RStatement(0, new StringBuffer(), new StringBuffer())
{
if( isAtom ){
isAtom=false;
Rinst += Atom(StrGlob1);
}
}
| <LBRACE>
SeqStatements()
<RBRACE>
}

 

int SeqStatements() :
{int i=0;
String RPrevProg="";
StringBuffer Raux1=new StringBuffer(), Raux2=new StringBuffer();
inden+=1;
}
{
( {
if( !isAtom ){
if(i==0)
RPrevProg = Rinst;
Rinst="";
}
}
i=RStatement(i, Raux1, Raux2)
{
if( !isAtom ){
i++;
if(i==1){
Raux1.replace(0, Raux1.length(), Rinst);
}else if(i==2){
Raux2.replace(0, Raux2.length(), "Jrer.Seq(" + Raux1.toString() + "," );
Raux1.replace(0, Raux1.length(), Rinst);
}else{
Raux2.append( "Jrer.Seq(" + inden(inden) + Raux1.toString() + "," );
Raux1.replace(0, Raux1.length(), Rinst);
}
Rinst="";
}
}
)* {
if( isAtom ){
isAtom=false;
if( i>0 )
Raux2.append( "Jrer.Seq(" + inden(inden) + Raux1.toString() + "," );
Raux1.replace(0, Raux1.length(), Atom(StrGlob1));
i++;
}
if(i==0)
Rinst = RPrevProg + "Jrer.Nothing()";
else if(i==1)
Rinst = RPrevProg + Raux1.toString();
else{
Rinst = RPrevProg + Raux2.toString() + inden(inden) + Raux1.toString();
for(int j=1; j<i; j++)
Rinst += ")";
}
inden-=1;
return i;
}
}

 

int RStatement(int i, StringBuffer Raux1, StringBuffer Raux2) :
{}
{
LOOKAHEAD( <STRING_LITERAL> ( <BANG> | <HOOK> ) | RType() <IDENTIFIER> | <IF> |
<GEN> | <WAIT> | <LOOP> | <FREEZ> | <REPEAT> | <MIGRATE> | <BEHAVIOR> | <PAR> <LBRACE> |
<RUN> | <CALL> | <LINK> | <LOCAL> | <CURVAL> | <PRNLATM> | <GENERATE> | <PAR> <LPAREN> |
<TRY> | <WHEN> | <CTRL> | <UNTIL> | <PREVAL> | <PRNATM> | <SEMICOLON> | <STOP> <SEMICOLON>
)
{ i=testAtom(i, Raux1, Raux2); }
RStatements() { return i; }
|
AtomStatement() { return i; }
}

 

void RStatements() :
{}
{
  LOOKAHEAD(2)
   Par()
 | DynaPar()
 | If()
 | Try()
 | Run()
 | Call()
 | Stop()
 | Wait()
 | Loop()
 | When()
 | Print()
 | Until()
 | Repeat()
 | Nothing()
 | Migrate()
 | Generate()
 | Behavior()
 | Variables()
 | ValueEvent()
 | SuffixEvent()
 | LocalLinkCtrlFreez()
}

 

// Re-used productions : RType, RVariableDeclaratorId().

void Variables() :
{ StrGlob1 =""; }
{
RLocalVariableDeclaration()
<SEMICOLON> {
ClaOfVar += ";\n";

if(asig)
Rinst += Atom( spaAtom + StrGlob1 );
else
Rinst += "Jrer.Nothing()";
}
}

 

void RLocalVariableDeclaration() :
{ asig=false; }
{
RType() RVariableDeclarator()
(
<COMMA> { ClaOfVar += ","; }
RVariableDeclarator()
)*
}

 

void RVariableDeclarator() :
{String aux=""; }
{
aux = RVariableDeclaratorId()

[ <ASSIGN> {
asig=true;
StrGlob1 += aux + "=";
Reac = 2;
}
VariableInitializer() { Reac = 1; StrGlob1 += ";\n"; }
]
}

//------------------------------------------------------------------------------
// REJO instructions
//------------------------------------------------------------------------------

void Nothing() :
{}
{
<SEMICOLON> { Rinst += "Jrer.Nothing()"; }
}

 

void Stop() :
{}
{
<STOP><SEMICOLON> { Rinst += "Jrer.Stop()"; }
}

 

void Print() :
{
Token t;
String aux="";
}
{
( t=<PRNATM> | t=<PRNLATM> ) <LPAREN>
aux = SubSetOfExpression()
<RPAREN>
<SEMICOLON> {
if(t.kind==PRNLATM)
Rinst += Atom( spaAtom + "getDataAg().out().write(" + aux + " +\"\\n\");\n" );
else
Rinst += Atom( spaAtom + "getDataAg().out().write( " + aux + " );\n" );
}
}

 

void Run() :
{ String aux1="", aux2="";}
{
<RUN>
MyExpression() {
aux1 = spaAtom + "o = "+ StrGlob1 +";\n";
Atom( aux1 );
Rinst += "Jrer.Run( new VarProgramWrapper(locVar, "+nAtoms+") )";
}
<SEMICOLON>
}

 

void Call() :
{String aux="";}
{
<CALL>
MyExpression()
<SEMICOLON> { Rinst += StrGlob1 + " /* Call */ "; }
}

 

void AtomStatement() :
{ }
{
LOOKAHEAD( <ATOM> <LBRACE> )
<ATOM> <LBRACE> {
if( !isAtom ){
isAtom=true;
StrGlob1 = "";
}
Reac = 2;
StrGlob1 += spaAtom;
JavaParserTokenManager.setJaPar(true);
}
( StatementExpression() <SEMICOLON> { StrGlob1 += ";"; }
| { StrGlob1 += spaAtom; } ifStatement()
| { StrGlob1 += spaAtom; } DoStatement()
| { StrGlob1 += spaAtom; } ForStatement()
| { StrGlob1 += spaAtom; } BreakStatement()
| { StrGlob1 += spaAtom; } WhileStatement()
| { StrGlob1 += spaAtom; } SwitchStatement()
| { StrGlob1 += spaAtom; } ContinueStatement()
)*
<RBRACE> {
Reac = 1;
JavaParserTokenManager.setJaPar(false);
}
|
{
if( !isAtom ){
isAtom=true;
StrGlob1 = "";
}
Reac = 2;
StrGlob1 += spaAtom;
}
StatementExpression() <SEMICOLON> {
StrGlob1 += ";\n";
Reac = 1;
}
}

 

void ValueEvent() :
{
String aux1="", aux2="";
Token t;
}
{
(t=<CURVAL> | t=<PREVAL>)
aux1=SubSetOfExpression()
<COMMA> aux2=SubSetOfExpression()
<SEMICOLON>
{
if( t.kind == CURVAL )
Rinst += Atom( spaAtom + aux2 +
"= env.currentValues(Jrer.TestType2("+ aux1 +", env));\n");
else
Rinst += Atom( spaAtom + aux2 +
"= env.previousValues(Jrer.TestType2("+ aux1 +", env));\n");
}
}

 

void Generate() :
{
int o=0;
boolean cte1=true;
String aux, strAtm, strAtm2="";
}
{
(<GEN> | <GENERATE> )
aux= SubSetOfExpression() {
strAtm = spaAtom + "o = Jrer.TestType("+ aux +");\n";
cte1=cte;
}
[ <COMMA>
MyExpression()
{
o=1;
strAtm2 = spaAtom + "o = " + StrGlob1 + ";\n";
}
]
<SEMICOLON> {
if(o==1){
if(!cte1){
Atom( strAtm );
Atom( strAtm2 );
Rinst += "Jrer.Generate(new VarIdentifierWrapper(locVar, "+ (nAtoms-1) +"), "+
"new VarObjectWrapper(locVar, "+ nAtoms +"))";
} else{
Atom( strAtm2 );
Rinst += "Jrer.Generate(Jrer.StringWrapper("+ aux + "), new VarObjectWrapper(locVar, "+ nAtoms +"))";
}
} else {
if(!cte1){
Atom( strAtm );
Rinst += "Jrer.Generate(new VarIdentifierWrapper(locVar, "+ nAtoms +"))";
} else
Rinst += "Jrer.Generate(Jrer.StringWrapper("+ aux +"))";
}
}
}

 

void Wait() :
{
boolean value=false, cte1;
String aux1="", aux2="", aux3="";
}
{
<WAIT>
aux1 = CondExp() { cte1=cte; aux3=StrGlob1; }
[ <COMMA> aux2 = SubSetOfExpression() {
if(cond==true)
throw new ParseException("Configurations in wait cannot be used with values " );
value=true;
}
]
<SEMICOLON> {
if( !value )
Rinst += "Jrer.Await("+ aux1 +")";
else {
//---------------------------------->
Rinst += "Jrer.Seq(Jrer.Await("+ aux1 + "),Jrer.Seq(";
inden+=1;
Rinst += inden(inden)+"Jrer.Stop(),";
if(cte1)
Rinst += inden(inden) + Atom( spaAtom + aux2 +"= env.previousValues(Jrer.StringIdentifier("+ aux3 +"));\n")+"))";
else
Rinst += inden(inden) + Atom( spaAtom + aux2 +"= env.previousValues( (Identifier)actions(env,"+ nAtoms +"));\n")+"))";
inden-=1;
//------------------------------------>
}
}
}

 

void SuffixEvent() :
{
Token t1, t2;
}
{
<STRING_LITERAL> { t1=getToken(0); }
( <BANG> | <HOOK> ) { t2=getToken(0);
if(t2.kind==BANG)
Rinst += "Jrer.Generate(Jrer.StringWrapper(" + t1.image +"))";
else
Rinst += "Jrer.Await(Jrer.Presence(Jrer.StringWrapper(" + t1.image +")))";
}
<SEMICOLON>
}

 

void When() :
{
String aux;
}
{
<WHEN> <LPAREN>
aux= CondExp()
<RPAREN> {
inden +=1;
Rinst += "Jrer.When("+ aux +" ," + inden(inden);
}
RStatementS()
[ LOOKAHEAD(1)
<ELSE> {
inden--;
Rinst += ","+ inden(inden) + "// Else";
inden++;
Rinst += inden(inden);
}
RStatementS() ] {
Rinst += ")";
inden -= 1;
}
}


void If() :

{ }
{
<IF> <LPAREN>
MyExpression() <RPAREN> {
if(cte){
inden+=1;
Rinst += "Jrer.If(new VarBooleanWrapper("+ StrGlob1 + ") , ";
Rinst += inden(inden);
}else{
StrGlob1 = spaAtom + "o = ("+ StrGlob1 +")? null: this;\n";
Atom( StrGlob1 );
Rinst += "Jrer.If(new VarBooleanWrapper(locVar, "+ nAtoms + ") , ";
inden+=1;
Rinst += inden(inden);
}
}
RStatementS()
[ LOOKAHEAD(1)
<ELSE> { inden--;
Rinst += ","+ inden(inden) +"// Else";
inden++;
Rinst += inden(inden);
}
RStatementS() ] { Rinst += ")"; inden-=1; }
}

 

void Try() :
{ }
{
<TRY> {
Rinst += "Jrer.Try(";
inden++;
Rinst += inden(inden);
}
RStatementS()
<CATCH> { inden--;
Rinst += ","+ inden(inden) +"// Catch";
inden++;
Rinst += inden(inden);
}
RStatementS() { Rinst += ")"; inden--; }
}

 

void Loop() :
{}
{
<LOOP> { inden+=1; Rinst += "Jrer.Loop(" + inden(inden); }
RStatementS() { Rinst += ")"; inden-=1; }
}

 

void DynaPar() :
{}
{
<PAR> <LPAREN> { Rinst += "Jrer.DynaPar("; }
Expression()
<RPAREN> { Rinst += ","; }
RStatementS() { Rinst += ")"; }
}


void Par() :

{
int i=1;
String RPrevProg="", Raux1="", Raux2="";
}
{
<PAR> <LBRACE> {
RPrevProg = Rinst;
Rinst="";
}
SeqStatements()
( {
i++;
if(i==2)
Raux1 = Rinst;
Rinst="";
}
<SC_OR>
SeqStatements() {
if(i==2){
Raux2 = "Jrer.Par(" + Raux1 + ",";
Raux1=Rinst;
}else{
inden--;
Raux2 += "Jrer.Par(" + inden(inden) + " // ||";
inden++;
Raux2 += inden(inden) + Raux1 + ",";
Raux1=Rinst;
}
}
)*
<RBRACE> {
if(i==1){
Rinst = RPrevProg + Rinst;
}else{
inden--;
Rinst = RPrevProg + Raux2 + inden(inden) + " // ||";
inden++;
Rinst += inden(inden) + Raux1;
for(int j=1; j<i; j++)
Rinst += ")";
inden-=1;
}
}
}


void LocalLinkCtrlFreez() :

{
Token t;
String aux="";
}
{
( t=<LOCAL> | t=<CTRL> | t=<FREEZ> ) // t=<LINK>
{
if(t.kind==LOCAL)
Rinst += "Jrer.Local(";
else if(t.kind==CTRL)
Rinst += "Jrer.Control(";
else if(t.kind==FREEZ)
Rinst += "Jrer.Freezable(";
// else if(t.kind==LINK)
// Rinst += "Jrer.Link(";
}
<LPAREN>
aux = SubSetOfExpression()
<RPAREN> {
if(!cte){
if(t.kind==LOCAL){
Rinst += "new StringIdentifier("+ aux +")";
}else{
ConfigVar = "o = Jrer.TestType("+ aux +");\n"; // before it was +=
Atom( spaAtom + ConfigVar );
Rinst += "new VarIdentifierWrapper(locVar, "+ nAtoms +")";
}
}else if(t.kind==LOCAL)
Rinst += "Jrer.StringIdentifier("+ aux + ")";
else
Rinst += "Jrer.StringWrapper("+ aux + ")";
inden+=1;
Rinst += "," + inden(inden);
}
RStatementS() { Rinst += ")"; inden-=1; }
}

RStatementS() { Rinst += ")"; } // { Rinst += "\n))))))"; }
}

 

void Repeat() :
{
String aux;
}
{
<REPEAT> <LPAREN>
MyExpression()
<RPAREN>
{
if(cte){
inden+=1;
Rinst += "Jrer.Repeat(new VarIntegerWrapper("+ StrGlob1 +"),";
Rinst += inden(inden);
}else{
aux = spaAtom + "getLong = "+ StrGlob1 +";\n";
aux += spaAtom + "o=this; \n";
Atom( aux );
inden+=1;
Rinst += "Jrer.Repeat(new VarIntegerWrapper(locVar, "+ nAtoms +"),";
Rinst += inden(inden);
}
}
RStatementS() {
Rinst += ")";
inden-=1;
}
}

 

void Until() :
{
String aux;
}
{
<UNTIL> <LPAREN>
aux = CondExp()
<RPAREN> {
inden += 1;
Rinst += "Jrer.Until("+ aux +","+ inden(inden);
}
RStatementS()
[ LOOKAHEAD(1)
<HAND> {
inden--;
Rinst += "," + inden(inden) + "// Handler";
inden++;
Rinst += inden(inden);
}
RStatementS() ] {
Rinst += ")";
inden -= 1;
}
}



/*-----------------------------------------------------------------------------------------
   
Reactive Expressions
This method return
a String with the Junior tranlation, for instance And(Presence(...), Or( ...))
a String in ConfigVar with the atomic instruction for handling the java vars.
-------------------------------------------------------------------------------------------*/


String CondExp() :

{ String c="";
  ConfigVar="";
  cond=false;
}
{
   c = AND_Exp() { return c;}
}

String AND_Exp() :
{
  int two=0;
  String c1="", c2="";
}
{
  c1=OR_Exp()
  [ <SC_AND> c2=AND_Exp() {two=1;} ]
  {
    if(two==1){
      cond=true;
      return "Jrer.And(" + c1 + "," + c2 + ")";
    }else
      return c1;
  }
}

String OR_Exp() :
{
  int two=0;
  String c1="",c2="";
}
{
  c1=Not_Exp()
  [ <SC_OR> c2=OR_Exp() {two=1;} ]
  {
    if(two==1){
      cond=true;
      return "Jrer.Or(" + c1 + "," + c2 + ")";
    }else
      return c1;
  }
}

 

String Not_Exp() :
{ String c=""; }
{
  LOOKAHEAD(<BANG>)
  <BANG> c=Parent_Exp() {
                          cond=true;
                          return "Jrer.Not( "+ c +")";
                        }
  |
  c=Parent_Exp()        { return c; }
}


String Parent_Exp() :
{ String c=""; }
{
  LOOKAHEAD(<LPAREN>)
  <LPAREN> c=AND_Exp() <RPAREN> { return c; }
  |
  c=SubSetOfExpression()        {
                                  if(!cte){
                                    ConfigVar = "o =  Jrer.TestType("+c+");\n";
                                    Atom( spaAtom + ConfigVar );
                                    return "Jrer.Presence(new VarIdentifierWrapper(locVar, "+ nAtoms +"))";
                                  }else
                                    return "Jrer.Presence(Jrer.StringWrapper("+ c + "))";
                                }
}

 

String SubSetOfExpression() :
{ StrGlob1=""; }
{
  {
    JavaParserTokenManager.setJaPar(true);
    Reac=2;
    cte=true;
  }
  AdditiveExpression()
  {
    Reac=1;
    JavaParserTokenManager.setJaPar(false);
    return StrGlob1;
  }
}

 

String MyExpression() :
{ StrGlob1=""; }
{
  {
    JavaParserTokenManager.setJaPar(true);
    Reac=2;
    cte=true;
  }
  Expression()
  {
    Reac=1;
    JavaParserTokenManager.setJaPar(false);
    return StrGlob1;
  }

}