source: trunk/yao/share/antlr-2.7.7/examples/python/heteroAST/hetero.g @ 1

Last change on this file since 1 was 1, checked in by lnalod, 15 years ago

Initial import of YAO sources

File size: 1.0 KB
Line 
1// This file is part of PyANTLR. See LICENSE.txt for license
2// details..........Copyright (C) Wolfgang Haefelinger, 2004.
3//
4// $Id$
5
6header {
7    import hetero
8}
9
10options {
11    language=Python;
12}
13
14/** This example demonstrates the heterogeneous tree construction
15 *  mechanism.  Compare this example to examples/calc/calc.g
16 *  to see that I use tree node methods not a tree walker to compute
17 *  the result.
18 */
19class hetero_p extends Parser;
20options {
21        buildAST = true;        // uses CommonAST by default
22}
23
24// define a bunch of specific AST nodes to build.
25// can override at actual reference of tokens in grammar
26// below.
27tokens {
28        PLUS<AST=hetero.PLUSNode>;
29        STAR<AST=hetero.MULTNode>;
30}
31
32expr
33        :       mexpr (PLUS^ mexpr)* SEMI!
34        ;
35
36mexpr
37        :       atom (STAR^ atom)*
38        ;
39
40atom:   INT<AST=hetero.INTNode> // could have done in tokens{} section
41        ;
42
43class hetero_l extends Lexer;
44
45WS      :       (' '
46        |       '\t'
47        |       '\n'
48        |       '\r')
49                { _ttype = SKIP; }
50        ;
51
52LPAREN: '('
53        ;
54
55RPAREN: ')'
56        ;
57
58STAR:   '*'
59        ;
60
61PLUS:   '+'
62        ;
63
64SEMI:   ';'
65        ;
66
67protected
68DIGIT
69        :       '0'..'9'
70        ;
71
72INT     :       (DIGIT)+
73        ;
Note: See TracBrowser for help on using the repository browser.