1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
---|
2 | * Copyright (C) 2001 Gerwin Klein <[email protected]> *
|
---|
3 | * Copyright (C) 2001 Bernhard Rumpe <[email protected]> *
|
---|
4 | * All rights reserved. *
|
---|
5 | * *
|
---|
6 | * This program is free software; you can redistribute it and/or modify *
|
---|
7 | * it under the terms of the GNU General Public License. See the file *
|
---|
8 | * COPYRIGHT for more information. *
|
---|
9 | * *
|
---|
10 | * This program is distributed in the hope that it will be useful, *
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
---|
13 | * GNU General Public License for more details. *
|
---|
14 | * *
|
---|
15 | * You should have received a copy of the GNU General Public License along *
|
---|
16 | * with this program; if not, write to the Free Software Foundation, Inc., *
|
---|
17 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
|
---|
18 | * *
|
---|
19 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
---|
20 |
|
---|
21 |
|
---|
22 | // definition of tokens, if applicable with token type
|
---|
23 | terminal INPUT, FUNCTIONS, OUTPUT, END, ARGUMENTS;
|
---|
24 | terminal IF, THEN, ELSE, FI, ERROR;
|
---|
25 | terminal COMMA, LPAR, RPAR;
|
---|
26 | terminal EQ, LE, LEQ, MINUS, PLUS, TIMES, DIV, UMINUS;
|
---|
27 |
|
---|
28 | terminal String ID, NUMBER;
|
---|
29 |
|
---|
30 | non terminal Tprogram program;
|
---|
31 | non terminal Tparlist parlist;
|
---|
32 | non terminal Texplist explist;
|
---|
33 | non terminal Tdekllist dekllist;
|
---|
34 | non terminal Tdekl dekl;
|
---|
35 | non terminal Texp exp;
|
---|
36 | non terminal Tboolexp boolexp;
|
---|
37 | non terminal Tident ident;
|
---|
38 | non terminal Tnumber number;
|
---|
39 |
|
---|
40 |
|
---|
41 | // precedences, left associativity
|
---|
42 | precedence left EQ, LE, LEQ;
|
---|
43 | precedence left MINUS, PLUS;
|
---|
44 | precedence left TIMES, DIV;
|
---|
45 | precedence left UMINUS;
|
---|
46 |
|
---|
47 |
|
---|
48 | // here the rules start
|
---|
49 | program ::= INPUT parlist:p FUNCTIONS dekllist:d OUTPUT explist:o
|
---|
50 | ARGUMENTS explist:a END
|
---|
51 | {: RESULT = new Tprogram(p,d,o,a); :}
|
---|
52 | ;
|
---|
53 |
|
---|
54 | parlist ::= ident:i
|
---|
55 | {: RESULT = new Tparlist(i); :}
|
---|
56 | | parlist:p COMMA ident:i
|
---|
57 | {: RESULT = new Tparlist(p,i); :}
|
---|
58 | ;
|
---|
59 |
|
---|
60 | explist ::= exp:e
|
---|
61 | {: RESULT = new Texplist(e); :}
|
---|
62 | | explist:l COMMA exp:e
|
---|
63 | {: RESULT = new Texplist(l,e); :}
|
---|
64 | ;
|
---|
65 |
|
---|
66 | dekllist ::= dekl:d
|
---|
67 | {: RESULT = new Tdekllist(d);:}
|
---|
68 | | dekllist:l COMMA dekl:d
|
---|
69 | {: RESULT = new Tdekllist(l,d); :}
|
---|
70 | ;
|
---|
71 |
|
---|
72 | dekl ::= ident:i LPAR parlist:p RPAR EQ exp:e
|
---|
73 | {: RESULT = new Tdekl(i,p,e); :}
|
---|
74 | ;
|
---|
75 |
|
---|
76 | exp ::= number:n
|
---|
77 | {: RESULT = n; :}
|
---|
78 | | ident:i
|
---|
79 | {: RESULT = i; :}
|
---|
80 | | ident:i LPAR explist:e RPAR
|
---|
81 | {: RESULT = new Tfun(i,e); :}
|
---|
82 | | LPAR exp:e RPAR
|
---|
83 | {: RESULT = e; :}
|
---|
84 | | MINUS exp:e
|
---|
85 | {: RESULT = new Tuminus(e); :} %prec UMINUS
|
---|
86 | | exp:l PLUS exp:r
|
---|
87 | {: RESULT = new Texpinfix(l,'+',r); :}
|
---|
88 | | exp:l TIMES exp:r
|
---|
89 | {: RESULT = new Texpinfix(l,'*',r); :}
|
---|
90 | | exp:l DIV exp:r
|
---|
91 | {: RESULT = new Texpinfix(l,'/',r); :}
|
---|
92 | | exp:l MINUS exp:r
|
---|
93 | {: RESULT = new Texpinfix(l,'-',r); :}
|
---|
94 | | IF boolexp:b THEN exp:t ELSE exp:e FI
|
---|
95 | {: RESULT = new Tifthenelse(b,t,e); :}
|
---|
96 | ;
|
---|
97 |
|
---|
98 | boolexp ::= exp:l EQ exp:r
|
---|
99 | {: RESULT = new Tboolexp(l,'=',r); :}
|
---|
100 | | exp:l LE exp:r
|
---|
101 | {: RESULT = new Tboolexp(l,'<',r); :}
|
---|
102 | | exp:l LEQ exp:r
|
---|
103 | {: RESULT = new Tboolexp(l,'!',r); :}
|
---|
104 | ;
|
---|
105 |
|
---|
106 | ident ::= ID:n
|
---|
107 | {: RESULT = new Tident(n); :}
|
---|
108 | ;
|
---|
109 |
|
---|
110 | number ::= NUMBER:z
|
---|
111 | {: RESULT = new Tnumber(z); :}
|
---|
112 | ;
|
---|