summaryrefslogtreecommitdiff
path: root/src/PicoRec.java
blob: eec2f381b4a579469f43d434d1396f3e4c7619df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * Recognizer for the language described by grammar-ll1.txt.
 */
import dk.brics.automaton.RegExp;
import dk.brics.automaton.RunAutomaton;

public class PicoRec {

    /** Regular expression describing the ID symbol. */
    private final static String idRegex = "[a-z][a-z0-9]*";
    /** Regular expression describing the NAT symbol. */
    private final static String natRegex = "0|[1-9][0-9]*";

    private final RunAutomaton idR, natR;

    public PicoRec() {
        idR = new RunAutomaton(new RegExp(idRegex).toAutomaton());
        natR = new RunAutomaton(new RegExp(natRegex).toAutomaton());
    }

    /**
     * Try to parse the input.
     *
     * @return true if the language is recognized, false otherwise.
     */
    public boolean parse(String text) {
        // TODO implement this
        return false;
    }


    /* Convenience functions for testing. */

    private void check(String input, boolean expectOk) {
        boolean accepted = parse(input);
        System.out.println("Testing input: " + input);
        if (expectOk && !accepted || !expectOk && accepted) {
            throw new RuntimeException("Unexpected result for " + input);
        }
    }

    private void check(String[] input, boolean expectOk) {
        check(String.join("\n", input), expectOk);
    }

    private void assertOk(String... input) {
        check(input, true);
    }

    private void assertFail(String... input) {
        check(input, false);
    }

    public static void main(String[] args) {
        PicoRec rec = new PicoRec();

        // minimal program satisfying the language, without whitespace.
        rec.assertOk("begindeclare|end");
        // simple expression
        rec.assertOk("begin declare a | a := 1 end");

        // Trivial failures
        rec.assertFail("");
        rec.assertFail("begin");
        rec.assertFail("begin declare");
        rec.assertFail("begin declare |");
        rec.assertFail("begin declare | end end");
        rec.assertFail("begin declare end");
        rec.assertFail("begin end");
        rec.assertFail("end");
    }
}