/** * 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"); } }