/* JFlex example: part of Java language lexer specification */ import java_cup.runtime.*; /** * This class is a simple example lexer. */ %% %public %class Lexer %extends Sym %unicode %debug %line %column %cup %{ StringBuffer string = new StringBuffer(); Sym sym; private Symbol symbol(int type) { return new Symbol(type, yyline, yycolumn); } private Symbol symbol(int type, Object value) { return new Symbol(type, yyline, yycolumn, value); } %} %eof{ /* your code goes here */ %eof} LineTerminator = \r|\n|\r\n InputCharacter = [^\r\n] WhiteSpace = {LineTerminator} | [ \t\f] EndOfLineComment = "//" {InputCharacter}* {LineTerminator} Identifier = [:jletter:] [:jletterdigit:]* DecIntegerLiteral = 0 | [1-9][0-9]* %state STRING %state NESTED_COMMENT %% /* keywords */ "class" { return symbol(sym.CLASS); } "public" { return symbol(sym.PUBLIC); } { /* identifiers */ {Identifier} { return symbol(sym.IDENTIFIER); } /* literals */ {DecIntegerLiteral} { return symbol(sym.INTEGER_LITERAL); } \" { string.setLength(0); yybegin(STRING); } /* operators */ "=" { return symbol(sym.EQ); } /* comments */ {EndOfLineComment} { /* ignore */ } "/*" { /* your code goes here */ } /* whitespace */ {WhiteSpace} { /* ignore */ } } { \" { yybegin(YYINITIAL); return symbol(sym.STRING_LITERAL, string.toString()); } [^\n\r\"\\]+ { string.append( yytext() ); } \\t { string.append('\t'); } \\n { string.append('\n'); } \\r { string.append('\r'); } \\\" { string.append('\"'); } \\ { string.append('\\'); } } /* error fallback */ .|\n { throw new Error("Illegal character <"+ yytext()+">"); }