source: trunk/yao/share/antlr-2.7.7/examples/python/inherit.tinyc/tinyc_p.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: 1.7 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/*
7 * Make sure to run antlr.Tool on the lexer.g file first!
8 */
9options {
10        mangleLiteralPrefix = "TK_";
11    language=Python;
12}
13
14{
15def main():
16   import tinyc_l
17   import tinyc_p
18   
19   L = tinyc_l.Lexer()
20   P = tinyc_p.Parser(L)
21   P.setFilename(L.getFilename())
22
23   ### Parse the input expression
24   try:
25      P.program()
26   except antlr.ANTLRException, ex:
27      print "*** error(s) while parsing."
28      print ">>> exit(1)"
29      import sys
30      sys.exit(1)
31
32   
33   ast = P.getAST()
34   
35   if not ast:
36      print "stop - no AST generated."
37      return
38     
39   ###show tree
40   print "Tree: " + ast.toStringTree()
41   print "List: " + ast.toStringList()
42   print "Node: " + ast.toString()
43   print "visit>>"
44   visitor = Visitor()
45   visitor.visit(ast);
46   print "visit<<"
47
48
49if __name__ == "__main__":
50   main()
51
52}
53
54class tinyc_p extends Parser;
55options {
56        importVocab=TinyC;
57}
58
59program
60        :       ( declaration )* EOF
61        ;
62
63declaration
64        :       (variable) => variable
65        |       function
66        ;
67
68declarator
69        :       id:ID
70        |       STAR id2:ID
71        ;
72
73variable
74        :       type declarator SEMI
75        ;
76
77function
78        :       type id:ID LPAREN
79                (formalParameter (COMMA formalParameter)*)?
80                RPAREN
81                block
82        ;
83
84formalParameter
85        :       type declarator
86        ;
87
88type:   
89        (
90                TK_int
91        |       TK_char
92        |       id:ID
93        )
94        ;
95
96block
97        :       LCURLY ( statement )* RCURLY
98        ;
99
100statement
101        :       (declaration) => declaration
102        |       expr SEMI
103        |       TK_if LPAREN expr RPAREN statement
104                ( TK_else statement )?
105        |       TK_while LPAREN expr RPAREN statement
106        |       block
107        ;
108
109expr:   assignExpr
110        ;
111
112assignExpr
113        :       aexpr (ASSIGN assignExpr)?
114        ;
115
116aexpr
117        :       mexpr (PLUS mexpr)*
118        ;
119
120mexpr
121        :       atom (STAR atom)*
122        ;
123
124atom:   ID
125        |       INT
126        |       CHAR_LITERAL
127        |       STRING_LITERAL
128        ;
129
Note: See TracBrowser for help on using the repository browser.