options {
  STATIC = true;
  ERROR_REPORTING = true;
  IGNORE_CASE = true;
  SANITY_CHECK = true;
}


PARSER_BEGIN(FormC)


public class FormC {
  public static void emit(String s) {
    System.out.println(s);
  }

  public static void emit(Token t) {
    emit(t.image);
  }

  public static void main(String args[]) throws ParseException {
    FormC parser = new FormC(System.in);
    parser.Input();
  }
}

PARSER_END(FormC)

SKIP :
{
  " "
| "\t"
| "\r"
}

TOKEN :
{
   <BEGIN: "BEGIN_JAVA" > : JCODE
}

TOKEN :
{
   <FORM: "FORM" >
|  <SUBMIT: "SUBMIT" >
|  <FIELD: "FIELD">
|  <GROUP: "GROUP">
|  <ENDGROUP: "ENDGROUP">
|  <ID: <IDCHAR1>(<IDCHAR>)* >
|  <NUM: (["0" - "9"])+ >
|  <IDCHAR: (["A" - "Z", "0" - "9", "_"]) >
|  <IDCHAR1: (["A" - "Z", "_"])>
|  <EOL: "\n" >
|  <QSTRING: "\"" 
  (~["\"","\\","\n","\r"])*
   "\"">
}



<JCODE>
MORE :
{
  < ~[] >  
}

<JCODE>
TOKEN :
{
  <JCODEBLK: "//END_JAVA"   > : DEFAULT
}




void Input() :
{
}
{
  <FORM> formname() (<EOL>)*
  (element() <EOL>)+  { emit("</FORM> <% } else { %>"); }
  <BEGIN> <JCODEBLK> { 
     String codeblk=token.image;
     emit("<%");
     emit(codeblk); 
     emit("} %>");
     }
  (<EOL>)*
  <EOF>
}

void formname() :
{
  Token formn;
}
{
  formn=<ID>
  {
  emit("<% if (request.getContentLength()==-1) { %>");
  emit("<FORM ACTION=\"" + formn.image + ".jsp\" METHOD=POST>");
  }
}

void element() :
{ }
{
  fieldorsub() { emit("<BR>"); }
}


void fieldorsub() :
{ }
{
   group() 
|  field() 
|  submit()
}

void group() :
{ }
{
  <GROUP> <EOL>
  (fieldorsub()<EOL>)+
  <ENDGROUP> 
}

void field() :
{ 
  Token fid;
  Token prompt=null;
  Token len=null;
  Token siz=null;
}
{
  <FIELD> fid=<ID> [prompt=<QSTRING> [siz=<NUM> [len=<NUM>]]]
   {
    if (prompt!=null) emit(prompt.image.substring(1,prompt.image.length()-1)+": ");
    emit("<INPUT NAME=\"" + fid.image + "\"");
    if (siz!=null) emit("SIZE=" + siz.image);
    if (len!=null) emit("MAXLENGTH=" + len.image);
    emit(">");
   }
}

String qid() : 
{
  Token t;
}
{
   t=<QSTRING>  { return t.image; }
|  t=<ID>       { return t.image; }
}

void submit() :
{
  Token id;
  String qid=null;
}
{
  <SUBMIT> id=<ID> [qid=qid()]
   {
   emit("<INPUT TYPE=SUBMIT NAME=\"" + id.image + "\"");   
   if (qid!=null) 
     emit("VALUE=\"" + id.image + "\"");
   emit (">");
   }
}