~ubuntu-branches/ubuntu/breezy/antlr/breezy

« back to all changes in this revision

Viewing changes to examples/python/exprAST/expr.g

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2005-06-29 16:11:22 UTC
  • mfrom: (0.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20050629161122-g81crc3z92p5xhsg
Tags: 2.7.5-6ubuntu4
Build depend on java-gcj-compat-dev, depend on java-gcj-compat.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 expr_p extends Parser;
 
11
 
 
12
options {
 
13
    codeGenMakeSwitchThreshold = 3;
 
14
    codeGenBitsetTestThreshold = 4;
 
15
    buildAST=true;
 
16
    ASTLabelType = "antlr.CommonAST"; // change default of "AST"
 
17
}
 
18
 
 
19
expr : assignExpr EOF! ;
 
20
 
 
21
assignExpr
 
22
    :   addExpr
 
23
        (
 
24
            ASSIGN^
 
25
            assignExpr 
 
26
        )?
 
27
    ;
 
28
 
 
29
addExpr
 
30
    :   multExpr 
 
31
        (
 
32
            pm:PLUS_MINUS^
 
33
            me:multExpr
 
34
            exception 
 
35
                catch [ antlr.RecognitionException,ex ] 
 
36
                { 
 
37
                    print "Caught error in addExpr"
 
38
                    self.reportError(ex) 
 
39
                }
 
40
        )*
 
41
    ;
 
42
 
 
43
multExpr
 
44
    :   postfixExpr
 
45
        (
 
46
            MULT_DIV^
 
47
            postfixExpr
 
48
        )*
 
49
    ;
 
50
 
 
51
postfixExpr
 
52
    :   (id:ID LPAREN)=>
 
53
        // Matches function call syntax like "id(arg,arg)" 
 
54
        id2:ID^
 
55
        (
 
56
         parenArgs
 
57
        )?
 
58
    |   atom
 
59
    ;
 
60
 
 
61
parenArgs
 
62
    :   
 
63
      LPAREN!
 
64
      (
 
65
         assignExpr
 
66
         (
 
67
            COMMA!
 
68
            assignExpr
 
69
         )*
 
70
      )?
 
71
      RPAREN!
 
72
    ;
 
73
 
 
74
atom
 
75
    :   ID
 
76
    |   INT
 
77
    |   CHAR_LITERAL 
 
78
    |   STRING_LITERAL
 
79
    |   LPAREN! assignExpr RPAREN!
 
80
    ;
 
81
 
 
82
class expr_l extends Lexer;
 
83
 
 
84
WS  :   (' '
 
85
    |   '\t'
 
86
    |   '\n'
 
87
    |   '\r')
 
88
        { _ttype = SKIP; }
 
89
    ;
 
90
 
 
91
LPAREN: '('
 
92
    ;
 
93
 
 
94
RPAREN: ')'
 
95
    ;
 
96
 
 
97
PLUS_MINUS: '+' | '-'
 
98
    ;
 
99
 
 
100
MULT_DIV : '*' | '/'
 
101
   ;
 
102
 
 
103
ASSIGN :    '='
 
104
    ;
 
105
 
 
106
COMMA : ','
 
107
   ;
 
108
   
 
109
CHAR_LITERAL
 
110
    :   '\'' (ESC|~'\'') '\''
 
111
    ;
 
112
 
 
113
STRING_LITERAL
 
114
    :   '"' (ESC|~'"')* '"'
 
115
    ;
 
116
 
 
117
protected
 
118
ESC :   '\\'
 
119
        (   'n'
 
120
        |   'r'
 
121
        |   't'
 
122
        |   'b'
 
123
        |   'f'
 
124
        |   '"'
 
125
        |   '\''
 
126
        |   '\\'
 
127
        |   ('0'..'3')
 
128
            (
 
129
                options {
 
130
                    warnWhenFollowAmbig = false;
 
131
                }
 
132
            :   ('0'..'9')
 
133
                (   
 
134
                    options {
 
135
                        warnWhenFollowAmbig = false;
 
136
                    }
 
137
                :   '0'..'9'
 
138
                )?
 
139
            )?
 
140
        |   ('4'..'7')
 
141
            (
 
142
                options {
 
143
                    warnWhenFollowAmbig = false;
 
144
                }
 
145
            :   ('0'..'9')
 
146
            )?
 
147
        )
 
148
    ;
 
149
 
 
150
protected
 
151
DIGIT
 
152
    :   '0'..'9'
 
153
    ;
 
154
 
 
155
INT 
 
156
    : (DIGIT)+
 
157
    ;
 
158
 
 
159
ID
 
160
options {
 
161
    testLiterals = true;
 
162
}
 
163
    :   ('a'..'z'|'A'..'Z'|'_') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')*
 
164
    ;
 
165