source: trunk/yao/share/antlr-2.7.7/examples/python/treewalk/treewalk.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: 2.1 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
6options {
7    language=Python;
8}
9
10class treewalk_p extends Parser;
11
12options {
13    codeGenMakeSwitchThreshold = 3;
14    codeGenBitsetTestThreshold = 4;
15    buildAST=true;
16}
17
18block
19    :   LCURLY^ ( statement )* RCURLY!
20    ;
21
22statement
23    :   expr SEMI!
24    |   "if"^ LPAREN! expr RPAREN! statement
25        ( "else"! statement )?
26    |   "while"^ LPAREN! expr RPAREN! statement
27    |!  b:block { statement_AST = b_AST; }
28        // do some manual tree returning
29    ;
30
31expr:   assignExpr
32    ;
33
34assignExpr
35    :   aexpr (ASSIGN^ assignExpr)?
36    ;
37
38aexpr
39    :   mexpr (PLUS^ mexpr)*
40    ;
41
42mexpr
43    :   atom (STAR^ atom)*
44    ;
45
46atom:   ID
47    |   INT
48    ;
49
50class treewalk_w extends TreeParser;
51
52block
53    :   #( LCURLY ( stat )+ )
54    ;
55
56stat:   #("if" expr stat (stat)?)
57    |   #("while" expr stat)
58    |   expr
59    |   block
60    ;
61
62expr:   #(ASSIGN expr expr)     {print "found assign"           }
63    |   #(PLUS expr expr)       {print "found +"                }
64    |   #(STAR expr expr)       {print "found *"                }
65    |   a:ID                    {print "found ID " ,a.getText() }
66    |   b:INT                   {print "found INT ",b.getText() }
67    ;
68
69class treewalk_l extends Lexer;
70
71WS  :   (' '
72    |   '\t'
73    |   '\n'
74    |   '\r')
75        { _ttype = SKIP; }
76    ;
77
78LPAREN: '('
79    ;
80
81RPAREN: ')'
82    ;
83
84LCURLY: '{'
85    ;
86
87RCURLY: '}'
88    ;
89
90STAR:   '*'
91    ;
92
93PLUS:   '+'
94    ;
95
96ASSIGN
97    :   '='
98    ;
99
100SEMI:   ';'
101    ;
102
103COMMA
104    :   ','
105    ;
106
107protected
108ESC :   '\\'
109        (   'n'
110        |   'r'
111        |   't'
112        |   'b'
113        |   'f'
114        |   '"'
115        |   '\''
116        |   '\\'
117        |   ('0'..'3') ( DIGIT (DIGIT)? )?
118        |   ('4'..'7') (DIGIT)?
119        )
120    ;
121
122protected
123DIGIT
124    :   '0'..'9'
125    ;
126
127INT :   (DIGIT)+
128    ;
129
130ID  :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
131        {
132            try:
133              i = literals[self.getText()]
134              _ttype =  i;
135            except:
136              pass
137        }
138    ;
Note: See TracBrowser for help on using the repository browser.