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: