-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cup
114 lines (96 loc) · 5.15 KB
/
parser.cup
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
/**
* Package and Import Specifications
*/
import java_cup.runtime.*;
/**
* Usercode Components
*/
parser code {:
// Connect this parser to a scanner!
Scanner s;
Parser(Scanner s){ this.s=s; }
:}
/* define how to connect to the scanner! */
scan with {: return s.next_token(); :};
/**
* Symbol Lists
*/
/* Terminals (tokens returned by the scanner). */
terminal PLUS, LPAREN, RPAREN, EQUAL, COMMA, RBRACE, RPLBRACE, IF, ELSE, IN;
terminal String STR, STRING_LITERAL;
/* Non terminals */
non terminal expr_list;
non terminal String def, defexec, body, argdef, argexec, more, more2, defexec2, argexec2, ifstate, ifarg, iftrue, elsetrue;
/**
* Precedence Declarations
*/
precedence left COMMA,STR;
precedence left EQUAL, IN;
precedence left PLUS;
precedence left LPAREN,RPAREN;
precedence left RBRACE, RPLBRACE;
precedence left IF,ELSE;
/**
* The Grammar Rules
*/
expr_list ::= expr_list def:d {: System.out.println(d); :}
| expr_list defexec:de {: System.out.println(de); :}
|
;
// Exw enwsei to "}(" se RPLBRACE gia na mhn uparksei conflict me thn klhsh ths defexec
def ::= STR:s LPAREN argdef:a1 RPLBRACE body:b RBRACE {: RESULT = "(" + "define " + " (" + s + " "+ a1 + ") " + b + ")"; :}
;
// Ta orismata gia th dhlwsh sunarthshs
argdef ::= argdef:a1 COMMA argdef:a2 {: RESULT = " " + a1 + " " + a2; :}
| STR:s {: RESULT = s; :}
| {: RESULT = ""; :}
;
// H klhsh ths sunarthshs
defexec ::= STR:s LPAREN argexec:ae RPAREN {: RESULT = "(" + s + ae + ")"; :}
;
// H klhsh sunarthshs se eswteriko orismo sunarthshs
defexec2 ::= STR:s LPAREN argexec2:ae RPAREN {: RESULT = "(" + s + ae + ")"; :}
;
// Ta orismata gia thn klhsh sunarthshs
argexec ::= more:m COMMA argexec:ae {: RESULT = " " + m + " " + ae; :}
| more:m argexec:ae {: RESULT = " " + m + " " + ae; :}
| {: RESULT = ""; :}
;
// Ta orismata gia thn klhsh sunarthshs se eswteriko orismo sunarthshs
argexec2 ::= more2:m COMMA argexec2:ae {: RESULT = " " + m + " " + ae; :}
| more2:m argexec2:ae {: RESULT = " " + m + " " + ae; :}
| {: RESULT = ""; :}
;
more ::= defexec:de {: RESULT = de; :}
| more:m1 PLUS more:m2 {: RESULT = "(string-append " + m1 + " " + m2 + ")"; :}
| LPAREN more:m RPAREN {: RESULT = m; :}
| STRING_LITERAL:sl {: RESULT = "\"" + sl + "\""; :}
;
// Ta orismata sthn eswterikh klhsh apla periexoun kai metavlhth se sxesh me to more
more2 ::= defexec2:de {: RESULT = de; :}
| more2:m1 PLUS more2:m2 {: RESULT = "(string-append " + m1 + " " + m2 + ")"; :}
| LPAREN more2:m RPAREN {: RESULT = m; :}
| STRING_LITERAL:sl {: RESULT = "\"" + sl + "\""; :}
| STR:s {: RESULT = s; :}
;
body ::= body:b1 PLUS body:b2 {: RESULT = "(string-append " + b1 + " " + b2 + ")"; :}
| STRING_LITERAL:sl {: RESULT = "\"" + sl + "\""; :}
| LPAREN body:b RPAREN {: RESULT = b; :}
| STR:s {: RESULT = s; :}
| defexec2:de {: RESULT = de; :}
| ifstate:is {: RESULT = is; :}
| {: RESULT = ""; :}
;
ifstate ::= IF LPAREN ifarg:ia RPAREN iftrue:it ELSE elsetrue:e {: RESULT = "(if " + ia + it + e + ")"; :}
;
ifarg ::= ifstate:is {: RESULT = is; :}
| more2:m {: RESULT = m; :}
| LPAREN ifarg:ia RPAREN {: RESULT = ia; :}
| ifarg:ia1 EQUAL ifarg:ia2 {: RESULT = "(equal? " + ia1 + " " + ia2 + ")"; :}
| ifarg:ia1 IN ifarg:ia2 {: RESULT = "(substring? " + ia1 + " " + ia2 + ")"; :}
;
iftrue ::= ifstate:is {: RESULT = is; :}
| more2:m {: RESULT = " " + m; :}
;
elsetrue ::= iftrue:is {: RESULT = " " + is; :}
;