test.g
资源名称:pccts133.zip [点击查看]
上传用户:itx_2006
上传日期:2007-01-06
资源大小:493k
文件大小:1k
源码类别:
编译器/解释器
开发平台:
Others
- /* 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:MismatchedTokenn");>>
- default : <<printf("dflt:dfltn");>>
- */
- #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 %sn", $id->getText());>>
- ;
- exception[t]
- default :
- <<
- printf("invalid conditional in 'if' statementn");
- consumeUntilToken(THEN);
- suppressSignal;
- >>
- exception
- catch MismatchedToken :
- catch NoViableAlt :
- catch NoSemViableAlt :
- <<
- printf("stat:caught predefined signaln");
- consumeUntil(DIE_set);
- suppressSignal;
- >>
- expr: expr1 ("+" expr1)*
- ;
- expr1
- : expr2 ("*" expr2)*
- ;
- expr2: ID
- ;
- }
- #token ID "[a-z]+"