1 /* This is test.g which tests a simple DLG-based scanner.
2 * No garbage collection for tokens since our token can't
6 /* ANTLR will assign token type numbers (by creating an enum which you
11 /* user must define ANTLRToken */
12 class ANTLRToken : public ANTLRAbstractToken {
14 ANTLRTokenType _type; // what's the token type of the token object
15 int _line; // track line info for errors
17 /* For our simple purposes, a token and a string is enough for
23 ANTLRToken(ANTLRTokenType t, ANTLRChar *s)
24 { setType(t); _line = 0; setText(s); }
26 /* Your derived class MUST have a blank constructor. */
28 { setType((ANTLRTokenType)0); _line = 0; setText(""); }
30 // how to access the token type and line number stuff
31 ANTLRTokenType getType() { return _type; }
32 void setType(ANTLRTokenType t) { _type = t; }
33 virtual int getLine() { return _line; }
34 void setLine(int line) { _line = line; }
36 ANTLRChar *getText() { return _text; }
37 void setText(ANTLRChar *s) { strncpy(_text, s, 30); }
39 /* WARNING WARNING WARNING: you must return a stream of distinct tokens */
40 /* This function will disappear when I can use templates */
41 virtual ANTLRAbstractToken *makeToken(ANTLRTokenType tt,
45 ANTLRAbstractToken *t = new ANTLRToken(tt,txt);
51 /* "DLGLexer" must match what you use on DLG command line (-cl);
52 * "DLGLexer" is the default.
54 #include "DLGLexer.h" /* include definition of DLGLexer.
55 * This cannot be generated automatically because
56 * ANTLR has no idea what you will call this file
57 * with the DLG command-line options.
62 DLGFileInput in(stdin); /* create input stream for DLG to get chars from */
63 DLGLexer scan(&in); /* create scanner reading from stdin */
64 ANTLRTokenBuffer pipe(&scan);/* make buffered pipe between lexer&parser */
65 ANTLRTokenPtr aToken=new ANTLRToken; // create a token to fill in for DLG
66 scan.setToken(mytoken(aToken));
67 Expr parser(&pipe); /* create parser of type Expr hooked to scanner */
68 parser.init(); /* init the parser; prime lookahead etc... */
70 parser.e(); /* start parsing at rule 'e' of that parser */
75 #token "[\ \t\n]+" <<skip();>>
78 #tokclass My { IDENTIFIER NUMBER }
80 class Expr { /* Define a grammar class */
83 <<fprintf(stderr, "text is %s,%s\n", $1->getText(), $2->getText());>>
88 #token IDENTIFIER "[a-z]+"
89 #token NUMBER "[0-9]+"