Blame


1 2228b436 2024-02-10 benni // Declarations
2 2228b436 2024-02-10 benni %import eu.bandm.tools.lexic.*
3 2228b436 2024-02-10 benni %import java.io.IOException
4 2228b436 2024-02-10 benni %import java.io.InputStreamReader
5 2228b436 2024-02-10 benni %import java.nio.charset.StandardCharsets
6 2228b436 2024-02-10 benni
7 2228b436 2024-02-10 benni // Declare an end-of-file token called EOF.
8 2228b436 2024-02-10 benni %eof EOF
9 2228b436 2024-02-10 benni
10 2228b436 2024-02-10 benni // Declare an error token called Error.
11 2228b436 2024-02-10 benni %error Error
12 2228b436 2024-02-10 benni
13 2228b436 2024-02-10 benni Integer = "0" | [1-9][0-9]*;
14 2228b436 2024-02-10 benni Ident = [a-zA-Z_][a-zA-Z_0-9]*;
15 2228b436 2024-02-10 benni
16 2228b436 2024-02-10 benni %%
17 2228b436 2024-02-10 benni // Definitions
18 2228b436 2024-02-10 benni
19 2228b436 2024-02-10 benni Whitespace : [ \n\t\r\f]+;
20 2228b436 2024-02-10 benni // A File can be either an integer, or an identifier.
21 2228b436 2024-02-10 benni File : Integer | Ident;
22 2228b436 2024-02-10 benni
23 2228b436 2024-02-10 benni %%
24 2228b436 2024-02-10 benni // Code
25 2228b436 2024-02-10 benni
26 2228b436 2024-02-10 benni public static void main(String[] args) {
27 2228b436 2024-02-10 benni // The construct() function is generated by lexicgen and returns a Lexer.
28 2228b436 2024-02-10 benni var lexer = construct();
29 2228b436 2024-02-10 benni
30 2228b436 2024-02-10 benni try (var rdr = new InputStreamReader(System.in, StandardCharsets.UTF_8)) {
31 2228b436 2024-02-10 benni TokenSource<String, TokenType> tokens = lexer
32 2228b436 2024-02-10 benni .lex(CodePointSource.read(rdr, e -> {}))
33 2228b436 2024-02-10 benni .removeTypes(TokenType.Whitespace);
34 2228b436 2024-02-10 benni while (true) {
35 2228b436 2024-02-10 benni var token = tokens.get();
36 2228b436 2024-02-10 benni if (token.getType() == TokenType.EOF)
37 2228b436 2024-02-10 benni break;
38 2228b436 2024-02-10 benni System.out.println(token);
39 2228b436 2024-02-10 benni }
40 2228b436 2024-02-10 benni } catch (IOException e) {}
41 2228b436 2024-02-10 benni }