// This file contains a complete JLex specification for a very small // example. // User Code section: For right now, we will not use it. %% // JLex Directives section: // Macro definitions DIGIT= [0-9] LETTER= [a-zA-Z] WHITESPACE= [ \t\n] // space, tab, newline // State declarations: // The following state is used to identify when we are reading in // an integer literal specified in octal or hexadecimal format. // In both cases, the integer literal starts with the number 0 // (zero): octal literals look like regular integer literals but // start with a 0 (zero), e.g. 0123; hexadecimal literals start // with "0x" followed by hexadecimal digits %state SPECIALINTSTATE // The next 3 lines are included so that we can use the generated scanner // with java CUP (the Java parser generator) %implements java_cup.runtime.Scanner %function next_token %type java_cup.runtime.Symbol // Tell JLex what to do on end-of-file %eofval{ System.out.println("All done"); return null; %eofval} // Turn on line counting %line // Below the %% are the Regular Expression Rules // Note that the rules for identifying an octal literal and a hexadecimal // literal are not quite correct: as written, 0989 is recognized as an octal // literal (when it shouldn't be) and 0x12a8b is not recognized as a // hexadecimal literal (when it should be). It is left as an exercise to // fix the rules so that octal and hexadecimal literals are appropriately // recognized. %% 0 { yybegin(SPECIALINTSTATE); System.out.println("Entering special state"); } {DIGIT}+ { yybegin(YYINITIAL); System.out.println(yyline+1 + ": OCTALLIT (" + yytext() + ")"); System.out.println("Leaving special int state"); } x{DIGIT}+ { yybegin(YYINITIAL); System.out.println(yyline+1 + ": HEXLIT (" + yytext() + ")"); System.out.println("Leaving special int state"); } . { System.out.println(yyline+1 + ": bad char in special int state"); } [1-9]{DIGIT}* { System.out.println(yyline+1 + ": INTLIT (" + yytext() + ")"); } ({LETTER}|("_"+({LETTER}|{DIGIT})))({LETTER}|{DIGIT}|"_")* { System.out.println(yyline+1 + ": ID " + yytext()); } "=" { System.out.println(yyline+1 + ": ASSIGN"); } "+" { System.out.println(yyline+1 + ": PLUS"); } "^" { System.out.println(yyline+1 + ": EXP"); } "<" { System.out.println(yyline+1 + ": LESSTHAN"); } "+=" { System.out.println(yyline+1 + ": INCR"); } "<=" { System.out.println(yyline+1 + ": LEQ"); } {WHITESPACE}* { } . { System.out.println(yyline+1 + ": bad char"); }