Edited at

決定性オートマトン写経

More than 5 years have passed since last update.

    \s*([0-9][0-9]*|[A-Za-z][A-Za-z0-9]*|=|==)

0個以上の空白に続き、整数リテラル、または、英字、または英字と数字、または「=」か、「==」と一致する


Lexer.java

package chapA;

import stone.CodeDialog;

import java.io.IOException;
import java.io.Reader;

public class Lexer {
private Reader reader;

private static final int EMPTY = -1;
private int lastChar = EMPTY;

public Lexer(Reader r) { reader = r; }

private int getChar() throws IOException {
if (lastChar == EMPTY)
return reader.read();
else {
int c = lastChar;
lastChar = EMPTY;
return c;
}
}

private void ungetChar(int c) { lastChar = c; }
public String read() throws IOException {
StringBuilder sb = new StringBuilder();
int c;

do {
c = getChar();
} while (isSpace(c));

if (c < 0)
return null;
else if (isDigit(c)) {
do {
sb.append((char)c);
c = getChar();
} while (isDigit(c));
}
else if (isLetter(c)) {
do {
sb.append((char)c);
c = getChar();
} while (isLetter(c) || isDigit(c));
}
else if (c == '=') {
c = getChar();
if (c == '=')
return "==";
else {
ungetChar(c);
return "=";
}
}
else
throw new IOException();

if (c >= 0)
ungetChar(c);

return sb.toString();
}

private static boolean isLetter(int c) {
return 'A' <= c && c <= 'Z' || 'a' <= c && c <= 'z';
}

private static boolean isDigit(int c) { return '0' <= c && c <= '9'; }
private static boolean isSpace(int c) { return 0 <= c && c <= ' '; }

public static void main(String[] args) throws Exception {
Lexer l = new Lexer(new CodeDialog());
for (String s; (s = l.read()) != null; )
System.out.println("-> " + s);
}
}