Usenet.com

www.Usenet.com

Group Index

Comp Thread Archive from Usenet.com

<-- __Chronological__ --> <-- __Thread__ -->

Newbie question



this is a .jjt file I have so far. In the Line() and Rectangle()
methods, I have used variables x1, y1, x2 and y2. How can I specify
them so that I do not need to put the declarations in every method?

Also, instead of printing the lines in those methods to the system.out
stream, how can I send them to a text file?

Cheers,
Dave

***************************************

PARSER_BEGIN(eg1)

class eg1 {
  public static void main(String args[]) {

    System.out.println("Reading from standard input...");
    eg1 t = new eg1(System.in);
    try {
      SimpleNode n = t.Start();
      //n.dump("");
      System.out.println("Thank you.");
    } catch (Exception e) {
      System.out.println("Oops.");
      System.out.println(e.getMessage());
      e.printStackTrace();
    }
  }
}

PARSER_END(eg1)


SKIP :
{
  " "
| "\t"
| "\n"
| "\r"
| <"//" (~["\n","\r"])* ("\n"|"\r"|"\r\n")>
| <"/*" (~["*"])* "*" (~["/"] (~["*"])* "*")* "/">
}

TOKEN : /* IDENTIFIERS */
{
  < NUMBER: ["1"-"9"] (["0"-"9"])*>
|
  < LBRACE: "[" >
|
  < RBRACE: "]" >
|
  < CLBRACE: "{" >
|
  < CRBRACE: "}" >
}

SimpleNode Start() : {}
{
  (Shape())* ";"
  { return jjtThis; }
}

void Shape() : {}
{
  Line() | Rectangle()
}

void Line() :
{ 
  int x1;
  int y1;
  int x2;
  int y2;
}
{
  "line" 
  <LBRACE> 
  x1 = Number()
  y1 = Number() 
  x2 = Number() 
  y2 = Number() <RBRACE>
  {
    System.out.println("<line x1=\"" + x1 + "\" y1=\"" + y1 + "\"
x2=\"" + x2 + "\" y2=\"" + y2 + "\">" );
  }
}

void Rectangle() : 
{
  int x1;
  int y1;
  int x2;
  int y2;
}
{
  "rectangle" <LBRACE> 
  x1 = Number()
  y1 = Number() 
  x2 = Number() 
  y2 = Number() <RBRACE>
  {
    System.out.println("<rectangle x1= \"" + x1 + "\" y1=\"" + y1 +
"\" width= \"" + (x2-x1) + "\" height=\"" + (y2-y1) + "\">" );
  }
}

int Number() :
{
  Token t;
}
{
  t = <NUMBER>
  {return Integer.parseInt(t.image);}
}



<-- __Chronological__ --> <-- __Thread__ -->


Usenet.com



Please check out one of the premium Usenet Newsgroup Service Providers below for access to Usenet.