%%
%class Lexer
%int
L = [A-Za-z_]
D = [0-9]
Ident = {L}({L}|{D})*
Iconst = [-+]?{D}+
String = \"(\\\"|[^\"])*\"
Blank = [ \t\n]+
%%
{Blank} { /* ignore */ }
while { return 4; }
{Ident} { return 1; }
{Iconst} { return 2; }
{String} { return 3; }
import java.util.*;
import java.io.*;
public class Sam51 {
public static void main(String[] args) throws Exception {
Lexer lex = new Lexer(new InputStreamReader(System.in));
while(true) {
int tok = lex.yylex();
if(tok == Lexer.YYEOF) { break; }
System.out.printf("%d %s\n", tok, lex.yytext());
}
}
}
import java.util.*;
import java.io.*;
public class Sam52 {
public static void main(String[] args) throws Exception {
Lexer lex = new Lexer(new InputStreamReader(System.in));
while(true) {
int tok = lex.yylex();
if(tok == Lexer.YYEOF) { break; }
System.out.printf("%d %s\n", tok, lex.yytext());
}
}
}
class Lexer {
public static final int YYEOF = -1;
Reader rd;
int nc;
String text = "";
HashMap<String,Integer> map = new HashMap<String,Integer>();
public Lexer(Reader r) throws Exception {
rd = r; nc = rd.read(); map.put("while", 4);
}
public int yylex() {
try {
while(nc == ' ' || nc == '\t' || nc == '\n') { nc = rd.read(); }
text = "";
if(nc == YYEOF) {
// do nothing
} else if(alpha(nc)) {
text += (char)nc; nc = rd.read();
while(alpha(nc) || digit(nc)) { text += (char)nc; nc = rd.read(); }
if(map.containsKey(text)) { return map.get(text); }
return 1;
} else if(sign(nc) || digit(nc)) {
text += (char)nc; nc = rd.read();
while(digit(nc)) { text += (char)nc; nc = rd.read(); }
return 2;
} else {
System.err.printf("%x: invalid char\n", nc);
nc = rd.read(); return yylex();
}
} catch(IOException ex) { }
nc = YYEOF; return YYEOF;
}
public String yytext() { return text; }
private boolean alpha(int c) {
return c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z';
}
private boolean digit(int c) { return c >= '0' && c <= '9'; }
private boolean sign(int c) { return c == '+' || c == '-'; }
}