test.g
资源名称:pccts133.zip [点击查看]
上传用户:itx_2006
上传日期:2007-01-06
资源大小:493k
文件大小:1k
源码类别:
编译器/解释器
开发平台:
Others
- /* This is test.g which tests multiple scanners/parsers; DLG-based scanner;
- * also, we test multiple lexical classes.
- */
- <<
- #include "Lexer.h"
- typedef ANTLRCommonToken ANTLRToken;
- #include "PBlackBox.h"
- int main()
- {
- ParserBlackBox<Lexer, Include, ANTLRToken> p(stdin);
- p.parser()->input();
- return 0;
- }
- >>
- #token "[ tn]+" <<skip();>>
- #lexclass START
- class Include {
- <<
- /* this is automatically defined to be a member function of Include::
- * since it is within the "class {...}" boundaries.
- */
- private:
- char *stripquotes(ANTLRChar *s)
- {
- s[strlen(s)-1] = ' ';
- return &s[1];
- }
- >>
- input
- : ( cmd | include )* "@"
- ;
- cmd : "print"
- ( NUMBER <<printf("%sn", $1->getText());>>
- | STRING <<printf("%sn", $1->getText());>>
- )
- ;
- include
- : "#data" STRING
- <<{
- FILE *f;
- f = fopen(stripquotes($2->getText()), "r");
- if ( f==NULL ) {fprintf(stderr, "can't open %sn", $2->getText()+1);}
- else {
- ANTLRTokenPtr aToken = new ANTLRToken;
- DLGFileInput in(f);
- Lexer scan(&in);
- scan.setToken(mytoken(aToken));
- scan.mode(Lexer::DATA);
- ANTLRTokenBuffer pipe(&scan);
- Include parser(&pipe);
- parser.init();
- parser.data();
- }
- }>>
- ;
- #lexclass DATA
- #token "[ tn]+" <<skip();>>
- data: "0x[0-9]+" ":" "0x[0-9]+"
- <<printf("data %sn", $1->getText());>>
- ;
- }
- #lexclass START
- #token STRING "" [a-zA-Z0-9_., t]+ ""
- #token NUMBER "[0-9]+"