Search in sources :

Example 1 with Lexer

use of priv.bajdcc.util.lexer.Lexer in project jMiniLang by bajdcc.

the class TestLexer method main.

public static void main(String[] args) {
    try {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.nextLine();
        scanner.close();
        Lexer lexer = new Lexer(str);
        Token token;
        for (; ; ) {
            token = lexer.scan();
            if (token.kToken == TokenType.EOF) {
                break;
            }
            System.out.println(token.toString());
        }
    } catch (RegexException e) {
        System.err.println(e.getPosition() + "," + e.getMessage());
        e.printStackTrace();
    }
}
Also used : Scanner(java.util.Scanner) Lexer(priv.bajdcc.util.lexer.Lexer) RegexException(priv.bajdcc.util.lexer.error.RegexException) Token(priv.bajdcc.util.lexer.token.Token)

Example 2 with Lexer

use of priv.bajdcc.util.lexer.Lexer in project jMiniLang by bajdcc.

the class Main method main.

public static void main(String[] args) {
    String filename = "E:/vmm.cpp";
    try {
        // 读文件
        BufferedReader br = new BufferedReader(new FileReader(filename));
        String line;
        StringBuilder sb = new StringBuilder();
        while ((line = br.readLine()) != null) {
            sb.append(line).append(System.lineSeparator());
        }
        br.close();
        String context = sb.toString();
        // 词法分析
        Lexer lexer = new Lexer(context);
        lexer.discard(TokenType.COMMENT);
        lexer.discard(TokenType.WHITESPACE);
        lexer.discard(TokenType.ERROR);
        Token token;
        PrintStream ps = new PrintStream(new FileOutputStream("E:\\output.txt"));
        for (; ; ) {
            token = lexer.scan();
            if (token == null) {
                continue;
            }
            if (token.kToken == TokenType.EOF) {
                break;
            }
            ps.println(token.toString());
            ps.flush();
        }
        ps.close();
    } catch (Exception e) {
        System.err.print(e.getMessage());
        e.printStackTrace();
    }
}
Also used : PrintStream(java.io.PrintStream) Lexer(priv.bajdcc.util.lexer.Lexer) FileOutputStream(java.io.FileOutputStream) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) Token(priv.bajdcc.util.lexer.token.Token)

Aggregations

Lexer (priv.bajdcc.util.lexer.Lexer)2 Token (priv.bajdcc.util.lexer.token.Token)2 BufferedReader (java.io.BufferedReader)1 FileOutputStream (java.io.FileOutputStream)1 FileReader (java.io.FileReader)1 PrintStream (java.io.PrintStream)1 Scanner (java.util.Scanner)1 RegexException (priv.bajdcc.util.lexer.error.RegexException)1