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