1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
/* This is test.g which tests simple AST refs and construction */
/* Revised 2002-2-15: preorder_action prototype no longer
matched prototype of base class. (Eero Ivask,
Tallinn Technical University, Estonia).
*/
<<
typedef ANTLRCommonToken ANTLRToken;
#include "DLGLexer.h"
#include "PBlackBox.h"
class AST : public ASTBase {
public:
ANTLRTokenPtr token;
AST(ANTLRTokenPtr t) { token = t; }
void preorder_action(void * clientData) {
char *s = token->getText();
printf(" %s", s);
}
};
int main()
{
ParserBlackBox<DLGLexer, Expr, ANTLRToken> p(stdin);
ASTBase *root = NULL;
p.parser()->e(&root);
root->preorder();
printf("\n");
root->destroy();
return 0;
}
>>
#token "[\ \t\n]+" <<skip();>>
#token Eof "@"
class Expr { /* Define a grammar class */
e : mult_expr ( ("\+"^|"\-"^) mult_expr )*
;
mult_expr
: atom ( ("\*"^|"\/"^) atom )*
;
atom: IDENTIFIER
| NUMBER
;
}
#token IDENTIFIER "[a-z]+"
#token NUMBER "[0-9]+"
|