Saturday 4 August 2012

Natural/ADABAS to JAVA converter notes

Input file: IF #SEC=3
#BAS1:='STAND' #BAS2:='IRSALIYE' #ISTEK:='TESLIM'
MOVE 'S' TO #SWITCH MOVE'IST' TO #IST
MOVE (AD=P)TO #IST-CV #SWITCH-CV
ELSE IF #SEC=4 MOVE 'O' TO #SWITCH COMPUTE #ISTEK='TESLIM'
IF *GROUP NE 'TARSO' AND *GROUP NE 'GT01' AND *GROUP NE'GZTPLNGR'
AND *GROUP NE 'GZTANLGR'
MOVE #ISTASYON TO #IST MOVE(AD=P) TO #IST-CV
END-IF
END-IF
END-IF
.
Parser output :
Converter Output (in two parts):

Natural/ADABAS to JAVA converter

options {
IGNORE_CASE = true;
DEBUG_PARSER = false;
DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = false;
LOOKAHEAD = 2;
FORCE_LA_CHECK=true;

MULTI=true;
VISITOR=true;
NODE_DEFAULT_VOID=true;
}
PARSER_BEGIN(ARSconverter)

public class ARSconverter {
//Parser execution
public static void main ( String args [ ] ) {
//Parser initialization
ARSconverter parser;
WorkArea.localMemArea="lGztkonp0";
WorkArea.globalMemArea="gGztkonp0";
//System.out.println(WorkArea.i);

if(args.length == 0){
System.out.println ("\nARSconverter: GIRIS OKUNUYOR ...\n\n");
parser = new ARSconverter(System.in);
} else if(args.length == 1){
System.out.println ("ARSconverter: Reading the file " + args[0] + " ..." );
try {
parser = new ARSconverter(new java.io.FileInputStream(args[0]));
} catch(java.io.FileNotFoundException e) {
System.out.println ("ARSconverter: The file " + args[0] + " was not found.");
return;
}
} else {
System.out.println ("ARSconverter: You must use one of the following:");
System.out.println (" java ARSconverter < file");
System.out.println ("Or");
System.out.println (" java ARSconverter file");
return ;
}

try {
ASTStart n = parser.Start();
//n.dump("");
System.out.println ("\n\nARSconverter: PARSE ISLEMI BASARILDI");
System.out.println ("\n\nARSconverter: CONVERSION ISLEMI BASLADI");
ARSconverterVisitor v = new ARSconverterDumpVisitor();
n.jjtAccept(v, null);
System.out.println ("\n\nARSconverter: CONVERSION ISLEMI BASARILDI");
} catch(ParseException e){
System.out.println ("ARSconverter: There was an error during the parse.");
System.out.println (e.getMessage());
} catch(TokenMgrError e){
System.out.println ("ARSconverter: There was an error.");
System.out.println (e.getMessage());
}
}

public static String indentString(int l) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < l; ++i) {
sb.append(' ');
}
return sb.toString();
}
}
PARSER_END(ARSconverter)

//STRUCTURES AND CHARACTERS TO SCAPE

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

//STATIC TOKENS
TOKEN : {
<INTEGER_CONSTANT: (<DIGIT>)+>
| <LOGIC_CONSTANT: "true" | "false" | "-1">
| <#DIGIT: ["0"-"9"]>
}

//RESERVED WORDS
TOKEN : {
/* A */
<AND: "and">
| <ADD: "add">
| <ALARM: "alarm">
| <AT_END_OF_DATA: "at end of data">
| <AT_BREAK_OF: "at break of">
| <AT_TOP_OF_PAGE: "at top of page">
/* B */
| <BY: "by">
| <BOTTOM: "bottom">
/* C */
| <COMPUTE: "compute">
| <COMMENT: "CCMMNNTT">
| <CONTROL: "control">
/* D */
| <DEFINE: "define">
| <DATA: "data">
| <DISPLAY: "display">
/* E */
| <ELSE: "else">
| <END: "end">
| <END_BREAK: "end-break">
| <END_DEFINE: "end-define">
| <END_ENDDATA: "end-enddata">
| <END_FIND: "end-find">
| <END_FOR: "end-for">
| <END_IF: "end-if">
| <END_READ: "end-read">
| <END_ALL: "end-all">
| <END_NOREC: "end-norec">
| <END_REPEAT: "end-repeat">
| <END_SUBROUTINE: "end-subroutine">
| <END_TOPPAGE: "end-toppage">
| <ESCAPE: "escape">
/* F */
| <FIND: "find">
| <FROM: "from">
| <FOR: "for">
| <FORMAT: "format">
/* G */
| <GET: "get">
/* I */
| <IF: "if">
| <IN: "in">
| <INPUT : "input">
/* K */
| <KEY: "key">
/* L */
| <LIMIT: "limit">
| <LOGICAL: "logical">
| <LOCAL: "local">
/* M */
| <MOVE: "move">
| <MAP: "map">
| <MARK: "mark">
/* N */
| <NAMED: "named">
| <NOTITLE: "notitle">
| <NOHDR: "nohdr">
| <NOT: "not">
| <NO_REC_FOUND: "no record found">
| <NO_ERASE: "no erase">
/* O */
| <OR: "or">
/* P */
| <PERFORM: "perform">
/* R */
| <READ: "read">
| <RECORD: "record">
| <REPEAT: "repeat">
| <RESET: "reset">
| <REINPUT: "reinput">
| <ROUTINE: "routine">
/* S */
| <SET: "set">
| <SIZE: "size">
| <SORTED: "sorted">
| <STORE: "store">
| <SUBTRACT: "subtract">
| <SUBROUTINE: "subroutine">
/* T */
| <TO: "to">
| <TOP: "top">
| <THRU: "thru">
/* U */
| <USING: "using">
| <UPDATE: "update">
/* W */
| <WINDOW: "window">
| <WITH: "with">
| <WHERE: "where">
| <WRITE: "write">
}
TOKEN :
{
<SG : ("SG=" ("OFF" | "ON" )+)>
| <LS : ("LS=" (<INTEGER_CONSTANT>)+)>
| <PS : ("PS=" (<INTEGER_CONSTANT>)+)>

| <AD : ("AD=" (("M'_'") | ("MI'_'") | "P" | "I")) >
| <ASSIGNCHARS : <COLONCHAR><EQUALCHAR>>
| <IP : ("IP=" ("OFF" | "ON" )+)>
| <MASK : ( "MASK" <LPARENCHAR><NATURAL_WORD><RPARENCHAR>)>
}
TOKEN :
{
// < LEVEL_66: "66" >
// < LEVEL_NUMBER: ( (("0")? ["1"-"9"]) | (["1"-"4"]["0"-"9"]) | "78" ) >
// < INTEGER: (["0"-"9"])+ >
< MINUSCHAR: "-" > // a.k.a. dash
| < LPARENCHAR: "(" >
| < RPARENCHAR: ")" >
| < COLONCHAR: ":" >
| < DOTCHAR: "." >
| < COMMACHAR: "," >
| < DOUBLEDQUOTECHAR: "\"\"" >
| < QUOTECHAR: "\"" >
| < DOUBLEDAPOSTROPHE: "''" >
| < APOSTROPHE: "'" >
| < PLUSCHAR: "+" >
| < ASTERISKCHAR: "*" >
//| < POWEROF: "**" >
| < SLASHCHAR: "/" >
| < DOLLARCHAR: "$" >
| < LESSTHANOREQUAL: "<=" >
| < LESSTHANCHAR: "<" >
| < MORETHANOREQUAL: ">=" >
| < MORETHANCHAR: ">" >
| < EQUALCHAR: "=" >
| < NOTEQUAL: "<>" >
| < WNOTEQUAL: "NE" >
| < SHARPCHAR: "#" >

//| < COMPARISON_OP: (["=","<",">"])+>
| < HEXNUMBER: ["h","x"] ( ( <QUOTECHAR> ( ["0"-"9","a"-"f"] )+ <QUOTECHAR> )
| ( <APOSTROPHE> ( ["0"-"9","a"-"f"] )+ <APOSTROPHE> )
)
>
| < QUOTEDSTRING: ( <QUOTECHAR> (~["\""] | <DOUBLEDQUOTECHAR> )* <QUOTECHAR>
| <APOSTROPHE> (~["'"] | <DOUBLEDAPOSTROPHE> )* <APOSTROPHE>
)
>
| <REINPUT_ASTERISK_PARM: <ASTERISKCHAR> ((["0"-"9"])(["0"-"9"])(["0"-"9"])) >
// | <REINPUT_MARK_PARM: (<SHARPCHAR>| <PLUSCHAR>)? <NATURAL_WORD> >
| < NATURAL_WORD: (["a"-"z","0"-"9"])+ ( (<MINUSCHAR> | "_" )+ (["a"-"z","0"-"9"])+ )* >
| < OTHER_CHARS: ~[] >
}
TOKEN: {
<COMMENT_LINE: (<COMMENT> <ASTERISKCHAR> (<GENERAL>)* "\r\n")>
| <#GENERAL: (["a"-"z","A"-"Z", "0"-"9", "(", ")", "[", "]", ":", "\'", "*", "|", ";",
"%", "-", "/", "."," ", "=", "#", "@", "+", "_", "<", ">", "^", "$",
"?", "&", ",", "ı", "İ", "ş", "Ş", "ü", "Ü", "ç", "Ç", "ğ", "Ğ", "ö", "Ö"])>
}

void CodeLine() :
{//System.out.println("\nCodeLine");
Token t;}
{
(<COMMENT_LINE> | NaturalStatementLine())
}
void NaturalStatementLine() :
{}
{
NaturalStatement()
}
void NaturalStatement() :
{}
{
MoveStatement()
| IfStatement()
| ComputeStatement()
| ComputationStatement()
| DotStatament()
}
Token compare() :
{Token t=null;}
{
(
t=<EQUALCHAR>
| t=<LESSTHANCHAR>
| t=<LESSTHANOREQUAL>
| t=<MORETHANCHAR>
| t=<MORETHANOREQUAL>
| t=<NOTEQUAL>
| t=<WNOTEQUAL>
)
{return t;}
}

Token mem_type() :
{Token t=null;}
{
(t=<SHARPCHAR> | t=<PLUSCHAR> | t=<ASTERISKCHAR>)
{return t;}
}

ASTDot DotStatament() #Dot :
{System.out.println();System.out.println("Dot statement");}
{
<DOTCHAR> ("\r\n") {System.out.println("DOTCHAR");}
{ return jjtThis; }
}
ASTMove MoveStatement() #Move:
{System.out.println();System.out.println("Move statement");
Token mv_int_const=null, mv_nat_w1_1=null, mv_dot=null;
Token mv_mem_type=null, mv_nat1_2=null;
Token mv_quo_str=null, mv_lp=null, mv_ad=null, mv_rp=null;
Token mv_to=null;
Token[] mv_to_mem_type2_1=new Token[10], mv_to_nat_w2_1=new Token[10];
Token[] mv_to_lp=new Token[10], mv_to_asterisk=new Token[10];
Token[] mv_to_int_const=new Token[10], mv_to_mem_type2_2=new Token[10];
Token[] mv_to_nat_w2_2=new Token[10], mv_to_rp=new Token[10];
int i=0;
}
{
<MOVE> {System.out.print("MOVE ");}
(
mv_int_const=<INTEGER_CONSTANT> {System.out.print(mv_int_const.image);}
|(mv_nat_w1_1=<NATURAL_WORD> {System.out.print(mv_nat_w1_1.image);} mv_dot=<DOTCHAR> {System.out.print(mv_dot.image);})?
((( mv_mem_type=mem_type() {System.out.print(mv_mem_type.image);})?) mv_nat1_2=<NATURAL_WORD>{System.out.print(mv_nat1_2.image);})
| mv_quo_str=<QUOTEDSTRING> {System.out.print(mv_quo_str.image);}
| mv_lp=<LPARENCHAR> {System.out.print(mv_lp.image);} mv_ad=<AD> {System.out.print(mv_ad.image);} mv_rp=<RPARENCHAR> {System.out.print(mv_rp.image);}
)
mv_to=<TO> {System.out.print(" "+mv_to.image +" ");}
(
(mv_to_mem_type2_1[i]=mem_type(){System.out.print(mv_to_mem_type2_1[i].image);})?
mv_to_nat_w2_1[i]=<NATURAL_WORD> {System.out.print(mv_to_nat_w2_1[i].image);}

(
mv_to_lp[i]=<LPARENCHAR> {System.out.print(mv_to_lp[i].image);}
(
mv_to_asterisk[i]=<ASTERISKCHAR> {System.out.print(mv_to_asterisk[i].image);}
| mv_to_int_const[i]=<INTEGER_CONSTANT> {System.out.print(mv_to_int_const[i].image);}
| (mv_to_mem_type2_2[i]=mem_type() {System.out.print(mv_to_mem_type2_2[i].image);})?
mv_to_nat_w2_2[i]=<NATURAL_WORD> {System.out.print(mv_to_nat_w2_2[i].image);}
)
mv_to_rp[i]=<RPARENCHAR>{System.out.print(mv_to_rp[i].image);}
)? {i++;}
)+
("\r\n")? {System.out.println();}
{
jjtThis.setMv_int_const(mv_int_const);
jjtThis.setMv_nat_w1_1(mv_nat_w1_1);
jjtThis.setMv_dot(mv_dot);
jjtThis.setMv_mem_type(mv_mem_type);
jjtThis.setMv_nat1_2(mv_nat1_2);
jjtThis.setMv_quo_str(mv_quo_str);
jjtThis.setMv_lp(mv_lp);
jjtThis.setMv_ad(mv_ad);
jjtThis.setMv_rp(mv_rp);
jjtThis.setMv_to(mv_to);
jjtThis.setMv_to_mem_type2_1(mv_to_mem_type2_1);
jjtThis.setMv_to_nat_w2_1(mv_to_nat_w2_1);
jjtThis.setMv_to_lp(mv_to_lp);
jjtThis.setMv_to_asterisk(mv_to_asterisk);
jjtThis.setMv_to_int_const(mv_to_int_const);
jjtThis.setMv_to_mem_type2_2(mv_to_mem_type2_2);
jjtThis.setMv_to_nat_w2_2(mv_to_nat_w2_2);
jjtThis.setMv_to_rp(mv_to_rp);
jjtThis.setMv_i(i);
}
{ return jjtThis; }
}

ASTCompute ComputeStatement() #Compute:
{System.out.println();System.out.println("Compute statement");
Token compute_mem_type1=null, compute_nat_w1=null;
Token compute_equal=null, compute_equal_int_const=null, compute_equal_mem_type=null;
Token compute_equal_nat_w=null, compute_equal_quo_str=null;
Token[] compute_loop_op=new Token[10], compute_loop_int_const=new Token[10];
Token[] compute_loop_mem_type=new Token[10], compute_loop_nat_w=new Token[10];
int i=0;
}
{ <COMPUTE> {System.out.print("COMPUTE ");}
( (compute_mem_type1=mem_type() {System.out.print(compute_mem_type1.image);})?
compute_nat_w1=<NATURAL_WORD>{System.out.print(compute_nat_w1.image);}
)
<EQUALCHAR> {System.out.print(" = ");}
( compute_equal_int_const=<INTEGER_CONSTANT> {System.out.print(compute_equal_int_const.image);}
| (compute_equal_mem_type=mem_type(){System.out.print(compute_equal_mem_type.image);})?
compute_equal_nat_w=<NATURAL_WORD> {System.out.print(compute_equal_nat_w.image);}
| compute_equal_quo_str=<QUOTEDSTRING>{System.out.print(compute_equal_quo_str.image);})
(
(compute_loop_op[i]=<PLUSCHAR>
| compute_loop_op[i]=<MINUSCHAR>
| compute_loop_op[i]=<ASTERISKCHAR>
| compute_loop_op[i]=<SLASHCHAR>) {System.out.print(compute_loop_op[i].image);}
( compute_loop_int_const[i]=<INTEGER_CONSTANT> {System.out.print(compute_loop_int_const[i].image);}
| (compute_loop_mem_type[i]=mem_type(){System.out.print(compute_loop_mem_type[i].image);})?
compute_loop_nat_w[i]=<NATURAL_WORD> {System.out.print(compute_loop_nat_w[i].image);}
) {i++;}
)* ("\r\n")?
{System.out.println();}
{ jjtThis.setCompute_mem_type1(compute_mem_type1);
jjtThis.setCompute_nat_w1(compute_nat_w1);
jjtThis.setCompute_equal(compute_equal);
jjtThis.setCompute_equal_int_const(compute_equal_int_const);
jjtThis.setCompute_equal_mem_type(compute_equal_mem_type);
jjtThis.setCompute_equal_nat_w(compute_equal_nat_w);
jjtThis.setCompute_equal_quo_str(compute_equal_quo_str);
jjtThis.setCompute_loop_op(compute_loop_op);
jjtThis.setCompute_loop_int_const(compute_loop_int_const);
jjtThis.setCompute_loop_mem_type(compute_loop_mem_type);
jjtThis.setCompute_loop_nat_w(compute_loop_nat_w);
jjtThis.setCompute_loop_i(i);
}
{ return jjtThis; }
}
ASTComputation ComputationStatement() #Computation:
{System.out.println();System.out.println("Computation statement");
Token comp_mem_type=null,comp_nat_w=null, comp_assign=null;
Token comp_assign_int_const=null, comp_assign_quo_str=null, comp_assign_mem_type=null, comp_assign_nat_w=null;}
{
(comp_mem_type=mem_type(){System.out.print(comp_mem_type.image);})?
comp_nat_w=<NATURAL_WORD> {System.out.print(comp_nat_w.image);}
comp_assign=<ASSIGNCHARS> {System.out.print(comp_assign.image);}
(
comp_assign_int_const=<INTEGER_CONSTANT> {System.out.print(comp_assign_int_const.image);}
| comp_assign_quo_str=<QUOTEDSTRING>{System.out.print(comp_assign_quo_str.image);}
| (
(comp_assign_mem_type=mem_type(){System.out.print(comp_assign_mem_type.image);})?
comp_assign_nat_w=<NATURAL_WORD>{System.out.print(comp_assign_nat_w.image);})
)
("\r\n")? {System.out.println();}
{
jjtThis.setComp_mem_type(comp_mem_type);
jjtThis.setComp_nat_w(comp_nat_w);
jjtThis.setComp_assign(comp_assign);
jjtThis.setComp_assign_int_const(comp_assign_int_const);
jjtThis.setComp_assign_quo_str(comp_assign_quo_str);
jjtThis.setComp_assign_mem_type(comp_assign_mem_type);
jjtThis.setComp_assign_nat_w(comp_assign_nat_w);
}
{ return jjtThis; }
}

ASTIf IfStatement() #If:
{System.out.println();System.out.println("If statement");
Token if_not=null, if_norec_fnd=null;
Token if_cnd_parm1_nat_w1=null, if_cnd_parm1_dot=null, if_cnd_parm1_mem_type=null, if_cnd_parm1_nat_w2=null;
Token if_cnd_parm1_lp=null;
Token if_cnd_parm1_paren_int_const=null,if_cnd_parm1_paren_quo_str=null, if_cnd_parm1_paren_mem_type=null, if_cnd_parm1_paren_nat_w=null;
Token if_cnd_parm1_rp=null;
Token if_cnd_cmpr=null;
Token[] if_cnd_parm2_paren_int_const=new Token[10],if_cnd_parm2_paren_quo_str=new Token[10], if_cnd_parm2_paren_mem_type=new Token[10], if_cnd_parm2_paren_nat_w=new Token[10];
Token if_cnd_parm2_paren_mask=null;
Token if_thru=null;
Token[] if_thru_int_const=new Token[10],if_thru_quo_str=new Token[10], if_thru_mem_type=new Token[10], if_thru_nat_w=new Token[10];

Token[] if_and=new Token[10], if_or=new Token[10];
Token[] if_and_mem_type=new Token[10], if_and_nat_w=new Token[10];
Token[] if_and_lp=new Token[10];
Token[] if_and_int_const1=new Token[10], if_and_quo_str1=new Token[10], if_and_mem_type1=new Token[10], if_and_paren_nat_w1=new Token[10];
Token[] if_and_rp=new Token[10];
Token[] if_and_cmpr=new Token[10];
Token[] if_and_int_const2=new Token[10],if_and_quo_str2=new Token[10], if_and_mem_type2=new Token[10], if_and_paren_nat_w2=new Token[10];
Token if_else=null;
Token if_endif=null;
int i=0,j=0,k=0;
}
{
<IF> {System.out.print("IF ");}("\r\n")? (if_not=<NOT> {System.out.print(if_not.image+" ");})? ("\r\n")?
(
(
if_norec_fnd=<NO_REC_FOUND>{System.out.print(if_norec_fnd.image+" ");}("\r\n")?
)
|
(
(
(
(if_cnd_parm1_nat_w1=<NATURAL_WORD>{System.out.print(if_cnd_parm1_nat_w1.image+" ");}
if_cnd_parm1_dot=<DOTCHAR>{System.out.print(if_cnd_parm1_dot.image+" ");})?
(if_cnd_parm1_mem_type=mem_type(){System.out.print(if_cnd_parm1_mem_type.image+" ");})?
if_cnd_parm1_nat_w2=<NATURAL_WORD>{System.out.print(if_cnd_parm1_nat_w2.image+" ");}
) ("\r\n")?
(
if_cnd_parm1_lp=<LPARENCHAR>{System.out.print(if_cnd_parm1_lp.image+" ");}
(
if_cnd_parm1_paren_int_const=<INTEGER_CONSTANT> {System.out.print(if_cnd_parm1_paren_int_const.image+" ");}
| if_cnd_parm1_paren_quo_str=<QUOTEDSTRING>{System.out.print(if_cnd_parm1_paren_quo_str.image+" ");}
| (if_cnd_parm1_paren_mem_type=mem_type(){System.out.print(if_cnd_parm1_paren_mem_type.image+" ");})?
if_cnd_parm1_paren_nat_w=<NATURAL_WORD>{System.out.print(if_cnd_parm1_paren_nat_w.image+" ");}
)
if_cnd_parm1_rp=<RPARENCHAR>{System.out.print(if_cnd_parm1_rp.image+" ");}
)? ("\r\n")?
(if_cnd_cmpr=compare() {System.out.print(if_cnd_cmpr.image+" ");})("\r\n")?
(
if_cnd_parm2_paren_mask=<MASK> {System.out.print(if_cnd_parm2_paren_mask.image+" ");}("\r\n")?
|
(
(if_cnd_parm2_paren_int_const[j]=<INTEGER_CONSTANT> {System.out.print(if_cnd_parm2_paren_int_const[j].image+" ");}
| if_cnd_parm2_paren_quo_str[j]=<QUOTEDSTRING>{System.out.print(if_cnd_parm2_paren_quo_str[j].image+" ");}
| (if_cnd_parm2_paren_mem_type[j]=mem_type(){System.out.print(if_cnd_parm2_paren_mem_type[j].image+" ");})?
if_cnd_parm2_paren_nat_w[j]=<NATURAL_WORD>{System.out.print(if_cnd_parm2_paren_nat_w[j].image+" ");})
{j++;}
)+ ("\r\n")?

)
)
(
if_thru=<THRU> {System.out.print(if_thru.image+" ");}
(
(if_thru_int_const[k]=<INTEGER_CONSTANT> {System.out.print(if_thru_int_const[k].image+" ");}
| if_thru_quo_str[k]=<QUOTEDSTRING> {System.out.print(if_thru_quo_str[k].image+" ");}
| (if_thru_mem_type[k]=mem_type() {System.out.print(if_thru_mem_type[k].image+" ");})?
if_thru_nat_w[k]=<NATURAL_WORD> {System.out.print(if_thru_nat_w[k].image+" ");})
{k++;}
)+
)? ("\r\n")?
( ("\r\n")?
(if_and[i]=<AND>{System.out.print(if_and[i].image+" ");} | if_or[i]=<OR>{System.out.print(if_or[i].image+" ");}) ("\r\n")?
((if_and_mem_type[i]=mem_type(){System.out.print(if_and_mem_type[i].image+" ");})?
if_and_nat_w[i]=<NATURAL_WORD>{System.out.print(if_and_nat_w[i].image+" ");})? ("\r\n")?
(if_and_lp[i]=<LPARENCHAR>{System.out.print(if_and_lp[i].image+" ");}
(
if_and_int_const1[i]=<INTEGER_CONSTANT> {System.out.print(if_and_int_const1[i].image+" ");}
| if_and_quo_str1[i]=<QUOTEDSTRING>{System.out.print(if_and_quo_str1[i].image+" ");}
| (if_and_mem_type1[i]=mem_type(){System.out.print(if_and_mem_type1[i].image+" ");})?
if_and_paren_nat_w1[i]=<NATURAL_WORD>{System.out.print(if_and_paren_nat_w1[i].image+" ");}
)
if_and_rp[i]=<RPARENCHAR>{System.out.print(if_and_rp[i].image+" ");}
)? ("\r\n")?
if_and_cmpr[i]=compare() {System.out.print(if_and_cmpr[i].image+" ");}("\r\n")?
(
if_and_int_const2[i]=<INTEGER_CONSTANT>{System.out.print(if_and_int_const2[i].image+" ");}
| if_and_quo_str2[i]=<QUOTEDSTRING> {System.out.print(if_and_quo_str2[i].image+" ");}
| (if_and_mem_type2[i]=mem_type(){System.out.print(if_and_mem_type2[i].image+" ");})?
if_and_paren_nat_w2[i]=<NATURAL_WORD>{System.out.print(if_and_paren_nat_w2[i].image+" ");}
)?
{i++;}
)*
) ("\r\n")?
)
{
jjtThis.setIf_not(if_not);
jjtThis.setIf_norec_fnd(if_norec_fnd);
jjtThis.setIf_cnd_parm1_nat_w1(if_cnd_parm1_nat_w1);
jjtThis.setIf_cnd_parm1_dot(if_cnd_parm1_dot);
jjtThis.setIf_cnd_parm1_mem_type(if_cnd_parm1_mem_type);
jjtThis.setIf_cnd_parm1_nat_w2(if_cnd_parm1_nat_w2);
jjtThis.setIf_cnd_parm1_lp(if_cnd_parm1_lp);
jjtThis.setIf_cnd_parm1_paren_int_const(if_cnd_parm1_paren_int_const);
jjtThis.setIf_cnd_parm1_paren_quo_str(if_cnd_parm1_paren_quo_str);
jjtThis.setIf_cnd_parm1_paren_mem_type(if_cnd_parm1_paren_mem_type);
jjtThis.setIf_cnd_parm1_paren_nat_w(if_cnd_parm1_paren_nat_w);
jjtThis.setIf_cnd_parm1_rp(if_cnd_parm1_rp);
jjtThis.setIf_cnd_cmpr(if_cnd_cmpr);
jjtThis.setIf_cnd_parm2_paren_int_const(if_cnd_parm2_paren_int_const);
jjtThis.setIf_cnd_parm2_paren_quo_str(if_cnd_parm2_paren_quo_str);
jjtThis.setIf_cnd_parm2_paren_mem_type(if_cnd_parm2_paren_mem_type);
jjtThis.setIf_cnd_parm2_paren_nat_w(if_cnd_parm2_paren_nat_w);
jjtThis.setIf_cnd_parm2_paren_mask(if_cnd_parm2_paren_mask);
jjtThis.setIf_thru(if_thru);
jjtThis.setIf_thru_int_const(if_thru_int_const);
jjtThis.setIf_thru_quo_str(if_thru_quo_str);
jjtThis.setIf_thru_mem_type(if_thru_mem_type);
jjtThis.setIf_thru_nat_w(if_thru_nat_w);
jjtThis.setIf_and(if_and);
jjtThis.setIf_or(if_or);
jjtThis.setIf_and_mem_type(if_and_mem_type);
jjtThis.setIf_and_nat_w(if_and_nat_w);
jjtThis.setIf_and_lp(if_and_lp);
jjtThis.setIf_and_int_const1(if_and_int_const1);
jjtThis.setIf_and_quo_str1(if_and_quo_str1);
jjtThis.setIf_and_mem_type1(if_and_mem_type1);
jjtThis.setIf_and_paren_nat_w1(if_and_paren_nat_w1);
jjtThis.setIf_and_rp(if_and_rp);
jjtThis.setIf_and_cmpr(if_and_cmpr);
jjtThis.setIf_and_int_const2(if_and_int_const2);
jjtThis.setIf_and_quo_str2(if_and_quo_str2);
jjtThis.setIf_and_mem_type2(if_and_mem_type2);
jjtThis.setIf_and_paren_nat_w2(if_and_paren_nat_w2);
jjtThis.setIf_i(i);
jjtThis.setIf_j(j);
jjtThis.setIf_k(k);
}
(NaturalStatement())*
(
if_else=<ELSE>{System.out.print(if_else.image+" ");}{jjtThis.setIf_else(if_else);} ("\r\n")?
(NaturalStatement())+
)*
EndIfStatement()
{System.out.println();}
{ return jjtThis; }
}
ASTEndIf EndIfStatement() #EndIf :
{System.out.println();System.out.println("End If statement");}
{
(<END_IF>{System.out.print("END-IF ");}) ("\r\n")?
{ return jjtThis; }
}
//MAIN UNIT
ASTStart Start () #Start:
{
System.out.println("Start");
}
{
(
CodeLine()
)*
<EOF>
{ return jjtThis; }
}



Natural Detailed Parser notes

Input file follows: IF #SEC=3
#BAS1:='STAND' #BAS2:='IRSALIYE' #ISTEK:='TESLIM'
MOVE 'S' TO #SWITCH MOVE'IST' TO #IST
MOVE (AD=P)TO #IST-CV #SWITCH-CV
ELSE IF #SEC=4 MOVE 'O' TO #SWITCH COMPUTE #ISTEK='TESLIM'
IF *GROUP NE 'TARSO' AND *GROUP NE 'GT01' AND *GROUP NE'GZTPLNGR'
AND *GROUP NE 'GZTANLGR'
MOVE #ISTASYON TO #IST MOVE(AD=P) TO #IST-CV
END-IF
END-IF
END-IF
.
Output file follows:

Natural Detailed Parser

options {
IGNORE_CASE = true;
DEBUG_PARSER = false;
DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = false;
LOOKAHEAD = 2;
FORCE_LA_CHECK=true;
}
PARSER_BEGIN(VBTparser)

public class VBTparser {
//Parser execution
public static void main ( String args [ ] ) {
//Parser initialization
VBTparser parser;

if(args.length == 0){
System.out.println ("\nVBTparser: GIRIS OKUNUYOR ...\n\n");
parser = new VBTparser(System.in);
} else if(args.length == 1){
System.out.println ("VBTparser: Reading the file " + args[0] + " ..." );
try {
parser = new VBTparser(new java.io.FileInputStream(args[0]));
} catch(java.io.FileNotFoundException e) {
System.out.println ("VBTparser: The file " + args[0] + " was not found.");
return;
}
} else {
System.out.println ("VBTparser: You must use one of the following:");
System.out.println (" java VBTparser < file");
System.out.println ("Or");
System.out.println (" java VBTparser file");
return ;
}

try {
SimpleNode n = parser.Start();
//n.dump("");
System.out.println ("\n\nVBTparser: PARSE ISLEMI BASARILDI");
} catch(ParseException e){
System.out.println ("VBTparser: There was an error during the parse.");
System.out.println (e.getMessage());
} catch(TokenMgrError e){
System.out.println ("VBTparser: There was an error.");
System.out.println (e.getMessage());
}
}

public static String indentString(int l) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < l; ++i) {
sb.append(' ');
}
return sb.toString();
}
}
PARSER_END(VBTparser)

//STRUCTURES AND CHARACTERS TO SCAPE

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

//STATIC TOKENS
TOKEN : {
<INTEGER_CONSTANT: (<DIGIT>)+>
| <LOGIC_CONSTANT: "true" | "false" | "-1">
| <#DIGIT: ["0"-"9"]>
}

//RESERVED WORDS
TOKEN : {
/* A */
<AND: "and">
| <ADD: "add">
| <ALARM: "alarm">
| <AT_END_OF_DATA: "at end of data">
| <AT_BREAK_OF: "at break of">
| <AT_TOP_OF_PAGE: "at top of page">
/* B */
| <BY: "by">
| <BOTTOM: "bottom">
/* C */
| <COMPUTE: "compute">
| <COMMENT: "CCMMNNTT">
| <CONTROL: "control">
/* D */
| <DEFINE: "define">
| <DATA: "data">
| <DISPLAY: "display">
/* E */
| <ELSE: "else">
| <END: "end">
| <END_BREAK: "end-break">
| <END_DEFINE: "end-define">
| <END_ENDDATA: "end-enddata">
| <END_FIND: "end-find">
| <END_FOR: "end-for">
| <END_IF: "end-if">
| <END_READ: "end-read">
| <END_ALL: "end-all">
| <END_NOREC: "end-norec">
| <END_REPEAT: "end-repeat">
| <END_TOPPAGE: "end-toppage">
| <ESCAPE: "escape">
/* F */
| <FIND: "find">
| <FROM: "from">
| <FOR: "for">
| <FORMAT: "format">
/* G */
| <GET: "get">
/* I */
| <IF: "if">
| <IN: "in">
| <INPUT : "input">
/* K */
| <KEY: "key">
/* L */
| <LIMIT: "limit">
| <LOGICAL: "logical">
| <LOCAL: "local">
/* M */
| <MOVE: "move">
| <MAP: "map">
| <MARK: "mark">
/* N */
| <NAMED: "named">
| <NOTITLE: "notitle">
| <NOHDR: "nohdr">
| <NOT: "not">
| <NO_REC_FOUND: "no record found">
| <NO_ERASE: "no erase">
/* O */
| <OR: "or">
/* P */
| <PERFORM: "perform">
/* R */
| <READ: "read">
| <RECORD: "record">
| <REPEAT: "repeat">
| <RESET: "reset">
| <REINPUT: "reinput">
| <ROUTINE: "routine">
/* S */
| <SET: "set">
| <SIZE: "size">
| <SORTED: "sorted">
| <STORE: "store">
| <SUBTRACT: "subtract">
| <SUBROUTINE: "subroutine">
/* T */
| <TO: "to">
| <TOP: "top">
| <THRU: "thru">
/* U */
| <USING: "using">
| <UPDATE: "update">
/* W */
| <WINDOW: "window">
| <WITH: "with">
| <WHERE: "where">
| <WRITE: "write">
}
TOKEN :
{
<SG : ("SG=" ("OFF" | "ON" )+)>
| <LS : ("LS=" (<INTEGER_CONSTANT>)+)>
| <PS : ("PS=" (<INTEGER_CONSTANT>)+)>

| <AD : ("AD=" (("M'_'") | ("MI'_'") | "P" | "I")) >
| <ASSIGNCHARS : <COLONCHAR><EQUALCHAR>>
| <IP : ("IP=" ("OFF" | "ON" )+)>
| <MASK : ( "MASK" <LPARENCHAR><NATURAL_WORD><RPARENCHAR>)>
}
TOKEN :
{
// < LEVEL_66: "66" >
// < LEVEL_NUMBER: ( (("0")? ["1"-"9"]) | (["1"-"4"]["0"-"9"]) | "78" ) >
// < INTEGER: (["0"-"9"])+ >
< MINUSCHAR: "-" > // a.k.a. dash
| < LPARENCHAR: "(" >
| < RPARENCHAR: ")" >
| < COLONCHAR: ":" >
| < DOTCHAR: "." >
| < COMMACHAR: "," >
| < DOUBLEDQUOTECHAR: "\"\"" >
| < QUOTECHAR: "\"" >
| < DOUBLEDAPOSTROPHE: "''" >
| < APOSTROPHE: "'" >
| < PLUSCHAR: "+" >
| < ASTERISKCHAR: "*" >
//| < POWEROF: "**" >
| < SLASHCHAR: "/" >
| < DOLLARCHAR: "$" >
| < LESSTHANOREQUAL: "<=" >
| < LESSTHANCHAR: "<" >
| < MORETHANOREQUAL: ">=" >
| < MORETHANCHAR: ">" >
| < EQUALCHAR: "=" >
| < NOTEQUAL: "<>" >
| < WNOTEQUAL: "NE" >
| < SHARPCHAR: "#" >

//| < COMPARISON_OP: (["=","<",">"])+>
| < HEXNUMBER: ["h","x"] ( ( <QUOTECHAR> ( ["0"-"9","a"-"f"] )+ <QUOTECHAR> )
| ( <APOSTROPHE> ( ["0"-"9","a"-"f"] )+ <APOSTROPHE> )
)
>
| < QUOTEDSTRING: ( <QUOTECHAR> (~["\""] | <DOUBLEDQUOTECHAR> )* <QUOTECHAR>
| <APOSTROPHE> (~["'"] | <DOUBLEDAPOSTROPHE> )* <APOSTROPHE>
)
>
| <REINPUT_ASTERISK_PARM: <ASTERISKCHAR> ((["0"-"9"])(["0"-"9"])(["0"-"9"])) >
// | <REINPUT_MARK_PARM: (<SHARPCHAR>| <PLUSCHAR>)? <NATURAL_WORD> >
| < NATURAL_WORD: (["a"-"z","0"-"9"])+ ( (<MINUSCHAR> | "_" )+ (["a"-"z","0"-"9"])+ )* >
| < OTHER_CHARS: ~[] >
}
TOKEN: {
<COMMENT_LINE: (<COMMENT> <ASTERISKCHAR> (<GENERAL>)* "\r\n")>
| <#GENERAL: (["a"-"z","A"-"Z", "0"-"9", "(", ")", "[", "]", ":", "\'", "*", "|", ";",
"%", "-", "/", "."," ", "=", "#", "@", "+", "_", "<", ">", "^", "$",
"?", "&", ",", "ı", "İ", "ş", "Ş", "ü", "Ü", "ç", "Ç", "ğ", "Ğ", "ö", "Ö"])>
}

void CodeLine() :
{//System.out.println("\nCodeLine");
Token t;}
{
(<COMMENT_LINE> | NaturalStatementLine())
}
void NaturalStatementLine() :
{}
{
NaturalStatement()
}
void NaturalStatement() :
{}
{
MoveStatement()
| IfStatement()
| ComputeStatement()
| ComputationStatement()
| DotStatament()

}
Token compare() :
{Token t=null;}
{
(
t=<EQUALCHAR>
| t=<LESSTHANCHAR>
| t=<LESSTHANOREQUAL>
| t=<MORETHANCHAR>
| t=<MORETHANOREQUAL>
| t=<NOTEQUAL>
| t=<WNOTEQUAL>
)
{return t;}
}

Token mem_type() :
{Token t=null;}
{
(t=<SHARPCHAR> | t=<PLUSCHAR> | t=<ASTERISKCHAR>)
{return t;}
}

void DotStatament() :
{System.out.println();System.out.println("Dot statement");}
{
<DOTCHAR> ("\r\n") {System.out.println("DOTCHAR");}
}
void MoveStatement() :
{System.out.println();System.out.println("Move statement");
Token mv_int_const=null, mv_nat_w1_1=null, mv_dot=null;
Token mv_mem_type=null, mv_nat1_2=null;
Token mv_quo_str=null, mv_lp=null, mv_ad=null, mv_rp=null;
Token mv_to=null;
Token[] mv_to_mem_type2_1=new Token[10], mv_to_nat_w2_1=new Token[10];
Token[] mv_to_lp=new Token[10], mv_to_asterisk=new Token[10];
Token[] mv_to_int_const=new Token[10], mv_to_mem_type2_2=new Token[10];
Token[] mv_to_nat_w2_2=new Token[10], mv_to_rp=new Token[10];
int i=0;
}
{
<MOVE> {System.out.print("MOVE ");}
(
mv_int_const=<INTEGER_CONSTANT> {System.out.print(mv_int_const.image);}
|(mv_nat_w1_1=<NATURAL_WORD> {System.out.print(mv_nat_w1_1.image);} mv_dot=<DOTCHAR> {System.out.print(mv_dot.image);})?
((( mv_mem_type=mem_type() {System.out.print(mv_mem_type.image);})?) mv_nat1_2=<NATURAL_WORD>{System.out.print(mv_nat1_2.image);})
| mv_quo_str=<QUOTEDSTRING> {System.out.print(mv_quo_str.image);}
| mv_lp=<LPARENCHAR> {System.out.print(mv_lp.image);} mv_ad=<AD> {System.out.print(mv_ad.image);} mv_rp=<RPARENCHAR> {System.out.print(mv_rp.image);}
)
mv_to=<TO> {System.out.print(" "+mv_to.image +" ");}
(
(mv_to_mem_type2_1[i]=mem_type(){System.out.print(mv_to_mem_type2_1[i].image);})?
mv_to_nat_w2_1[i]=<NATURAL_WORD> {System.out.print(mv_to_nat_w2_1[i].image);}

(
mv_to_lp[i]=<LPARENCHAR> {System.out.print(mv_to_lp[i].image);}
(
mv_to_asterisk[i]=<ASTERISKCHAR> {System.out.print(mv_to_asterisk[i].image);}
| mv_to_int_const[i]=<INTEGER_CONSTANT> {System.out.print(mv_to_int_const[i].image);}
| (mv_to_mem_type2_2[i]=mem_type() {System.out.print(mv_to_mem_type2_2[i].image);})?
mv_to_nat_w2_2[i]=<NATURAL_WORD> {System.out.print(mv_to_nat_w2_2[i].image);}
)
mv_to_rp[i]=<RPARENCHAR>{System.out.print(mv_to_rp[i].image);}
)? {i++;}
)+
("\r\n")? {System.out.println();}
}

void ComputeStatement() :
{System.out.println();System.out.println("Compute statement");
Token compute_mem_type1=null, compute_nat_w1=null;
Token compute_equal=null, compute_equal_int_const=null, compute_equal_mem_type=null;
Token compute_equal_nat_w=null, compute_equal_quo_str=null;
Token[] compute_loop_op=new Token[10], compute_loop_int_const=new Token[10];
Token[] compute_loop_mem_type=new Token[10], compute_loop_nat_w=new Token[10];
int i=0;
}
{ <COMPUTE> {System.out.print("COMPUTE ");}
( (compute_mem_type1=mem_type() {System.out.print(compute_mem_type1.image);})?
compute_nat_w1=<NATURAL_WORD>{System.out.print(compute_nat_w1.image);}
)
<EQUALCHAR> {System.out.print(" = ");}
( compute_equal_int_const=<INTEGER_CONSTANT> {System.out.print(compute_equal_int_const.image);}
| (compute_equal_mem_type=mem_type(){System.out.print(compute_equal_mem_type.image);})?
compute_equal_nat_w=<NATURAL_WORD> {System.out.print(compute_equal_nat_w.image);}
| compute_equal_quo_str=<QUOTEDSTRING>{System.out.print(compute_equal_quo_str.image);})
(
(compute_loop_op[i]=<PLUSCHAR> | compute_loop_op[i]=<MINUSCHAR>
| compute_loop_op[i]=<ASTERISKCHAR> | compute_loop_op[i]=<SLASHCHAR>) {System.out.print(compute_loop_op[i].image);}
( compute_loop_int_const[i]=<INTEGER_CONSTANT> {System.out.print(compute_loop_int_const[i].image);}
| (compute_loop_mem_type[i]=mem_type(){System.out.print(compute_loop_mem_type[i].image);})?
compute_loop_nat_w[i]=<NATURAL_WORD> {System.out.print(compute_loop_nat_w[i].image);}
) {i++;}
)* ("\r\n")?
{System.out.println();}
}
void ComputationStatement() :
{System.out.println();System.out.println("Computation statement");
Token comp_mem_type=null,comp_nat_w=null, comp_assign=null;
Token comp_assign_int_const=null, comp_assign_quo_str=null, comp_assign_mem_type=null, comp_assign_nat_w=null;}
{
(comp_mem_type=mem_type(){System.out.print(comp_mem_type.image);})? comp_nat_w=<NATURAL_WORD> {System.out.print(comp_nat_w.image);}
comp_assign=<ASSIGNCHARS> {System.out.print(comp_assign.image);}
(
comp_assign_int_const=<INTEGER_CONSTANT> {System.out.print(comp_assign_int_const.image);}
| comp_assign_quo_str=<QUOTEDSTRING>{System.out.print(comp_assign_quo_str.image);}
| (
(comp_assign_mem_type=mem_type(){System.out.print(comp_assign_mem_type.image);})?
comp_assign_nat_w=<NATURAL_WORD>{System.out.print(comp_assign_nat_w.image);})
)
("\r\n")? {System.out.println();}
}


void IfStatement() :
{System.out.println();System.out.println("If statement");
Token if_not=null, if_norec_fnd=null;
Token if_cnd_parm1_nat_w1=null, if_cnd_parm1_dot=null, if_cnd_parm1_mem_type=null, if_cnd_parm1_nat_w2=null;
Token if_cnd_parm1_lp=null;
Token if_cnd_parm1_paren_int_const=null,if_cnd_parm1_paren_quo_str=null, if_cnd_parm1_paren_mem_type=null, if_cnd_parm1_paren_nat_w=null;
Token if_cnd_parm1_rp=null;
Token if_cnd_cmpr=null;
Token if_cnd_parm2_paren_int_const=null,if_cnd_parm2_paren_quo_str=null, if_cnd_parm2_mem_type=null, if_cnd_parm2_paren_nat_w=null;
Token if_cnd_parm2_paren_mask=null;
Token if_thru=null;
Token if_thru_int_const=null,if_thru_quo_str=null, if_thru_mem_type=null, if_thru_nat_w=null;

Token[] if_and=new Token[10], if_or=new Token[10];
Token[] if_and_mem_type=new Token[10], if_and_nat_w=new Token[10];
Token[] if_and_lp=new Token[10];
Token[] if_and_int_const1=new Token[10], if_and_quo_str1=new Token[10], if_and_mem_type1=new Token[10], if_and_paren_nat_w1=new Token[10];
Token[] if_and_rp=new Token[10];
Token[] if_and_cmpr=new Token[10];
Token[] if_and_int_const2=new Token[10],if_and_quo_str2=new Token[10], if_and_mem_type2=new Token[10], if_and_paren_nat_w2=new Token[10];
Token if_else=null;
Token if_endif=null;
int i=0;
}
{
<IF> {System.out.print("IF ");}("\r\n")? (if_not=<NOT> {System.out.print(if_not.image+" ");})? ("\r\n")?
(
(
if_norec_fnd=<NO_REC_FOUND>{System.out.print(if_norec_fnd.image+" ");}("\r\n")?
)
|
(
(
(
(if_cnd_parm1_nat_w1=<NATURAL_WORD>{System.out.print(if_cnd_parm1_nat_w1.image+" ");}
if_cnd_parm1_dot=<DOTCHAR>{System.out.print(if_cnd_parm1_dot.image+" ");})?
(if_cnd_parm1_mem_type=mem_type(){System.out.print(if_cnd_parm1_mem_type.image+" ");})?
if_cnd_parm1_nat_w2=<NATURAL_WORD>{System.out.print(if_cnd_parm1_nat_w2.image+" ");}
) ("\r\n")?
(
if_cnd_parm1_lp=<LPARENCHAR>{System.out.print(if_cnd_parm1_lp.image+" ");}
(
if_cnd_parm1_paren_int_const=<INTEGER_CONSTANT> {System.out.print(if_cnd_parm1_paren_int_const.image+" ");}
| if_cnd_parm1_paren_quo_str=<QUOTEDSTRING>{System.out.print(if_cnd_parm1_paren_quo_str.image+" ");}
| (if_cnd_parm1_paren_mem_type=mem_type(){System.out.print(if_cnd_parm1_paren_mem_type.image+" ");})?
if_cnd_parm1_paren_nat_w=<NATURAL_WORD>{System.out.print(if_cnd_parm1_paren_nat_w.image+" ");}
)
if_cnd_parm1_rp=<RPARENCHAR>{System.out.print(if_cnd_parm1_rp.image+" ");}
)? ("\r\n")?
if_cnd_cmpr=compare() {System.out.print(if_cnd_cmpr.image+" ");}("\r\n")?
(
(
if_cnd_parm2_paren_int_const=<INTEGER_CONSTANT> {System.out.print(if_cnd_parm2_paren_int_const.image+" ");}
| if_cnd_parm2_paren_quo_str=<QUOTEDSTRING>{System.out.print(if_cnd_parm2_paren_quo_str.image+" ");}
| (if_cnd_parm2_mem_type=mem_type(){System.out.print(if_cnd_parm2_mem_type.image+" ");})?
if_cnd_parm2_paren_nat_w=<NATURAL_WORD>{System.out.print(if_cnd_parm2_paren_nat_w.image+" ");}
)+ ("\r\n")?
| if_cnd_parm2_paren_mask=<MASK> {System.out.print(if_cnd_parm2_paren_mask.image+" ");}("\r\n")?
)
)
(
if_thru=<THRU> {System.out.print(if_thru.image+" ");}
(
if_thru_int_const=<INTEGER_CONSTANT> {System.out.print(if_thru_int_const.image+" ");}
| if_thru_quo_str=<QUOTEDSTRING> {System.out.print(if_thru_quo_str.image+" ");}
| (if_thru_mem_type=mem_type() {System.out.print(if_thru_mem_type.image+" ");})?
if_thru_nat_w=<NATURAL_WORD> {System.out.print(if_thru_nat_w.image+" ");}
)+
)? ("\r\n")?
( ("\r\n")?
(if_and[i]=<AND>{System.out.print(if_and[i].image+" ");} | if_or[i]=<OR>{System.out.print(if_or[i].image+" ");}) ("\r\n")?
((if_and_mem_type[i]=mem_type(){System.out.print(if_and_mem_type[i].image+" ");})?
if_and_nat_w[i]=<NATURAL_WORD>{System.out.print(if_and_nat_w[i].image+" ");})? ("\r\n")?
(if_and_lp[i]=<LPARENCHAR>{System.out.print(if_and_lp[i].image+" ");}
(
if_and_int_const1[i]=<INTEGER_CONSTANT> {System.out.print(if_and_int_const1[i].image+" ");}
| if_and_quo_str1[i]=<QUOTEDSTRING>{System.out.print(if_and_quo_str1[i].image+" ");}
| (if_and_mem_type1[i]=mem_type(){System.out.print(if_and_mem_type1[i].image+" ");})?
if_and_paren_nat_w1[i]=<NATURAL_WORD>{System.out.print(if_and_paren_nat_w1[i].image+" ");}
)
if_and_rp[i]=<RPARENCHAR>{System.out.print(if_and_rp[i].image+" ");}
)? ("\r\n")?
if_and_cmpr[i]=compare() {System.out.print(if_and_cmpr[i].image+" ");}("\r\n")?
(
if_and_int_const2[i]=<INTEGER_CONSTANT>{System.out.print(if_and_int_const2[i].image+" ");}
| if_and_quo_str2[i]=<QUOTEDSTRING> {System.out.print(if_and_quo_str2[i].image+" ");}
| (if_and_mem_type2[i]=mem_type(){System.out.print(if_and_mem_type2[i].image+" ");})?
if_and_paren_nat_w2[i]=<NATURAL_WORD>{System.out.print(if_and_paren_nat_w2[i].image+" ");}
)?
{i++;}
)*
) ("\r\n")?
)
(NaturalStatement())*
(
if_else=<ELSE>{System.out.print(if_else.image+" ");} ("\r\n")?
(NaturalStatement())+
)*
(if_endif=<END_IF>{System.out.print(if_endif.image+" ");}) ("\r\n")?
{System.out.println();}
}
//MAIN UNIT
SimpleNode Start () :
{
System.out.println("Start");
}
{
(
CodeLine()
)*
<EOF>
{ return jjtThis; }
}



Friday 3 August 2012

Natural/ADABAS to JAVA parser example notes

The input file follows:

IF #SEC=3
#BAS1:='STAND' #BAS2:='IRSALIYE' #ISTEK:='TESLIM'
MOVE 'S' TO #SWITCH MOVE'IST' TO #IST
MOVE (AD=P)TO #IST-CV #SWITCH-CV
ELSE IF #SEC=4 MOVE 'O' TO #SWITCH COMPUTE #ISTEK='TESLIM'

IF *GROUP NE 'TARSO' AND *GROUP NE 'GT01' AND *GROUP NE'GZTPLNGR'
AND *GROUP NE 'GZTANLGR'
MOVE #ISTASYON TO #IST MOVE(AD=P) TO #IST-CV
END-IF
END-IF
END-IF
.
The output file follows:

Natural/ADABAS parser example

options {
IGNORE_CASE = true;
DEBUG_PARSER = false;
DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = false;
LOOKAHEAD = 2;
FORCE_LA_CHECK=true;
}
PARSER_BEGIN(ARSparser)

public class ARSparser {
//Parser execution
public static void main ( String args [ ] ) {
//Parser initialization
ARSparser parser;

if(args.length == 0){
System.out.println ("\nARSparser: GIRIS OKUNUYOR ...\n\n");
parser = new ARSparser(System.in);
} else if(args.length == 1){
System.out.println ("ARSparser: Reading the file " + args[0] + " ..." );
try {
parser = new ARSparser(new java.io.FileInputStream(args[0]));
} catch(java.io.FileNotFoundException e) {
System.out.println ("ARSparser: The file " + args[0] + " was not found.");
return;
}
} else {
System.out.println ("ARSparser: You must use one of the following:");
System.out.println (" java ARSparser < file");
System.out.println ("Or");
System.out.println (" java ARSparser file");
return ;
}

try {
SimpleNode n = parser.Start();
//n.dump("");
System.out.println ("\n\nARSparser: PARSE ISLEMI BASARILDI");
} catch(ParseException e){
System.out.println ("ARSparser: There was an error during the parse.");
System.out.println (e.getMessage());
} catch(TokenMgrError e){
System.out.println ("ARSparser: There was an error.");
System.out.println (e.getMessage());
}
}

public static String indentString(int l) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < l; ++i) {
sb.append(' ');
}
return sb.toString();
}
}
PARSER_END(ARSparser)

//STRUCTURES AND CHARACTERS TO SCAPE

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

//STATIC TOKENS
TOKEN : {
<INTEGER_CONSTANT: (<DIGIT>)+>
| <LOGIC_CONSTANT: "true" | "false" | "-1">
| <#DIGIT: ["0"-"9"]>
}

//RESERVED WORDS
TOKEN : {
/* A */
<AND: "and">
| <ADD: "add">
| <ALARM: "alarm">
| <AT_END_OF_DATA: "at end of data">
| <AT_BREAK_OF: "at break of">
| <AT_TOP_OF_PAGE: "at top of page">
/* B */
| <BY: "by">
| <BOTTOM: "bottom">
/* C */
| <COMPUTE: "compute">
| <COMMENT: "CCMMNNTT">
| <CONTROL: "control">
/* D */
| <DEFINE: "define">
| <DATA: "data">
| <DISPLAY: "display">
/* E */
| <ELSE: "else">
| <END: "end">
| <END_BREAK: "end-break">
| <END_DEFINE: "end-define">
| <END_ENDDATA: "end-enddata">
| <END_FIND: "end-find">
| <END_FOR: "end-for">
| <END_IF: "end-if">
| <END_READ: "end-read">
| <END_ALL: "end-all">
| <END_NOREC: "end-norec">
| <END_REPEAT: "end-repeat">
| <END_TOPPAGE: "end-toppage">
| <ESCAPE: "escape">
/* F */
| <FIND: "find">
| <FROM: "from">
| <FOR: "for">
| <FORMAT: "format">
/* G */
| <GET: "get">
/* I */
| <IF: "if">
| <IN: "in">
| <INPUT : "input">
/* K */
| <KEY: "key">
/* L */
| <LIMIT: "limit">
| <LOGICAL: "logical">
| <LOCAL: "local">
/* M */
| <MOVE: "move">
| <MAP: "map">
| <MARK: "mark">
/* N */
| <NAMED: "named">
| <NOTITLE: "notitle">
| <NOHDR: "nohdr">
| <NOT: "not">
| <NO_REC_FOUND: "no record found">
| <NO_ERASE: "no erase">
/* O */
| <OR: "or">
/* P */
| <PERFORM: "perform">
/* R */
| <READ: "read">
| <RECORD: "record">
| <REPEAT: "repeat">
| <RESET: "reset">
| <REINPUT: "reinput">
| <ROUTINE: "routine">
/* S */
| <SET: "set">
| <SIZE: "size">
| <SORTED: "sorted">
| <STORE: "store">
| <SUBTRACT: "subtract">
| <SUBROUTINE: "subroutine">
/* T */
| <TO: "to">
| <TOP: "top">
| <THRU: "thru">
/* U */
| <USING: "using">
| <UPDATE: "update">
/* W */
| <WINDOW: "window">
| <WITH: "with">
| <WHERE: "where">
| <WRITE: "write">
}
TOKEN :
{
<SG : ("SG=" ("OFF" | "ON" )+)>
| <LS : ("LS=" (<INTEGER_CONSTANT>)+)>
| <PS : ("PS=" (<INTEGER_CONSTANT>)+)>

| <AD : ("AD=" (("M'_'") | ("MI'_'") | "P" | "I")) >
| <ASSIGNCHARS : <COLONCHAR><EQUALCHAR>>
| <IP : ("IP=" ("OFF" | "ON" )+)>
| <MASK : ( "MASK" <LPARENCHAR><NATURAL_WORD><RPARENCHAR>)>
}
TOKEN :
{
// < LEVEL_66: "66" >
// < LEVEL_NUMBER: ( (("0")? ["1"-"9"]) | (["1"-"4"]["0"-"9"]) | "78" ) >
// < INTEGER: (["0"-"9"])+ >
< MINUSCHAR: "-" > // a.k.a. dash
| < LPARENCHAR: "(" >
| < RPARENCHAR: ")" >
| < COLONCHAR: ":" >
| < DOTCHAR: "." >
| < COMMACHAR: "," >
| < DOUBLEDQUOTECHAR: "\"\"" >
| < QUOTECHAR: "\"" >
| < DOUBLEDAPOSTROPHE: "''" >
| < APOSTROPHE: "'" >
| < PLUSCHAR: "+" >
| < ASTERISKCHAR: "*" >
//| < POWEROF: "**" >
| < SLASHCHAR: "/" >
| < DOLLARCHAR: "$" >
| < LESSTHANOREQUAL: "<=" >
| < LESSTHANCHAR: "<" >
| < MORETHANOREQUAL: ">=" >
| < MORETHANCHAR: ">" >
| < EQUALCHAR: "=" >
| < NOTEQUAL: "<>" >
| < WNOTEQUAL: "NE" >
| < SHARPCHAR: "#" >
//| < COMPARISON_OP: (["=","<",">"])+>
| < HEXNUMBER: ["h","x"] ( ( <QUOTECHAR> ( ["0"-"9","a"-"f"] )+ <QUOTECHAR> )
| ( <APOSTROPHE> ( ["0"-"9","a"-"f"] )+ <APOSTROPHE> )
)
>
| < QUOTEDSTRING: ( <QUOTECHAR> (~["\""] | <DOUBLEDQUOTECHAR> )* <QUOTECHAR>
| <APOSTROPHE> (~["'"] | <DOUBLEDAPOSTROPHE> )* <APOSTROPHE>
)
>
| <REINPUT_ASTERISK_PARM: <ASTERISKCHAR> ((["0"-"9"])(["0"-"9"])(["0"-"9"])) >
// | <REINPUT_MARK_PARM: (<SHARPCHAR>| <PLUSCHAR>)? <NATURAL_WORD> >
| < NATURAL_WORD: (["a"-"z","0"-"9"])+ ( (<MINUSCHAR> | "_" )+ (["a"-"z","0"-"9"])+ )* >
| < OTHER_CHARS: ~[] >
}
TOKEN: {
<COMMENT_LINE: (<COMMENT> <ASTERISKCHAR> (<GENERAL>)* "\r\n")>
| <#GENERAL: (["a"-"z","A"-"Z", "0"-"9", "(", ")", "[", "]", ":", "\'", "*", "|", ";",
"%", "-", "/", "."," ", "=", "#", "@", "+", "_", "<", ">", "^", "$",
"?", "&", ",", "ı", "İ", "ş", "Ş", "ü", "Ü", "ç", "Ç", "ğ", "Ğ", "ö", "Ö"])>
}

void CodeLine() :
{//System.out.println("\nCodeLine");
Token t;}
{
(<COMMENT_LINE> | NaturalStatementLine())
}
void NaturalStatementLine() :
{}
{
NaturalStatement()
}
void NaturalStatement() :
{}
{
ComputationStatement()
| ComputeStatement()
| MoveStatement()
| IfStatement()
| DotStatament()
}
void compare() :
{}
{ <EQUALCHAR>
| <LESSTHANCHAR>
| <LESSTHANOREQUAL>
| <MORETHANCHAR>
| <MORETHANOREQUAL>
| <NOTEQUAL>
| <WNOTEQUAL>}

void DotStatament() :
{System.out.println("Dot statement");}
{
<DOTCHAR> ("\r\n")
}
void MoveStatement() :
{System.out.println("Move statement");}
{
<MOVE> (<INTEGER_CONSTANT>
| (<NATURAL_WORD><DOTCHAR>)? ((<SHARPCHAR>| <PLUSCHAR> | <ASTERISKCHAR>)?)<NATURAL_WORD>
| <QUOTEDSTRING>
| <LPARENCHAR> <AD> <RPARENCHAR>)
<TO> (
(<SHARPCHAR>| <PLUSCHAR>)? <NATURAL_WORD>
(<LPARENCHAR>
(<ASTERISKCHAR>| <INTEGER_CONSTANT> | (<SHARPCHAR>| <PLUSCHAR>)? <NATURAL_WORD>)
<RPARENCHAR>)?
)+
("\r\n")?
}

void ComputeStatement() :
{System.out.println("Compute statement");}
{ <COMPUTE>
((<SHARPCHAR>| <PLUSCHAR>)? <NATURAL_WORD>)
<EQUALCHAR>
( <INTEGER_CONSTANT> | (<PLUSCHAR> |<SHARPCHAR>)? <NATURAL_WORD> | <QUOTEDSTRING>)
(
(<PLUSCHAR> | <MINUSCHAR> | <ASTERISKCHAR> | <SLASHCHAR>)
( <INTEGER_CONSTANT> | (<SHARPCHAR>)? <NATURAL_WORD>)
)* ("\r\n")?
}
void ComputationStatement() :
{System.out.println("Compute statement");}
{
(<SHARPCHAR>| <PLUSCHAR> | <ASTERISKCHAR>)? <NATURAL_WORD>
<ASSIGNCHARS>
(<INTEGER_CONSTANT> | <QUOTEDSTRING> | ((<SHARPCHAR>| <PLUSCHAR> | <ASTERISKCHAR>)? <NATURAL_WORD>))
("\r\n")?
}
void IfStatement() :
{System.out.println("If statement");}
{
<IF> ("\r\n")? (<NOT>)* ("\r\n")?
(
(
<NO_REC_FOUND>("\r\n")?
)
|
(
(
((<NATURAL_WORD><DOTCHAR>)?(<SHARPCHAR>| <PLUSCHAR>| <ASTERISKCHAR>)? <NATURAL_WORD>) ("\r\n")?
(<LPARENCHAR>
(<INTEGER_CONSTANT> | <QUOTEDSTRING>| (<SHARPCHAR>| <PLUSCHAR>| <ASTERISKCHAR>)? <NATURAL_WORD>)
<RPARENCHAR>
)? ("\r\n")?
compare() ("\r\n")?
(
( <INTEGER_CONSTANT> | <QUOTEDSTRING>| (<SHARPCHAR>| <PLUSCHAR>| <ASTERISKCHAR>)? <NATURAL_WORD>)+ ("\r\n")?
| <MASK> ("\r\n")?
)
)
(<THRU>
( <INTEGER_CONSTANT> | <QUOTEDSTRING>| (<SHARPCHAR>| <PLUSCHAR>| <ASTERISKCHAR>)? <NATURAL_WORD>)+)? ("\r\n")?
( ("\r\n")?
(<AND> | <OR>) ("\r\n")?
((<SHARPCHAR>| <PLUSCHAR> | <ASTERISKCHAR>)? <NATURAL_WORD>)? ("\r\n")?
(<LPARENCHAR>
(<INTEGER_CONSTANT> | <QUOTEDSTRING>| (<SHARPCHAR>| <PLUSCHAR>| <ASTERISKCHAR>)? <NATURAL_WORD>)
<RPARENCHAR>
)? ("\r\n")?
compare() ("\r\n")?
( <INTEGER_CONSTANT> | <QUOTEDSTRING> | (<SHARPCHAR>| <PLUSCHAR>| <ASTERISKCHAR>)? <NATURAL_WORD>)?
)*
) ("\r\n")?
)
(NaturalStatement())*
(<ELSE> ("\r\n")?
(NaturalStatement())+)*
(<END_IF> | <END_NOREC>) ("\r\n")?

}
//MAIN UNIT
SimpleNode Start () :
{
System.out.println("Start");
}
{
(
CodeLine()
)*
<EOF>
{ return jjtThis; }
}



Monday 23 July 2012

Simple Parser for Natural-ADABAS - 2

This example prints header info and prints detail lines for definitions. If requested it is possible to select code lines by simply commenting definition choice and uncommenting code choice.

The jj file follows:
options {
IGNORE_CASE = true;
DEBUG_PARSER = false;
 DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = false;
LOOKAHEAD = 2;
FORCE_LA_CHECK=true;
 }

PARSER_BEGIN(ProcessFile)

/** Simple Natural parser. */
public class ProcessFile {

/** Main entry point. */
public static void main(String args[]) throws ParseException {

ProcessFile parser = new ProcessFile(System.in); parser.Input();
}

}

PARSER_END(ProcessFile)

TOKEN: {
<CR_LF: "\r\n">
| <HEADER: ("*H**") (<GENERAL>)* <CR_LF>>
| <TITLE: ("*C**") (<GENERAL>)* <CR_LF>>
| <DETAIL: ("*D") (<GENERAL>)* <CR_LF>>
| <CODE_LINE: ("*S**" <LINE_NUM> (<GENERAL>)* <CR_LF>)>
| <#LINE_NUM: (["0"-"9"])(["0"-"9"])(["0"-"9"])(["0"-"9"]) >
| <DEFINE_LINE: ("*S****" ("DF" | "DRR" | "DFR" | "C" | "DV" | "DD" | "V" "DFR" | "DRR" | "DSR" |"HS" | "I") (<GENERAL>)* <CR_LF>)>
| <END_STATEMENT: "*E" <CR_LF>>
| <#GENERAL: (["a"-"z","A"-"Z", "0"-"9", "(", ")", "[", "]", ":", "\'", "*", "|", ";", "%", " ", "-", "/", ".", "=", "#", "@", "+", "_", "<", ">", "^", "$", "?", "&", ",", "ı", "İ", "ş", "Ş", "ü", "Ü", "ç", "Ç", "ğ", "Ğ", "ö", "Ö"])>
}

TOKEN : {
< NATURAL_WORD: (["a"-"z","0"-"9"])+ ( ("-" | "_" )+ (["a"-"z","0"-"9"])+ )* >
| < OTHER : ~[] >
}

/** Root production. */
void Input() : {
System.out.println("Start"); Token th,tc,td,tt,tf;
}
{
( (th=<HEADER> {System.out.print("header=" + th.image);} )

 | (tt=<TITLE> {System.out.print("title=" + tt.image);} )

 //| ( {System.out.print("\ntc=");} "*C**" (tc=<OTHER>{System.out.print(tc.image);})*
<CR_LF> )

 | (td=<DETAIL> {System.out.print("detail=" + td.image);} )

| tc=<CODE_LINE>      //{System.out.print("code=" + tc.image);}

 | tf=<DEFINE_LINE> {System.out.print(tf.image);} )* <END_STATEMENT> <EOF>
}

Parts from the output follows:


Simple Parser for Natural-ADABAS - 1

This is a very simple parser that parses Natural-ADABAS programs. The NATURAL source program is parsed at HEADER, TITLE, DETAIL, CODE_LINE, DEFINE_LINE level.

The first example will only check the type of the lines and verify whether it is one of these types. The JavaCC jj file follows.

options {
IGNORE_CASE = true;
DEBUG_PARSER = false;
DEBUG_LOOKAHEAD = false;
DEBUG_TOKEN_MANAGER = false;
LOOKAHEAD = 1;
FORCE_LA_CHECK=true;
}

PARSER_BEGIN(ProcessFile)
 /** Simple Java Parser. */
public class ProcessFile

{ /** Main entry point. */
public static void main(String args[]) throws ParseException {

ProcessFile parser = new ProcessFile(System.in); parser.Input();
}
}
 PARSER_END(ProcessFile)

TOKEN: {
<CR_LF: "\r\n">
 | <HEADER: ("*H**") (<GENERAL>)* <CR_LF>>
 | <TITLE: ("*C**") (<GENERAL>)* <CR_LF>>
| <DETAIL: ("*D") (<GENERAL>)* <CR_LF>>
 | <CODE_LINE: ("*S**" <LINE_NUM> (<GENERAL>)* <CR_LF>)>
| <#LINE_NUM: (["0"-"9"])(["0"-"9"])(["0"-"9"])(["0"-"9"]) >
| <DEFINE_LINE: ("*S****" ("DF" | "DRR" | "DFR" | "C" | "DV" | "DD" | "V" "DFR" | "DRR" | "DSR" |"HS" | "I") (<GENERAL>)* <CR_LF>)>
| <END_STATEMENT: "*E" <CR_LF>>
| <#GENERAL: (["a"-"z","A"-"Z", "0"-"9", "(", ")", "[", "]", ":", "\'", "*", "|", ";", "%", " ", "-", "/", ".", "=", "#", "@", "+", "_", "<", ">", "^", "$", "?", "&", ",", "ı", "İ", "ş", "Ş", "ü", "Ü", "ç", "Ç", "ğ", "Ğ", "ö", "Ö"])>
}
/** Root production. */
 void Input() : {
System.out.println("Parsing started!");
}
{
( (<HEADER> )
 | (<TITLE> )
 | (<DETAIL> )
| <CODE_LINE>
| <DEFINE_LINE> )*

 <END_STATEMENT>

<EOF>
{System.out.println("Parsing is completed with success!");} }

The input lies at the bottom.


The output:
*H**ENAT230320070927120267 MVS/ESA 1 0AE B


*C** ACCOUNT ACCB001 F S
*D01NAT2303F ACCOUNT ACCB001 DBA0240 DBA0240 0000 S
*D02 2007072602061052007072602061050000009132
*D03MVS/ESA CICS NATP
*D04
*S**0010************************************************************************
*S**0020* SYSTEM NAME : ACCOUNT *
*S**0030* PROGRAM NAME : ACCB001 *
*S**0040* PROGRAMER NAME : MAGED EL-SHARKAWY *
*S**0050* DATE WRITTEN : SEP-29-1999 *
*S**0060* DATE UPDATED : *
*S**0070************************************************************************
*S**0080* P U R P U S E : THIS PROGRAM IS REPORT ABOUT MONTHLY REPORT *
*S**0090************************************************************************
*S**0100* LANGUAGE NAME : NATURAL 2.2 *
*S**0110* GLOBAL DATA AREA : ACCG001 *
*S**0120* SCREEN NAME : ACCO001 *
*S**0130* CHAINED PROGRAMS : ACCP950 *
*S**0140************************************************************************
*S**0150DEFINE DATA
*S**0160 GLOBAL USING ACCG001
*S**0170 LOCAL USING ACCL001B
*S**0180END-DEFINE
*S**0190*
*S**0200FORMAT PS=62 LS=132
*S**0210INPUT #PARM
*S**0220EXAMINE #PARM FOR '@' REPLACE WITH ' '
*S**0230CALLNAT 'LANGCHK' +LANG +HDATE
*S**0240*
*S**0250MOVE #MONTH-DESC(#MONTH) TO #MONTH-DES
*S**0260MOVE #CHAP-DESC(#CHAPTER) TO #BAB-DESC
*S**0270AT TOP OF PAGE
*S**0280 WRITE NOTITLE NOHDR USING MAP 'ACCO001'
*S**0290END-TOPPAGE
*S**0300*
*S**0310MOVE #SIT TO #SITE1 #SITE2
*S**0320MOVE #YEAR TO #C-YEAR1 #C-YEAR2
*S**0330MOVE 999999 TO #C-NO2
*S**0340MOVE 999999999999 TO #ACNT-ACC2
*S**0350*
*S**0360IF #CHAPTER = 1
*S**0370 FIND TBBUDDST-V WITH DST_YEAR = #YEAR-SIT
*S**0380 SORTED BY DST_ACNT_G
*S**0390 WHERE DST_CHAPTER = #CHAPTER
*S**0400 AND DST_ITEM > 0
*S**0410 AND DST_ELEMENT = 0
*S**0420 AND DST_SUB_ITEM = 0
*S**0430 AND DST_DEPT_CODE = 0
*S**0440 MOVE DST_CHAPTER TO #ACNT-CHAPTER
*S**0450 #ACNT-CHAPTER2
*S**0460 MOVE DST_ITEM TO #ACNT-ITEM
*S**0470 #ACNT-ITEM2
*S**0480 MOVE DST_SHORT_NAME TO #ACNT-DESC
*S**0490 MOVE DST_PRIMARY TO #ACNT
*S**0500 ADD DST_PRIMARY TO #ACNT-T
*S**0510 PERFORM READ-JDT
*S**0520 WRITE NOTITLE NOHDR USING MAP 'ACCO001A'
*S**0530 RESET #ACNT #ACNT-CHAPTER #ACNT-DESC #ACNT-ELEMENT #ACNT-ITEM
*S**0540 #ACNT-SUB-ITEM #CURRENT #PAST #TOTAL
*S**0550 END-FIND
*S**0560 WRITE NOTITLE NOHDR USING MAP 'ACCO001B'
*S**0570 RESET #ACNT-T #CURRENT-T #PAST-T #TOTAL-T
*S**0580END-IF
*S**0590IF #CHAPTER = 2
*S**0600 FIND TBBUDDST-V WITH DST_YEAR = #YEAR-SIT
*S**0610 SORTED BY DST_ACNT_G
*S**0620 WHERE DST_CHAPTER = #CHAPTER
*S**0630 AND DST_ITEM > 0
*S**0640 MOVE DST_CHAPTER TO #ACNT-CHAPTER
*S**0650 #ACNT-CHAPTER2
*S**0660 MOVE DST_ITEM TO #ACNT-ITEM
*S**0670 #ACNT-ITEM2
*S**0680 MOVE DST_ELEMENT TO #ACNT-ELEMENT
*S**0690 #ACNT-ELEMENT2
*S**0700 IF DST_ITEM = 206 OR = 208 OR = 222
*S**0710 IF DST_ELEMENT = 0
*S**0720 OR NOT DST_SUB_ITEM = 0
*S**0730 OR NOT DST_DEPT_CODE = 0
*S**0740 ESCAPE TOP*S**0750 END-IF
*S**0760 PERFORM READ-JDT
*S**0770 ELSE
*S**0780 IF NOT DST_ELEMENT = 0
*S**0790 OR NOT DST_SUB_ITEM = 0
*S**0800 OR NOT DST_DEPT_CODE = 0
*S**0810 ESCAPE TOP
*S**0820 END-IF
*S**0830 MOVE 999 TO #ACNT-ELEMENT2
*S**0840 PERFORM READ-JDT
*S**0850 END-IF
*S**0860 MOVE DST_SHORT_NAME TO #ACNT-DESC
*S**0870 MOVE DST_PRIMARY TO #ACNT
*S**0880 ADD DST_PRIMARY TO #ACNT-T
*S**0890 WRITE NOTITLE NOHDR USING MAP 'ACCO001A'
*S**0900 RESET #ACNT #ACNT-CHAPTER #ACNT-DESC #ACNT-ELEMENT #ACNT-ITEM
*S**0910 #ACNT-SUB-ITEM #CURRENT #PAST #TOTAL
*S**0920 END-FIND
*S**0930 WRITE NOTITLE NOHDR USING MAP 'ACCO001B'
*S**0940END-IF
*S**0950*
*S**0960IF #CHAPTER = 3
*S**0970 FIND TBBUDDST-V WITH DST_YEAR = #YEAR-SIT
*S**0980 SORTED BY DST_ACNT_G
*S**0990 WHERE DST_CHAPTER = #CHAPTER
*S**1000 AND DST_ITEM > 0
*S**1010* AND DST_ELEMENT = 0
*S**1020* AND DST_SUB_ITEM = 0
*S**1030 AND DST_DEPT_CODE = 0
*S**1040 MOVE DST_ELEMENT TO #DST-ELEMENT
*S**1050 MOVE DST_ITEM TO #DST-ITEM
*S**1060 RESET #COUNT
*S**1070 PERFORM READ-DST
*S**1080 IF #COUNT = 1 AND DST_ELEMENT = 0
*S**1090 ESCAPE TOP
...

...
...
...
*S**2560END-FIND
*S**2570**************
*S**2580END-SUBROUTINE
*S**2590**************
*S**2600*
*S**2610END
*C** ACCOUNT ACCB002 F S
*D01NAT2303F ACCOUNT ACCB002 DBA0240 DBA0240 0000 S
*D02 2007072602061262007072602061260000005996
*D03MVS/ESA CICS NATP
*D04
*S**0010************************************************************************
*S**0020* SYSTEM NAME : ACCOUNT *
*S**0030* PROGRAM NAME : ACCB002 *
*S**0040* PROGRAMER NAME : BANDER *
*S**0050* DATE WRITTEN : DEC-11-2000 *
*S**0060* DATE UPDATED : *
*S**0070************************************************************************
*S**0080* P U R P U S E : THIS PROGRAM IS REPORT ABOUT MONTHLY REPORT *
*S**0090************************************************************************
*S**0100* LANGUAGE NAME : NATURAL 2.2 *
*S**0110* GLOBAL DATA AREA : ACCG001 *
*S**0120* SCREEN NAME : ACCO002 *
*S**0130* CHAINED PROGRAMS : ACCP950 *
*S**0140************************************************************************
*S**0150DEFINE DATA
*S**0160 GLOBAL USING ACCG001
*S**0170 LOCAL USING ACCL002B
*S**0180END-DEFINE
*S**0190*
*S**0200FORMAT PS=62 LS=132
*S**0210INPUT #PARM
*S**0220EXAMINE #PARM FOR '@' REPLACE WITH ' '
*S**0230CALLNAT 'LANGCHK' +LANG +HDAT
*S**0240*

...
...
...

Thursday 19 July 2012

LL Parsing

LL parsing reads input from Left to write and and produces a Left most derivation. Following Java program uses the example provided at Wikipedia LL page.




/*
* converted to Java by Ali R+ SARAL
*/

package nbParse;

import com.sun.xml.internal.fastinfoset.util.CharArray;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
*
* @author Ali Riza SARAL
*/

public class LLparser {

public enum Symbols {
TS_L_PARENS, // (
TS_R_PARENS, // )
TS_A, // a
TS_PLUS, // +
TS_EOS, // $
TS_INVALID, // invalid token

// Non-terminal symbols:
NTS_S, // S
NTS_F
}



Symbols lexer(char c) {

switch (c) {

case '(':
return Symbols.TS_L_PARENS;

case ')':
return Symbols.TS_R_PARENS;

case 'a':
return Symbols.TS_A;

case '+':
return Symbols.TS_PLUS;

case '\0':
return Symbols.TS_EOS;

default:
return Symbols.TS_INVALID;
}
}

public static void main(String[] args) {

LLparser llparser = new LLparser();
System.out.println("Hello wonderful world!");
HashMap tt = new HashMap();
HashMap> table = new HashMap(); // LL Parser
Stack ss = new Stack(); // symbol stack

// char[] p = {'(','a', '+', 'a',')'}; // input buffer
char[] p = {'(','a', '+','(','a', '+', 'a',')',')'}; // input buffer
// char[] p = {'a','+','(','a', '+', 'a',')'}; // input buffer
// char[] p = {'(', 'a', '+', 'a', ')'}; // input buffer

int ppointer = 0;

// initialize the symbols stack
ss.addElement(Symbols.TS_EOS);
ss.addElement(Symbols.NTS_S);

//initialize the symbol stream cursor
ppointer = 0;

//setup the parsing table
tt.put(Symbols.TS_L_PARENS, 2);
table.put(Symbols.NTS_S, tt);

tt.put(Symbols.TS_A, 1);
table.put(Symbols.NTS_S, tt);

tt.put(Symbols.TS_A, 3);
table.put(Symbols.NTS_F, tt);

System.out.println("ss= " + ss.toString());
System.out.println("tt=" + tt);
System.out.println("table=" + table);
System.out.println("p= " + "(a+a)");

while (ss.size() > 1) {
System.out.println("\nppointer= " + ppointer);
System.out.println("lexer(p(ppointer))= " + llparser.lexer(p[ppointer]));
System.out.println("ss.lastElement= " + ss.lastElement());

if (llparser.lexer(p[ppointer]) == ss.lastElement()) {
System.out.println("Matched symbols " + llparser.lexer(p[ppointer]));

ppointer++;

ss.pop();

if (ppointer > (p.length - 1)) {

System.out.println("\nEnd of input string");
System.out.println(ss.toString());

if (ss.size() > 1) {
System.out.println("Parse is unsuccessful");
}

return;
}
} else {
HashMap tableItem = table.get(ss.lastElement());
System.out.println("Rule " + tableItem.get(llparser.lexer(p[ppointer])));

switch (tableItem.get(llparser.lexer(p[ppointer]))) {

case 1: // 1. s-> F
System.out.println("1. s-> F");
ss.pop();
ss.push(Symbols.NTS_F); // F
break;

case 2: // 2. s-> ( S + F )
System.out.println("2 s-> ( S + F )");
ss.pop();
ss.push(Symbols.TS_R_PARENS); // )
ss.push(Symbols.NTS_F); // F
ss.push(Symbols.TS_PLUS); // +
ss.push(Symbols.NTS_S); // S
ss.push(Symbols.TS_L_PARENS); // (

break;

case 3: // F -> A
System.out.println("3 F -> A");

ss.pop();
ss.push(Symbols.TS_A); // a

break;

default:
System.out.println("Parsing table default");

return;
}
}

System.out.println("ss= " + ss.toString());
System.out.println("ss.size()= " + ss.size());
}

System.out.println("Finished parsing");

return;
}
}



Output:

run:

Hello wonderful world!

ss= [TS_EOS, NTS_S]
tt={TS_A=3, TS_L_PARENS=2}
table={NTS_F={TS_A=3, TS_L_PARENS=2}, NTS_S={TS_A=3, TS_L_PARENS=2}}
p= (a+a)
ppointer= 0
lexer(p(ppointer))= TS_L_PARENS
ss.lastElement= NTS_S

Rule 2
2 s-> ( S + F )
ss= [TS_EOS, TS_R_PARENS, NTS_F, TS_PLUS, NTS_S, TS_L_PARENS]
ss.size()= 6

ppointer= 0
lexer(p(ppointer))= TS_L_PARENS
ss.lastElement= TS_L_PARENS

Matched symbols TS_L_PARENS
ss= [TS_EOS, TS_R_PARENS, NTS_F, TS_PLUS, NTS_S]
ss.size()= 5



ppointer= 1
lexer(p(ppointer))= TS_A
ss.lastElement= NTS_S

Rule 3
3 F -> A
ss= [TS_EOS, TS_R_PARENS, NTS_F, TS_PLUS, TS_A]
ss.size()= 5

ppointer= 1
lexer(p(ppointer))= TS_A
ss.lastElement= TS_A

Matched symbols TS_A
ss= [TS_EOS, TS_R_PARENS, NTS_F, TS_PLUS]
ss.size()= 4

ppointer= 2
lexer(p(ppointer))= TS_PLUS
ss.lastElement= TS_PLUS

Matched symbols TS_PLUS
ss= [TS_EOS, TS_R_PARENS, NTS_F]
ss.size()= 3

ppointer= 3
lexer(p(ppointer))= TS_L_PARENS
ss.lastElement= NTS_F

Rule 2
2 s-> ( S + F )
ss= [TS_EOS, TS_R_PARENS, TS_R_PARENS, NTS_F, TS_PLUS, NTS_S, TS_L_PARENS]
ss.size()= 7

ppointer= 3
lexer(p(ppointer))= TS_L_PARENS
ss.lastElement= TS_L_PARENS

Matched symbols TS_L_PARENS
ss= [TS_EOS, TS_R_PARENS, TS_R_PARENS, NTS_F, TS_PLUS, NTS_S]
ss.size()= 6

ppointer= 4
lexer(p(ppointer))= TS_A
ss.lastElement= NTS_S

Rule 3
3 F -> A
ss= [TS_EOS, TS_R_PARENS, TS_R_PARENS, NTS_F, TS_PLUS, TS_A]
ss.size()= 6

ppointer= 4
lexer(p(ppointer))= TS_A
ss.lastElement= TS_A

Matched symbols TS_A
ss= [TS_EOS, TS_R_PARENS, TS_R_PARENS, NTS_F, TS_PLUS]
ss.size()= 5

ppointer= 5
lexer(p(ppointer))= TS_PLUS
ss.lastElement= TS_PLUS

Matched symbols TS_PLUS
ss= [TS_EOS, TS_R_PARENS, TS_R_PARENS, NTS_F]
ss.size()= 4

ppointer= 6
lexer(p(ppointer))= TS_A
ss.lastElement= NTS_F

Rule 3
3 F -> A
ss= [TS_EOS, TS_R_PARENS, TS_R_PARENS, TS_A]
ss.size()= 4

ppointer= 6
lexer(p(ppointer))= TS_A
ss.lastElement= TS_A

Matched symbols TS_A
ss= [TS_EOS, TS_R_PARENS, TS_R_PARENS]
ss.size()= 3

ppointer= 7
lexer(p(ppointer))= TS_R_PARENS
ss.lastElement= TS_R_PARENS

Matched symbols TS_R_PARENS
ss= [TS_EOS, TS_R_PARENS]
ss.size()= 2

ppointer= 8
lexer(p(ppointer))= TS_R_PARENS
ss.lastElement= TS_R_PARENS

Matched symbols TS_R_PARENS


End of input string
[TS_EOS]

BUILD SUCCESSFUL (total time: 0 seconds)



LR parsing

SLR parsing is simple LR parsing, reads input from Left to write and and produces a Right most derivation. Following Java program uses the example provided at Wikipedia SLR page.


/*
* by Ali R+ SARAL
* Please indicate my name if you copy my work.
*/

package nbParse;
import com.sun.xml.internal.fastinfoset.util.CharArray;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;

/**
*
* @author Ali Riza SARAL
*/

public class LRparser {

    public enum Symbols {

      TS_ONE, // +
      TS_EOS, // $
      TS_INVALID, // invalid token

      // Non-terminal symbols:
      NTS_S, // S
      NTS_E
      }

   public enum States {
      ST_ZERO,
      ST_ONE,
      ST_TWO,
      ST_THREE
   }

   Symbols lexer(char c) {

      switch (c) {
      case '1':
      return Symbols.TS_ONE;

      case '$':
      return Symbols.TS_EOS;

      case 'E':
      return Symbols.NTS_E;

      default:
      return Symbols.TS_INVALID;
      }
   }

   public static void main(String[] args) {

      LRparser lrparser = new LRparser();
      StringBuilder p = new StringBuilder("111$"); // input buffer

      int ppointer = 0;
      int state = 0;

      //initialize the symbol stream cursor
      ppointer = 0;
      boolean reduced = false;

      for (ppointer = 0; ppointer < p.length(); ppointer++) {

         if (reduced) {
         ppointer = 0;
         reduced = false;
      }

      System.out.println("\np[" + ppointer + "]= " + p.charAt(ppointer));
      System.out.println("lexer(p(ppointer))= " + lrparser.lexer(p.charAt(ppointer)));
      System.out.println("state= " + state);
      System.out.println("p= " + p.toString());

switch (state) {

   case 0:
   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_ONE) {
   System.out.println("1. shift one state 0");
   state = 1;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_EOS) {
   System.out.println("none");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.NTS_E) {
   System.out.println("goto state 2");
   state = 2;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_INVALID) {
   System.out.println("invalid input");
   return;
   }

   break;

   case 1:
   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_ONE) {
   System.out.println("1. shift one state 1");
   state = 1;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_EOS) {
   System.out.println("reduce 2 ");
   p.deleteCharAt(ppointer - 1);
   p.insert(ppointer - 1, 'E');
   state = 0;
   ppointer = 0;
   reduced = true;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.NTS_E) {
   System.out.println("goto state 3");
   state = 3;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_INVALID) {
   System.out.println("invalid input");
   return;
   }

   break;

   case 2:
   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_ONE) {
   System.out.println("none state 2.1");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_EOS) {
   System.out.println("accept ");
   state = 0;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.NTS_E) {
   System.out.println("none state 2.3");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_INVALID) {
   System.out.println("invalid input");
   return;
   }

   break;

   case 3:
   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_ONE) {
   System.out.println("none state 3.1");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_EOS) {
   System.out.println("reduce 1 ");
   p.deleteCharAt(ppointer - 2);
   state = 0;
   ppointer = 0;
   reduced = true;
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.NTS_E) {
   System.out.println("none state 3.3");
   }

   if (lrparser.lexer(p.charAt(ppointer)) == Symbols.TS_INVALID) {
   System.out.println("invalid input");
   return;
   }

   break;
   default:
   System.out.println("Parsing table default");
   return;
   }
   }

   System.out.println("Finished parsing");
   return;
   }
}


Output:

run:
p[0]= 1
lexer(p(ppointer))= TS_ONE
state= 0
p= 111$
1. shift one state 0

p[1]= 1
lexer(p(ppointer))= TS_ONE
state= 1
p= 111$
1. shift one state 1

p[2]= 1
lexer(p(ppointer))= TS_ONE
state= 1
p= 111$
1. shift one state 1

p[3]= $
lexer(p(ppointer))= TS_EOS
state= 1
p= 111$
reduce 2

p[0]= 1
lexer(p(ppointer))= TS_ONE
state= 0
p= 11E$
1. shift one state 0

p[1]= 1
lexer(p(ppointer))= TS_ONE
state= 1
p= 11E$
1. shift one state 1

p[2]= E
lexer(p(ppointer))= NTS_E
state= 1
p= 11E$
goto state 3

p[3]= $
lexer(p(ppointer))= TS_EOS
state= 3
p= 11E$
reduce 1

p[0]= 1
lexer(p(ppointer))= TS_ONE
state= 0
p= 1E$
1. shift one state 0

p[1]= E
lexer(p(ppointer))= NTS_E
state= 1
p= 1E$
goto state 3

p[2]= $
lexer(p(ppointer))= TS_EOS
state= 3
p= 1E$
reduce 1
none state 3.3

p[0]= E
lexer(p(ppointer))= NTS_E
state= 0
p= E$
goto state 2

p[1]= $
lexer(p(ppointer))= TS_EOS
state= 2
p= E$
accept

Finished parsing
BUILD SUCCESSFUL (total time: 0 seconds)

Tuesday 17 July 2012

Simple2.jj Second Simple JavaCC example

The second example is a simple improvement.

This one does not abend when the input does not
have the word test in it. It gives due
warning instead.

options {
    IGNORE_CASE = true;
   DEBUG_PARSER = false;
   DEBUG_LOOKAHEAD = false;
   DEBUG_TOKEN_MANAGER = false;
   LOOKAHEAD = 2;
   FORCE_LA_CHECK=true;
}

PARSER_BEGIN(Simple2)

public class Simple2 {

   /** Main entry point. */
   public static void main(String args[]) throws ParseException {

   Simple2 parser = new Simple2(System.in);

   parser.Input();
   }

}

PARSER_END(Simple2)

SKIP : {
   " "
   "\t"
   "\n"
   "\r"
}
TOKEN: { < TEST: "test">}

TOKEN: {<CR_LF: "\r\n"> }

TOKEN : { <OTHER : ~[] > }

void Input() :
{
   System.out.println("Start");
   Token t1=null;}
{
   (
      (<OTHER>)*
      (t1=<TEST> {System.out.println("This line has test in it.");})?
      (<OTHER>)*

      <CR_LF> {if (t1==null) System.out.println("Sorry no test in this line!");}
   )*
   <EOF >
}

Simple1.jj First JavaCC example

options {
   IGNORE_CASE = true;
   DEBUG_PARSER = false;
   DEBUG_LOOKAHEAD = false;
   DEBUG_TOKEN_MANAGER = false;
   LOOKAHEAD = 2;
   FORCE_LA_CHECK=true;
}

PARSER_BEGIN(Simple1)

public class Simple1 {

   /** Main entry point. */
   public static void main(String args[]) throws ParseException {
   Simple1 parser = new Simple1(System.in);

   parser.Input();
   }
}

PARSER_END(Simple1)

SKIP : {
   " "
   "\t"
   "\n"
   "\r"
}
TOKEN: { < TEST: "test" > }

TOKEN: {<CR_LF: "\r\n"> }

TOKEN : { < OTHER : ~[] > }

void Input() :
{System.out.println("Start");}
{
   ((<OTHER>)* <TEST> (<OTHER>)* <CR_LF>)*
}



Natural-ADABAS to JAVA-Oracle-UNIX program conversion

This will be the first of many simle and advanced JavaCC and jjTree examples

that I will provide here in my Techne-Tekne blog.

I have produced these examples during a feasibility project which
is about the conversion of NATURAL-ADABAS software of an IBM mainframe
system to JAVA, STRUTS, ORACLE, UNIX 'mini' system which may be non-IBM.

I will provide many examples for: NATURAL local-global variable conversion to JAVA,
Transaction processing, flow control by using threads to enable online programs
to use maps, field value transfer from maps to the transaction program and vice versa.

Let's begin with some very simple JavaCC examples:
 
Please continue to: Simple1

Wednesday 28 March 2012

A Few Notes on Programming Web Services with XML-RPC

http://www.tutorialspoint.com/xml-rpc/xml_rpc_examples.htm gives the below example:

import org.apache.xmlrpc.*;

public class JavaServer {

public Integer sum(int x, int y) {
return new Integer(x+y);
}

public static void main (String [] args) {
try {

System.out.println("Attempting to start XML-RPC Server...");
WebServer server = new WebServer(80);
server.addHandler("sample", new JavaServer());
server.start();
System.out.println("Started successfully.");
System.out.println("Accepting requests. (Halt program to stop.)");
} catch (Exception exception) {
System.err.println("JavaServer: " + exception);
}
}
}

------------------------------------

import java.util.*;
import org.apache.xmlrpc.*;

public class JavaClient {
public static void main (String [] args) {
try {
XmlRpcClient server = new XmlRpcClient("http://localhost:80");
Vector params = new Vector();
params.addElement(new Integer(17));
params.addElement(new Integer(13));

Object result = server.execute("sample.sum", params);

int sum = ((Integer) result).intValue();
System.out.println("The sum is: "+ sum);

} catch (Exception exception) {
System.err.println("JavaClient: " + exception);
}
}
}

This requires: xmlrpc-1.2.jar
in the Java build path. It works OK. No problem.

I will use this example to solve minor problems of 3 examples from
books.google.com - "Programming Web Services with XML-RPC of Simon Laurent, Joe Johnston,
and Edd Dumbill."

The problems that I met are:


1- Installing the helma.xmlrpc Library
You can not find this lib because it has evolved toxmlrpc.jar
2-the location of the handler
The examples are given in such fashion that the position of the handler
is left ambiguous. It should be an inner class of the server class.
3- the handler must be static
4- client-or the server does not need io try catch
5- Some examples require import for io
6- port problem connection error
The examples suggests a port number which does not connect at least on my machine.

I removed param processing which can easily be restored. I also
put the server and client into the same project for simplicity.
You should put them in seperate projects also.

An example of batch running follows:

1- XML-RPChandler project


C:\Users\ars\Desktop\eclipseWS\eclipseJAXWSrpc\XML-RPCproject\bin>java -cp ../li
b/xmlrpc-1.2.jar;. JavaServer
Attempting to start XML-RPC Server...
Started successfully.
Accepting requests. (Halt program to stop.)


C:\Users\ars\Desktop\eclipseWS\eclipseJAXWSrpc\XML-RPCproject\bin>java -cp ../li
b/xmlrpc-1.2.jar;. JavaClient
The sum is: 30


The corrected and running solutions lie below:

import java.io.IOException;
//import helma.xmlrpc.WebServer;
//import helma.xmlrpc.XmlRpc;
import org.apache.xmlrpc.*;

public class AreaServer {

public static class AreaHandler {

public Double rectArea(double length, double width) {
return new Double(length * width);
}

public Double circleArea(double radius) {
double value = (radius * radius * Math.PI);
return new Double(value);
}

}

public static void main(String[] args) {
// if (args.length < 1) {
// System.out.println(
// "Usage: java AreaServer [port]");
// System.exit(-1);
// }

// try {

// Start the server, using built-in version
System.out.println("Attempting to start XML-RPC Server...");
// WebServer server = new WebServer(Integer.parseInt(args[0]));
WebServer server = new WebServer(80);

System.out.println("Started successfully.");

// Register our handler class as area
//server.addHandler("area", new AreaHandler());
server.addHandler("area", new AreaHandler());
System.out.println("Registered AreaHandler class to area.");
server.start();
System.out.println("Started successfully.");
System.out.println("Now accepting requests. (Halt program to stop.)");

// } catch (IOException e) {
// System.out.println("Could not start server: " +
// e.getMessage( ));
// }
}

}
----------------------------------
import java.io.IOException;
import java.util.Vector;
//import helma.xmlrpc.XmlRpc;
//import helma.xmlrpc.XmlRpcClient;
//import helma.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.*;

public class AreaClient {

public static void main(String args[]) {
// if (args.length < 1) {
// System.out.println(
// "Usage: java AreaClient [radius]");
// System.exit(-1);
// }

try {
// Create the client, identifying the server
XmlRpcClient client =
new XmlRpcClient("http://localhost:80/");

// Create the request parameters using user input
Vector params = new Vector( );
//params.addElement(new Double(args[0]));
params.addElement(new Double("5"));

// Issue a request
Object result =
client.execute("area.circleArea", params);

// Report the results
System.out.println("The area of the circle would be: " + result.toString( ));

} catch (IOException e) {
System.out.println("IO Exception: " + e.getMessage( ));
} catch (XmlRpcException e) {
System.out.println("Exception within XML-RPC: " + e.getMessage( ));
}
}

}



2.passing hash parameter example

import java.io.IOException;
import java.util.Hashtable;
//import helma.xmlrpc.WebServer;
//import helma.xmlrpc.XmlRpc;
import org.apache.xmlrpc.*;

public class AreaServer {

public static class AreaHandler {

public Double rectArea(double length, double width) {
return new Double(length * width);
}

public Double circleArea(double radius) {
double value = (radius * radius * Math.PI);
return new Double(value);
}

public Double anyArea(Hashtable arguments) {
Double value;
value=new Double(0);
String requestType=(String) arguments.get("type");
if (requestType.equals("circle")) {
Double radius=(Double) (arguments.get("radius"));
value=circleArea(radius.doubleValue( ));
}
if (requestType.equals("rectangle")) {
Double length=(Double) (arguments.get("length"));
Double width=(Double) (arguments.get("width"));
value=rectArea(length.doubleValue(), width.doubleValue( ));
}

return value;
}


}

public static void main(String[] args) {
// if (args.length < 1) {
// System.out.println(
// "Usage: java AreaServer [port]");
// System.exit(-1);
// }

// try {

// Start the server, using built-in version
System.out.println("Attempting to start XML-RPC Server...");
// WebServer server = new WebServer(Integer.parseInt(args[0]));
WebServer server = new WebServer(80);

System.out.println("Started successfully.");

// Register our handler class as area
//server.addHandler("area", new AreaHandler());
server.addHandler("area", new AreaHandler());
System.out.println("Registered AreaHandler class to area.");
server.start();
System.out.println("Started successfully.");
System.out.println("Now accepting requests. (Halt program to stop.)");

// } catch (IOException e) {
// System.out.println("Could not start server: " +
// e.getMessage( ));
// }
}

}
-------------------------------------------------
import java.io.IOException;
import java.util.Hashtable;
import java.util.Vector;
//import helma.xmlrpc.XmlRpc;
//import helma.xmlrpc.XmlRpcClient;
//import helma.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.*;

public class AreaClient {

public static void main(String args[]) {
// if (args.length < 1) {
// System.out.println(
// "Usage: java AreaClient [radius]");
// System.exit(-1);
// }

try {
// Create the client, identifying the server
XmlRpcClient client =
new XmlRpcClient("http://localhost:80/");

// Create a double from the user argument
//Double radius=new Double(args[0]);
Double radius=new Double("5");

// Create a hashtable and add a circle request
Hashtable requestHash = new Hashtable( );
requestHash.put("type", "circle");
requestHash.put("radius", radius);

// Create the request parameters using user input
Vector params = new Vector( );
params.addElement(requestHash);

// Issue a request
Object result =
client.execute("area.anyArea", params);

// Report the results
System.out.println("The area of the circle would be: " + result.toString( ));


} catch (IOException e) {
System.out.println("IO Exception: " + e.getMessage( ));
} catch (XmlRpcException e) {
System.out.println("Exception within XML-RPC: " + e.getMessage( ));
}
}

}


3. log service program
import java.io.IOException;
//import helma.xmlrpc.WebServer;
//import helma.xmlrpc.XmlRpc;
import org.apache.xmlrpc.*;

public class XLogServer {

public static class XLogHandler {
public String XLogReport(String address, String message) {
System.out.println("From: " + address);
System.out.println("Message: " + message);
return "ack";
}
}

public static void main(String[] args) {
// if (args.length < 1) {
// System.out.println("Usage: java AreaServer [port]");
// System.exit(-1);
// }

// try {
// Start the server, using built-in version
System.out.println("Attempting to start XML-RPC Server...");
// WebServer server = new WebServer(Integer.parseInt(args[0]));
WebServer server = new WebServer(Integer.parseInt("80"));

System.out.println("Started successfully.");

// Register our handler class as area
server.addHandler("XLog", new XLogHandler());
System.out.println("Registered XLogHandler class to XLog.");
server.start();
System.out.println("Started successfully.");
System.out.println("Now accepting requests. (Halt program to stop.)");

// } catch (IOException e) {
// System.out.println("Could not start server: " +
// e.getMessage( ));
// }
}
}
-----------------------------------------------------------
import java.io.IOException;
import java.net.*;
import java.util.Vector;
//import helma.xmlrpc.XmlRpc;
//import helma.xmlrpc.XmlRpcClient;
//import helma.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.*;

public class XLogClient {

public static void main(String args[]) {
try {
throw new Exception("help");
} catch (Exception e) {
report (e);
}
}

public static void report(Exception eReport) {
try {
// Create the client, identifying the server
XmlRpcClient client =
new XmlRpcClient("http://127.0.0.1:80/");
//new XmlRpcClient("http://192.168.2.191:80/");

//get local hostname and IP address
InetAddress address=InetAddress.getLocalHost( );
String ipAddress=address.toString( );

// Create the request parameters using user input
Vector params = new Vector( );
params.addElement(ipAddress);
params.addElement(eReport.getMessage( ));
// Issue a request
Object result =
client.execute("XLog.XLogReport", params);

// Report the results - this is just for the example
// In production, the 'ack' will be thrown away.
// Alternatively, the log system could be more interactive
// and the result might have meaning.
System.out.println("Response was: " + result.toString( ));

//If we can't report to server, report locally
} catch (IOException e) {
System.out.println("IO Exception: " + e.getMessage( ));
} catch (XmlRpcException e) {
System.out.println("Exception within XML-RPC: " + e.getMessage( ));
}
}
}

4. getting and setting a value

import java.io.IOException;
//import helma.xmlrpc.WebServer;
//import helma.xmlrpc.XmlRpc;
import org.apache.xmlrpc.*;

public class GetSetServer {

public static class GetSetHandler {
protected int value;

public GetSetHandler(int initialValue) {
value = initialValue;
}

public int getValue(String requester) {
return value;
}

public int setValue(String requester, int newValue) {
value = newValue;
return value;
}
}

public static void main(String[] args) {
// if (args.length < 1) {
// System.out.println(
// "Usage: java GetSetServer [port]");
// System.exit(-1);
// }
// try {
// Start the server, using built-in version
System.out.println("Attempting to start XML-RPC Server...");
// WebServer server = new WebServer(Integer.parseInt(args[0]));
WebServer server = new WebServer(Integer.parseInt("80"));

System.out.println("Started successfully.");

// Register our handler class as area
server.addHandler("getSet", new GetSetHandler(20));
System.out.println("Registered GetSetHandler class to getSet.");
server.start();
System.out.println("Started successfully.");
System.out.println("Now accepting requests. (Halt program to stop.)");

// } catch (IOException e) {
// System.out.println("Could not start server: " +
// e.getMessage( ));
// }
}
}
-------------------------------------------------
import java.io.IOException;
import java.net.*;
import java.util.Vector;
//import helma.xmlrpc.XmlRpc;
//import helma.xmlrpc.XmlRpcClient;
//import helma.xmlrpc.XmlRpcException;
import org.apache.xmlrpc.*;

public class GetSetClient {

public static void main(String args[]) {
if (args.length < 1) {
System.out.println(
"Usage: java GetSetClient [get set] [value]");
System.exit(-1);
}

String getOrSet=new String(args[0]);

if (!((getOrSet.equals("get")) (getOrSet.equals("set")))) {
System.out.println(
"First argument must be get or set");
System.exit(-1);
}

try {
// Create the client, identifying the server
XmlRpcClient client =
new XmlRpcClient("http://localhost:80/");

//get local host IP address
InetAddress address=InetAddress.getLocalHost( );
String ipAddress=address.toString( );

// Create the request parameters using user input
Vector params = new Vector( );
params.addElement(ipAddress);
if (getOrSet.equals("set")) {
Integer newValue=new Integer(args[1]);
params.addElement(newValue);
}

// Issue a request
Object result=null;
if (getOrSet.equals("set")) {
result = client.execute("getSet.setValue", params);
} else {
result = client.execute("getSet.getValue", params);
}

// Report the results
System.out.println("The response was: " + result.toString( ));

} catch (IOException e) {
System.out.println("IO Exception: " + e.getMessage( ));
} catch (XmlRpcException e) {
System.out.println("Exception within XML-RPC: " + e.getMessage( ));
}
}
}