import java.io.*; import java.lang.*; import java.util.*; public class Lexer { /** * Entry point for the scanner. Returns the token identifier corresponding * to the next token and prepares to return the semantic value * of the token. * @return the token identifier corresponding to the next token. */ // from parser public final static Integer CHARACTERS=257; public final static Integer CONSTANTS=258; public final static Integer DO=259; public final static Integer ELSE=260; public final static Integer ELSIF=261; public final static Integer END=262; public final static Integer ENUM=263; public final static Integer EXPORT=264; public final static Integer FI=265; public final static Integer FIELD=266; public final static Integer FUNC=267; public final static Integer HIDE=268; public final static Integer IF=269; public final static Integer IN=270; public final static Integer EXTEND=271; public final static Integer LET=272; public final static Integer NI=273; public final static Integer OD=274; public final static Integer READONLY=275; public final static Integer RECORD=276; public final static Integer THEN=277; public final static Integer TYPE=278; public final static Integer UNION=279; public final static Integer USE=280; public final static Integer VAL=281; public final static Integer VAR=282; public final static Integer WITH=283; public final static Integer RIGHT_ARROW=284; public final static Integer EQUALS_EQUALS=285; public final static Integer EQUALS_EQUALS_EQUALS=286; public final static Integer COLON=287; public final static Integer WORDID=288; public final static Integer OPID=289; public final static Integer PROD=290; public final static Integer QSTRING=291; public final static Integer UQSTRING=292; public final static Integer LEFT_ANGLE_BRACKET=293; public final static Integer RIGHT_ANGLE_BRACKET=294; public final static Integer EXTERN=295; public final static Integer SIGNATURE=296; public final static Integer COR=297; public final static Integer CAND=298; public final static Integer YYERRCODE=256; // end parser public static final int EOF = -1; public static final int LIST_END = -1; private static final int EOFCC = -1; private static final int BADCC = 0; private static final int WHTCC = 1; private static final int LETCC = 2; private static final int DIGCC = 3; private static final int SEPCC = 4; private static final int OPRCC = 5; private static final int SQUCC = 6; private static final int DQUCC = 7; private static final char ESCCHAR = '#';/* signals position in preprocessor output */ public static int token_cnt; public static int numeral_cnt; public static int comments_cnt; public static int dbl_quoted_cnt; public static int seperator_cnt; public static String yydescr; private static StringBuffer current_line = new StringBuffer(""); private static String c_str = new String(""); private static int c; private static int cc; private static String scansavc_str = new String(""); private static int scansavc; private static int outtok; private static int stktop = -1; private static String scanstk = new String(""); private static boolean retry_loop; private static boolean saw_quote; private static String yycat; private static boolean do_retry; private static StringBuffer p = new StringBuffer(); private static Integer yylval = new Integer(0); private static int yyvline; private static int yyline; private static int startline; private static int tokenlgth; private static int sing_quoted_cnt; private static int startfnm; private static int yyinfnm; private static int cmtnest = 0; // private static boolean eof_flag = false; private static boolean initialized = false; private static Map restab = new HashMap(Globals.initsttsize); private static Map stringstab = new HashMap(Globals.initsttsize); private static void init_restab() { restab.put(new String("cand"), CAND); restab.put(new String("characters"), CHARACTERS); restab.put(new String("constants"), CONSTANTS); restab.put(new String("cor"), COR); restab.put(new String("do"), DO); restab.put(new String("else"), ELSE); restab.put(new String("elsif"), ELSIF); restab.put(new String("enum"), ENUM); restab.put(new String("export"), EXPORT); restab.put(new String("extend"), EXTEND); restab.put(new String("extern"), EXTERN); restab.put(new String("fi"), FI); restab.put(new String("field"), FIELD); restab.put(new String("func"), FUNC); restab.put(new String("hide"), HIDE); restab.put(new String("if"), IF); restab.put(new String("in"), IN); Mine.russinfo("SET restab let "); restab.put(new String("let"), LET); restab.put(new String("ni"), NI); restab.put(new String("od"), OD); restab.put(new String("prod"), PROD); restab.put(new String("readonly"), READONLY); restab.put(new String("record"), RECORD); restab.put(new String("signature"), SIGNATURE); restab.put(new String("then"), THEN); restab.put(new String("type"), TYPE); restab.put(new String("union"), UNION); restab.put(new String("use"), USE); restab.put(new String("val"), VAL); restab.put(new String("var"), VAR); restab.put(new String("with"), WITH); restab.put(new String(":"), COLON); restab.put(new String("<<"), LEFT_ANGLE_BRACKET); restab.put(new String("=="), EQUALS_EQUALS); restab.put(new String("==="), EQUALS_EQUALS_EQUALS); restab.put(new String("==>"), RIGHT_ARROW); restab.put(new String(">>"), RIGHT_ANGLE_BRACKET); } private static int[] cctab = { BADCC, /* NUL */ BADCC, /* SOH */ SEPCC, /* STX */ /* used as user program marker in prologue */ BADCC, /* ETX */ BADCC, /* EOT */ BADCC, /* ENQ */ BADCC, /* ACK */ BADCC, /* BEL */ OPRCC, /* BS */ WHTCC, /* HT */ WHTCC, /* LF */ BADCC, /* VT */ WHTCC, /* FF */ WHTCC, /* CR */ BADCC, /* SO */ BADCC, /* SI */ BADCC, /* DLE */ BADCC, /* DC1 */ BADCC, /* DC2 */ BADCC, /* DC3 */ BADCC, /* DC4 */ BADCC, /* NAK */ BADCC, /* SYN */ BADCC, /* ETB */ BADCC, /* CAN */ BADCC, /* EM */ BADCC, /* SUB */ BADCC, /* ESC */ BADCC, /* FS */ BADCC, /* GS */ BADCC, /* RS */ BADCC, /* US */ WHTCC, /* SP */ OPRCC, /* ! */ DQUCC, /* " */ SEPCC, /* # */ SEPCC, /* $ */ OPRCC, /* % */ OPRCC, /* & */ SQUCC, /* ' */ SEPCC, /* ( */ SEPCC, /* ) */ OPRCC, /* * */ OPRCC, /* + */ SEPCC, /* , */ OPRCC, /* - */ OPRCC, /* . */ OPRCC, /* / */ DIGCC, /* 0 */ DIGCC, /* 1 */ DIGCC, /* 2 */ DIGCC, /* 3 */ DIGCC, /* 4 */ DIGCC, /* 5 */ DIGCC, /* 6 */ DIGCC, /* 7 */ DIGCC, /* 8 */ DIGCC, /* 9 */ OPRCC, /* : */ SEPCC, /* ; */ OPRCC, /* < */ OPRCC, /* = */ OPRCC, /* > */ OPRCC, /* ? */ OPRCC, /* @ */ LETCC, /* A */ LETCC, /* B */ LETCC, /* C */ LETCC, /* D */ LETCC, /* E */ LETCC, /* F */ LETCC, /* G */ LETCC, /* H */ LETCC, /* I */ LETCC, /* J */ LETCC, /* K */ LETCC, /* L */ LETCC, /* M */ LETCC, /* N */ LETCC, /* O */ LETCC, /* P */ LETCC, /* Q */ LETCC, /* R */ LETCC, /* S */ LETCC, /* T */ LETCC, /* U */ LETCC, /* V */ LETCC, /* W */ LETCC, /* X */ LETCC, /* Y */ LETCC, /* Z */ SEPCC, /* [ */ OPRCC, /* \ */ SEPCC, /* ] */ OPRCC, /* ^ */ LETCC, /* _ */ OPRCC, /* ` */ LETCC, /* a */ LETCC, /* b */ LETCC, /* c */ LETCC, /* d */ LETCC, /* e */ LETCC, /* f */ LETCC, /* g */ LETCC, /* h */ LETCC, /* i */ LETCC, /* j */ LETCC, /* k */ LETCC, /* l */ LETCC, /* m */ LETCC, /* n */ LETCC, /* o */ LETCC, /* p */ LETCC, /* q */ LETCC, /* r */ LETCC, /* s */ LETCC, /* t */ LETCC, /* u */ LETCC, /* v */ LETCC, /* w */ LETCC, /* x */ LETCC, /* y */ LETCC, /* z */ SEPCC, /* { */ OPRCC, /* | */ SEPCC, /* } */ OPRCC, /* ~ */ BADCC /* DEL */ }; public static Object getLVal() { int yyv_it; Integer yy_outtok; yy_outtok = new Integer(outtok); yyv_it = yylval.intValue(); System.out.printf("getLVal outtok = %d yylval = %d cc = %d c = %d c_str = |%s|\n", outtok,yyv_it,cc,c,c_str); // return yylval; return yyv_it; } // reschk_id(p, dflt ) private static Integer str_chk(StringBuffer p) { int it; String tmp = p.toString(); Mine.russinfo("str_chk p = " + p); Globals.strings_cnt++; it = Stt.string_enter(tmp); yylval = new Integer(it); return yylval; } private static int reschk_id(StringBuffer p, int dflt) { int it; Integer val; String ky = p.toString(); Mine.russinfo("reschk_id p = " + p); if (restab.containsKey(ky)) { Mine.russinfo("restab contains key " + ky); val = restab.get(ky); it = val.intValue(); System.out.printf("it = %d\n",it); Globals.reserved_id_cnt++; return it; } else { Mine.russinfo("restab does not contain key " + ky); Globals.defined_id_cnt++; yylval = Stt.stt_enter(ky); it = yylval.intValue(); yydescr = yylval.toString(); System.out.printf("yylval = %d dflt = %d\n",it, dflt); return dflt; } } private static int reschk_op(StringBuffer p, int dflt) { int it; String ky = p.toString(); Mine.russinfo("reschk_op p = " + ky); if (restab.containsKey(ky)) { it = restab.get(ky); Globals.reserved_op_cnt++; return it; } else { Globals.defined_op_cnt++; yylval = Stt.stt_enter(ky); yydescr = yylval.toString(); return dflt; } } private static int cclass(int c) { int it2; System.out.printf("cclass c = %d\n",c); Mine.russinfo("cclass1 c_str" + c_str); if (c =='\n' || c == '\t') { Mine.russinfo("cclass WHTCC"); // write(ccode_file, "c = " || c || " class = " || WHTCC) System.out.printf("cclass WHTCC\n",c); return WHTCC; } if (c == EOF) { Mine.russinfo("cclass EOFCC"); // write(ccode_file, "c = " || c || " class = " || EOFCC) System.out.printf("cclass EOFCC\n",c); return EOFCC; } it2 = cctab[c]; // write(ccode_file, "c = " || c || " class = " || it) // Mine.russinfo("cclass c = " + "cctab[c] + " + it); System.out.printf("cclass it2 = %d\n",it2); return it2; } public static int yylex() { if (! initialized) { init_restab(); initialized = true; } Mine.russinfo("Hello yylex() !!!"); if (stktop >= 0) { outtok = scanstk.charAt(stktop); stktop--; token_cnt++; rpt_progress(p,yycat,outtok,yydescr); return outtok; } retry_loop = true; while (retry_loop) { c = scansavc; cc = cclass(c); yylval = new Integer(0); if (p.length() <= 0) p = new StringBuffer(""); // Will concat to while ( cc == WHTCC) { if( c == '\n' ) { yyline++; yyvline++; getChStr(); if( c == ESCCHAR & Globals.pflag) { rdposition(); c = '\n'; // Repeat check for ESCCHAR next time around c_str = "\n"; yyline--; yyvline--; // Line number is correct for next line } } else { getChStr(); } cc = cclass(c); } switch (cc) { case LETCC: do { p = p.append(c_str); getChStr(); cc = cclass(c); } while ( (cc == LETCC) || (cc == DIGCC) ); tokenlgth = p.length(); outtok = reschk_id(p,YYParser.Lexer.WORDID); scansavc = c; scansavc_str = c_str; token_cnt++; yycat = "ID"; if (outtok != YYParser.Lexer.WORDID) yydescr = "RESERVED ID"; else yydescr = "DEFINED ID"; rpt_progress(p,yycat,outtok,yydescr); return outtok; case SQUCC : // single quote // p = p.append(c_str); // getChStr(); // cc = cclass(c); saw_quote = false; while (true) { if ( cc == EOFCC || c == '\n' ) { yyperror("Unterminated quoted identifier"); break; } if (c == '\\') { getChStr(); switch (c) { case 't' : p.append("\t") ; break; case 'n' : p.append("\n"); break; case 'r' : p.append("\r"); break; default: p.append(c_str); } } else { p = p.append(c_str); } getChStr(); cc = cclass(c); if (saw_quote) { if (cc == SQUCC) { // ignore this character and keep scanning getChStr(); cc = cclass(c); } else { // end of identifier break; } } if (cc == SQUCC) saw_quote = true; else saw_quote = false; } outtok = YYParser.Lexer.WORDID; String tmp = p.toString(); yylval = Stt.stt_enter(tmp); //################################# scansavc = c; scansavc_str = c_str; token_cnt++; yycat = "Sing Quote"; yydescr = "Sing Quote"; sing_quoted_cnt++; rpt_progress(p,yycat,outtok,yydescr); return outtok; //################################### case DQUCC: // double quote // p.append(c_str); // getChStr(); // cc = cclass(c) saw_quote = false; while (true) { if (cc == EOFCC || c == '\n') { yyperror("Unterminated string"); break; } if (c == '\\') { getChStr(); switch (c) { case 't': p.append("\t"); break; case 'n': p.append("\n"); break; case 'r': p.append("\r"); default: p.append(c_str); } } else { p.append(c_str); } getChStr(); cc = cclass(c); if (saw_quote) { if (cc == DQUCC) { // ignore this character and keep scanning // Note that the previous double quote was saved getChStr(); cc = cclass(c); } else { // end of string break; } } } if (cc == DQUCC) saw_quote = true; else saw_quote = false; // Delete trailing quote. p.deleteCharAt(p.length()-1); outtok = QSTRING; // allocate a buffer for the string and return it yylval = str_chk(p); // ################################# scansavc = c; scansavc_str = c_str; token_cnt++; yycat = "Dbl Quote"; yydescr ="Dbl Quote"; dbl_quoted_cnt++; rpt_progress(p,yycat,outtok,yydescr); return outtok; // ################################### case SEPCC: do_retry = false; p = new StringBuffer(c_str); scansavc_str = c_str; scansavc = c; getChStr(); // added java if ((scansavc == '(') && (c == '*')) { comments_cnt++; //# process a comment startline = yyline; //# temporary line counter used in comments //# so error message has a useful line number //# if EOF occurs inside a comment startfnm = yyinfnm; cmtnest = 0; // BEGIN $include "pass1/scanner/scan_sep.inc.icn" do { // while switch ( scansavc ) // case { case EOF: yyline = startline; yyinfnm = startfnm; do_retry = true; // return an end of file // EOF break; case '\n': yyline++; yyvline++; if( c == ESCCHAR && Globals.pflag ) { rdposition(); c = '\n'; c_str = "\n"; // Repeat check for ESCCHAR next time around yyline--; yyvline--; } // Line number is correct for next line break; case '*': if( c == ')' ) { cmtnest--; } // * break; case '(': if( c == '*' ) { cmtnest++; getChStr(); } // break; case '$': if (c == '$') { getChStr(); switch ( c ) { case '+': Globals.yydebug++; break; case '-': if (Globals.yydebug > 0) Globals.yydebug--; break; } // case c } // c == break; } // case scansavc = c; scansavc_str = c_str; getChStr(); } while ( cmtnest > 0 ); if (do_retry) { continue; } // put2w( S_YYLINE, yyline ) scansavc = c; scansavc_str = c_str; continue; //# outtok = p ||= c } // # if comment outtok = p.charAt(0); // p.deleteCharAt(0); // # not sure -- was outtok = p++ // yyinfo("SEP p = " || p || " c = " || c || " outtok = " || outtok ) yycat = "SEP"; yydescr = p.toString(); scansavc_str = c_str; scansavc = c; token_cnt++; seperator_cnt++; rpt_progress(p,yycat,outtok,yydescr); p.deleteCharAt(0); return ( outtok ); case DIGCC: do { p.append(c_str); getChStr(); cc = cclass(c); } while( cc == DIGCC || cc == LETCC ); outtok = UQSTRING; // # unquoted string //#allocate buffer and return it, as for quoted strings yylval = str_chk(p); yycat = "NUM"; yydescr = p.toString(); scansavc = c; scansavc_str = c_str; token_cnt++; numeral_cnt++; rpt_progress(p,yycat,outtok,yydescr); return ( outtok ); case OPRCC: do { p.append(c_str); getChStr(); cc = cclass(c); } while ( cc == OPRCC ); tokenlgth = p.length(); outtok = reschk_op(p,OPID); scansavc = c; scansavc_str = c_str; token_cnt++; yycat = "OPR"; if (outtok != OPID) yydescr = "RESERVED OPR"; else yydescr = "DEFINED OPR"; rpt_progress(p,yycat,outtok,yydescr); return outtok; case EOFCC: scansavc = '\n'; // Set things up for core image to be subsequently scansavc_str = "\n"; // Set things up for core image to be subsequently // restarted rpt_progress(p,"EOF",EOF,"EOF"); return(EOF); case BADCC: getChStr(); scansavc = c; scansavc_str = c_str; continue; } } scansavc = c; scansavc_str = c_str; token_cnt++; rpt_progress(p,yycat,outtok,yydescr); return ( outtok ); } private static void rpt_progress(StringBuffer p,String yycat,int outtok,String yydescr) { Integer tmp = new Integer(outtok); String pt1 = p.toString(); String pt2 = yydescr; String pt3 = yycat; String pt4 = tmp.toString(); String pt5 = yylval.toString(); String tmp2 = Mine.prog_str(pt1,pt2,pt3,pt4,pt5); Mine.write_token_line(tmp2); } public static void yywarn(String str) { System.err.println("___ YYERR ___"); System.err.println(str); System.err.println("___ YYERR ___"); } public static void yyerror(String str) { System.err.println("___ YYERR ___"); System.err.println(str); System.err.println("___ YYERR ___"); } public static void yyperror(String str) { System.err.println("___ YYPERR ___"); System.err.println(str); System.err.println("___ YYPERR ___"); } private static void rdposition() { } private static void getChStr() { while (true) { if (current_line.length() > 0) { Mine.russinfo("getChStr A line = " + current_line); c_str = current_line.substring(0,1); c = c_str.charAt(0); current_line = current_line.delete(0,1); Mine.russinfo("getChStr A c_str = |" + c_str + "|"); return; } else { try { try { current_line = new StringBuffer(Globals.ifile.readLine()); Mine.russinfo("getChStr B line = " + current_line); c_str = current_line.substring(0,1); c = c_str.charAt(0); Mine.russinfo("getChStr B c_str = |" + c_str + "|"); current_line = current_line.delete(0,1); Mine.russinfo("getChStr 000005A" + c_str); return; } catch (IOException e) { c_str = "EOF"; c = EOF; Mine.russinfo("HELP001 !!!! getChStr C c_str = |" + c_str + "|"); return; } } catch (NullPointerException e) { c_str = "EOF"; c = EOF; Mine.russinfo("HELP002 !!!! getChStr D c_str = |" + c_str + "|"); return; } } } } }