]> pd.if.org Git - pccts/commitdiff
auto commit for import
authorTerence Parr <>
Tue, 12 Sep 1995 00:13:02 +0000 (19:13 -0500)
committerNathan Wagner <nw@hydaspes.if.org>
Sun, 26 Feb 2017 02:16:51 +0000 (20:16 -0600)
testcpp/13/test.g [new file with mode: 0755]

diff --git a/testcpp/13/test.g b/testcpp/13/test.g
new file mode 100755 (executable)
index 0000000..eaffc4f
--- /dev/null
@@ -0,0 +1,77 @@
+/* C++ interface test of Parser Exception Handling
+ *
+ * Given input:
+ *
+ *             if a+ then a=b+b;
+ *
+ * the program should respond with
+ *
+ *             invalid conditional in 'if' statement
+ *             found assignment to a
+ */
+<<
+#include <stream.h>
+#include "DLGLexer.h"
+#include "PBlackBox.h"
+typedef ANTLRCommonToken ANTLRToken;
+
+int main()
+{
+       ParserBlackBox<DLGLexer, PEHTest, ANTLRToken> p(stdin);
+       int retsignal;
+       p.parser()->rule(&retsignal);
+       return 0;
+}
+>>
+
+/*
+Uncommenting this will make ANTLR think you put these handlers at the
+end of each rule:
+exception
+       catch MismatchedToken   : <<printf("dflt:MismatchedToken\n");>>
+       default : <<printf("dflt:dflt\n");>>
+*/
+
+#token "[\ \t]+"       <<skip();>>
+#token "\n"                    <<skip(); newline();>>
+#token THEN    "then"
+#tokclass DIE { "@" "if" ID "else" }
+
+class PEHTest {
+
+rule:  ( stat )+
+       ;
+
+stat:  "if" t:expr THEN stat { "else" stat }
+       |       id:ID "=" expr ";"
+               <<printf("found assignment to %s\n", $id->getText());>>
+       ;
+       exception[t]
+               default :
+                       <<
+                       printf("invalid conditional in 'if' statement\n");
+                       consumeUntilToken(THEN);
+                       >>
+       exception
+               catch MismatchedToken   :
+               catch NoViableAlt               :
+               catch NoSemViableAlt    :
+                       <<
+                       printf("stat:caught predefined signal\n");
+                       consumeUntil(DIE_set);
+                       >>
+
+expr:  expr1 ("\+" expr1)*
+       ;
+
+expr1
+       :       expr2 ("\*" expr2)*
+       ;
+
+expr2: ID
+       ;
+
+}
+
+#token ID "[a-z]+"