test.g
上传用户:itx_2006
上传日期:2007-01-06
资源大小:493k
文件大小:1k
源码类别:

编译器/解释器

开发平台:

Others

  1. /* This is test.g which tests a simple DLG-based scanner
  2.  * with user-defined tokens
  3.  */
  4. /* Here, we use #tokdefs to define token types, but still let DLG do the
  5.  * lexing. ANTLR will not create a tokens.h file.
  6.  */
  7. #tokdefs "mytokens.h"
  8. <<
  9. #include "DLGLexer.h" /* include definition of DLGLexer.
  10.  * This cannot be generated automatically because
  11.  * ANTLR has no idea what you will call this file
  12.  * with the DLG command-line options.
  13.  */
  14. typedef ANTLRCommonToken ANTLRToken;
  15. int main()
  16. {
  17. ANTLRTokenPtr aToken = new ANTLRToken;
  18. DLGFileInput in(stdin);
  19. DLGLexer scan(&in);
  20. ANTLRTokenBuffer pipe(&scan);
  21. scan.setToken(mytoken(aToken));
  22. Expr parser(&pipe);
  23. parser.init();
  24. parser.e();
  25. return 0;
  26. }
  27. >>
  28. #token "[ tn]+" <<skip();>>
  29. class Expr { /* Define a grammar class */
  30. e : IDENTIFIER NUMBER "@"
  31. <<fprintf(stderr, "text is %s,%sn", $1->getText(), $2->getText());>>
  32. ;
  33. }
  34. #token IDENTIFIER "[a-z]+"
  35. #token NUMBER "[0-9]+"