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 | |
---|
6 | header { |
---|
7 | import hetero |
---|
8 | } |
---|
9 | |
---|
10 | options { |
---|
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 | */ |
---|
19 | class hetero_p extends Parser; |
---|
20 | options { |
---|
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. |
---|
27 | tokens { |
---|
28 | PLUS<AST=hetero.PLUSNode>; |
---|
29 | STAR<AST=hetero.MULTNode>; |
---|
30 | } |
---|
31 | |
---|
32 | expr |
---|
33 | : mexpr (PLUS^ mexpr)* SEMI! |
---|
34 | ; |
---|
35 | |
---|
36 | mexpr |
---|
37 | : atom (STAR^ atom)* |
---|
38 | ; |
---|
39 | |
---|
40 | atom: INT<AST=hetero.INTNode> // could have done in tokens{} section |
---|
41 | ; |
---|
42 | |
---|
43 | class hetero_l extends Lexer; |
---|
44 | |
---|
45 | WS : (' ' |
---|
46 | | '\t' |
---|
47 | | '\n' |
---|
48 | | '\r') |
---|
49 | { _ttype = SKIP; } |
---|
50 | ; |
---|
51 | |
---|
52 | LPAREN: '(' |
---|
53 | ; |
---|
54 | |
---|
55 | RPAREN: ')' |
---|
56 | ; |
---|
57 | |
---|
58 | STAR: '*' |
---|
59 | ; |
---|
60 | |
---|
61 | PLUS: '+' |
---|
62 | ; |
---|
63 | |
---|
64 | SEMI: ';' |
---|
65 | ; |
---|
66 | |
---|
67 | protected |
---|
68 | DIGIT |
---|
69 | : '0'..'9' |
---|
70 | ; |
---|
71 | |
---|
72 | INT : (DIGIT)+ |
---|
73 | ; |
---|
Note: See
TracBrowser
for help on using the repository browser.