package SimpleExample; import java_cup.runtime.*; // Some comments to help you out: // If you want to run this file through JLex perform the following 2 steps: // 1. setenv CLASSPATH .:..:/usr/local/lib/jar // 2. java JLex.Main e.lex // The resulting .java file will be called e.lex.java // // The directives in the following section have the following meanings: // 1. %cup lets JLex know that this will be used with java-cup // 2. %line tells JLex to use int yyline as a variable. With this on, // you'll be able to use the variable yyline to echo out the line number // 3. NUM, ID, and WhiteSpace are macros and are similar to #define // statements in C %% %cup %line NUM = [0-9] ID = [a-zA-Z] WhiteSpace = [ \t\r\n\f] %% // Any item put in the {} will create code for your Yylex.java file // See the last entry for a good example // Symbol(sym.***); - This adds the *** to your sym.class when you compile the // final executable. // Symbol allows you to declare the type of the variable as a 2nd arguement // for example, NUM, is declared to be a instance of class Integer "+" {return new Symbol(sym.ADD);} ";" {return new Symbol(sym.SEMI);} {NUM}+ {return new Symbol(sym.NUM, new Integer(yytext()));} {ID}+ {return new Symbol(sym.ID, new String(yytext()));} {WhiteSpace} { /* ignore white space. */ } . { System.err.println("Illegal Character: "+yytext()+" at line "+yyline);}