/* User code */ import java.io.*; // For FileInputStream and its exceptions. /* ========================================================================= */ class Token { public String type; public String spelling; public Token(String t) { type = t; } public Token(String t, String s) { type = t; spelling = s; } public String toString() { if (spelling != null) return "\"" + spelling + "\""; else return type; } } /* ========================================================================= */ class P2 { public static void main(String[] args) throws FileNotFoundException, IOException { FileInputStream fis = new FileInputStream(args[0]); Lexer L = new Lexer(fis); Token T = null; do { T = L.next(); System.out.println(T); } while (T.type != "Eof"); } } /* ========================================================================= */ %% /* JLex directives */ %class Lexer %function next %type Token %eofval{ return new Token("Eof"); %eofval} ALPHA = [A-Za-z] DIGIT = [0-9] WORD = {ALPHA}({ALPHA}|{DIGIT}|_)* SPACE = [\ \t\b\r\n] COMMENT1 = [\/][\*]([^\*]|[\*][^\/])*[\*][\/] COMMENT2 = [\/][\/].* COMMENT = ({COMMENT1}|{COMMENT2}) %% {SPACE} { } {COMMENT} { } class { return new Token("Class"); } int { return new Token("Int"); } boolean { return new Token("Boolean"); } static { return new Token("Static"); } true { return new Token("Bool", yytext()); } {WORD} { return new Token("Id", yytext()); } {DIGIT}+ { return new Token("Num", yytext()); } "(" { return new Token("L-paren"); } ")" { return new Token("R-paren"); } "{" { return new Token("L-brace"); } "}" { return new Token("R-brace"); } . { return new Token("Err"); }