File: parser.cup

package info (click to toggle)
jflex 1.7.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 13,944 kB
  • sloc: java: 421,255; xml: 1,130; makefile: 123; lisp: 90; yacc: 65; sh: 13
file content (101 lines) | stat: -rw-r--r-- 3,596 bytes parent folder | download | duplicates (5)
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Copyright (C) 2001       Gerwin Klein <lsf@jflex.de>                    *
 * Copyright (C) 2001       Bernhard Rumpe <rumpe@in.tum.de>               *
 * All rights reserved.                                                    *
 *                                                                         *
 * License: BSD                                                            *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


// definition of tokens, if applicable with token type 
terminal INPUT, FUNCTIONS, OUTPUT, END, ARGUMENTS;
terminal IF, THEN, ELSE, FI, ERROR;
terminal COMMA, LPAR, RPAR;
terminal EQ, LE, LEQ, MINUS, PLUS, TIMES, DIV, UMINUS;

terminal String ID, NUMBER;

non terminal Tprogram  program;
non terminal Tparlist  parlist;
non terminal Texplist  explist;
non terminal Tdekllist dekllist;
non terminal Tdekl     dekl;
non terminal Texp      exp;
non terminal Tboolexp  boolexp;
non terminal Tident    ident;
non terminal Tnumber   number;


// precedences, left associativity
precedence left EQ, LE, LEQ;
precedence left MINUS, PLUS;
precedence left TIMES, DIV;
precedence left UMINUS;


// here the rules start 
program       ::= INPUT parlist:p FUNCTIONS dekllist:d OUTPUT explist:o
                                        ARGUMENTS explist:a END
                  {: RESULT = new Tprogram(p,d,o,a); :}
                ;

parlist       ::= ident:i
                  {: RESULT = new Tparlist(i); :}
                | parlist:p COMMA ident:i
                  {: RESULT = new Tparlist(p,i); :}
                ;

explist       ::= exp:e
                  {: RESULT = new Texplist(e); :}
                | explist:l COMMA exp:e
                  {: RESULT = new Texplist(l,e); :}
                ;

dekllist      ::= dekl:d
                  {: RESULT = new Tdekllist(d);:}
                | dekllist:l COMMA dekl:d
                  {: RESULT = new Tdekllist(l,d); :}
                ;

dekl          ::= ident:i LPAR parlist:p RPAR EQ exp:e
                  {: RESULT = new Tdekl(i,p,e); :}
                ;

exp           ::= number:n
                  {: RESULT = n; :}
                | ident:i
                  {: RESULT = i; :}
                | ident:i LPAR explist:e RPAR
                  {: RESULT = new Tfun(i,e); :}
                | LPAR exp:e RPAR
                  {: RESULT = e; :}
                | MINUS exp:e                
                  {: RESULT = new Tuminus(e); :} %prec UMINUS
                | exp:l PLUS exp:r
                  {: RESULT = new Texpinfix(l,'+',r); :}
                | exp:l TIMES exp:r
                  {: RESULT = new Texpinfix(l,'*',r); :}
                | exp:l DIV exp:r
                  {: RESULT = new Texpinfix(l,'/',r); :}
                | exp:l MINUS exp:r
                  {: RESULT = new Texpinfix(l,'-',r); :}
                | IF boolexp:b THEN exp:t ELSE exp:e FI
                  {: RESULT = new Tifthenelse(b,t,e); :}
                ;

boolexp       ::= exp:l EQ exp:r
                  {: RESULT = new Tboolexp(l,'=',r); :}
                | exp:l LE exp:r
                  {: RESULT = new Tboolexp(l,'<',r); :}
                | exp:l LEQ exp:r
                  {: RESULT = new Tboolexp(l,'!',r); :}
                ;

ident         ::= ID:n
                  {: RESULT = new Tident(n); :}
                ;

number        ::= NUMBER:z
                  {: RESULT = new Tnumber(z); :}
                ;