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 | options { |
---|
7 | language=Python; |
---|
8 | } |
---|
9 | |
---|
10 | class transform_p extends Parser; |
---|
11 | options { |
---|
12 | buildAST = true; // uses CommonAST by default |
---|
13 | ASTLabelType = "antlr.CommonAST"; |
---|
14 | } |
---|
15 | |
---|
16 | expr |
---|
17 | : mexpr (PLUS^ mexpr)* SEMI! |
---|
18 | ; |
---|
19 | |
---|
20 | mexpr |
---|
21 | : atom (STAR^ atom)* |
---|
22 | ; |
---|
23 | |
---|
24 | atom: INT |
---|
25 | ; |
---|
26 | |
---|
27 | class transform_l extends Lexer; |
---|
28 | |
---|
29 | WS : (' ' |
---|
30 | | '\t' |
---|
31 | | '\n' |
---|
32 | | '\r') |
---|
33 | { $skip; } |
---|
34 | ; |
---|
35 | |
---|
36 | LPAREN: '(' |
---|
37 | ; |
---|
38 | |
---|
39 | RPAREN: ')' |
---|
40 | ; |
---|
41 | |
---|
42 | STAR: '*' |
---|
43 | ; |
---|
44 | |
---|
45 | PLUS: '+' |
---|
46 | ; |
---|
47 | |
---|
48 | SEMI: ';' |
---|
49 | ; |
---|
50 | |
---|
51 | protected |
---|
52 | DIGIT |
---|
53 | : '0'..'9' |
---|
54 | ; |
---|
55 | |
---|
56 | INT : (DIGIT)+ |
---|
57 | ; |
---|
58 | |
---|
59 | class transform_w extends TreeParser; |
---|
60 | options { |
---|
61 | buildAST = true; |
---|
62 | ASTLabelType = "antlr.CommonAST"; |
---|
63 | } |
---|
64 | |
---|
65 | expr:! #(PLUS left:expr right:expr) |
---|
66 | { |
---|
67 | if #right.getType()==INT and int(#right.getText()) == 0: |
---|
68 | #expr = #left |
---|
69 | elif #left.getType()==INT and int(#left.getText()) ==0: |
---|
70 | #expr = #right; |
---|
71 | else: |
---|
72 | #expr = #(PLUS, left, right); |
---|
73 | } |
---|
74 | | #(STAR expr expr) |
---|
75 | | i:INT |
---|
76 | ; |
---|
Note: See
TracBrowser
for help on using the repository browser.