// jBASIC lexer, JFlex version import java_cup.runtime.*; %% %class Lexer %unicode %cup %line %column //%char //%type java_cup.runtime.Symbol //%function next_token %eofval{ {return symbol(sym.EOF,null); } %eofval} /* */ %{ /* copied from JFlex example */ StringBuffer string = new StringBuffer(); int mylinenum = 1; private java_cup.runtime.Symbol symbol(int type) { return new java_cup.runtime.Symbol(type, yyline, yycolumn); } private java_cup.runtime.Symbol symbol(int type, Object value) { return new java_cup.runtime.Symbol(type, yyline, yycolumn, value); } %} LineTerminator = \r|\n|\r\n InputCharacter = [^\r\n] WhiteSpace = {LineTerminator} | [ \t\f] /* comments */ Comment = {TraditionalComment} | {EndOfLineComment} | {DocumentationComment} TraditionalComment = "/*" [^*] ~"*/" | "/*" "*"+ "/" EndOfLineComment = "//" {InputCharacter}* {LineTerminator} DocumentationComment = "/**" {CommentContent} "*"+ "/" CommentContent = ( [^*] | \*+ [^/*] )* Identifier = [:jletter:] [:jletterdigit:]* DecIntegerLiteral = 0 | [1-9][0-9]* %state STRING %% /* keywords */ /* {NONNEWLINE_WHITE_SPACE}+ {} \"{STRING_TEXT}\" { // System.out.println("I found a string: " + changestring(yytext().substring(1,yytext().length() - 1))); return tok(sym.STRINGe, changestring(yytext().substring(1,yytext().length() - 1))); } */ /*{LineTerminator} { System.err.println("line "+(mylinenum++)); } */ ";" { return symbol(sym.SEMI, null); } goto { return symbol(sym.GOTO, null); } and { return symbol(sym.ANDop, null); } label { return symbol(sym.LABEL, null); } "(" { return symbol(sym.LPAREN, null); } ")" { return symbol(sym.RPAREN, null); } ":=" { return symbol(sym.ASSIGN, null); } "=" { return symbol(sym.EQUAL, null); } true { return symbol(sym.TRUE, null); } false { return symbol(sym.FALSE, null); } if { return symbol(sym.IF, null); } "+" { return symbol(sym.PLUS, null); } "*" { return symbol(sym.TIMES, null); } then { return symbol(sym.THEN, null); } not { return symbol(sym.NOT, null); } print { // System.out.println("I found a print token!"); return symbol(sym.PRINT, null); } while { return symbol(sym.WHILE, null); } begin { return symbol(sym.BEGIN, null); } end { return symbol(sym.END, null); } { {Identifier} { return symbol(sym.ID, yytext()); } {DecIntegerLiteral} { return symbol(sym.NUM,new Integer(yytext())); } {Comment} { /* ignore*/ } {WhiteSpace} {/* ignore */} } \" { string.setLength(0); yybegin(STRING); } /*{DIGITS} { return symbol(sym.NUM, new Integer(yytext())); } [a-zA-Z]([a-zA-Z0-9]*) { return symbol(sym.ID, yytext()); } */ { \" { yybegin(YYINITIAL); return symbol(sym.STRINGe, 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 */ [^] { throw new Error("Illegal character <"+ yytext()+">"); }