Help us understand the problem. What is going on with this article?

決定性オートマトン写経

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);
    }
}

kametaro
今年でエンジニア生活22年目 COBOLer | SIer | fujisan.co.jp | dely.jp | retail-ai.jp url.joor.net/cke url.joor.net/ckf App/サーバサイド/Cloud データエンジニア/機械学習 数学好き
https://note.com/long10
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away