-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cup
126 lines (94 loc) · 5.51 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
115
116
117
118
119
120
121
122
123
124
125
126
/**
* 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 LPAREN, RPAREN, LCURLBRACKET, RCURLBRACKET, IDENT, COMMA, PLUS, IF, ELSE, REVERSE, PREFIX;
terminal String STRING_LITERAL;
/* Non terminals */
non terminal func_call, func_calls, func_args,
declaration, declarations, dec_args, declaration_body,
program_start, program_body,
concat_expr, string_expr, conditional_expr,
if_statement, if_else_body;
/**
* Precedence Declarations
*/
precedence left IF;
precedence left PLUS;
precedence left REVERSE;
/**
* The Grammar Rules
*/
/* Program structure */
program_start ::= program_body:body {: System.out.println("public class Main {");
System.out.println(body);
System.out.println("}"); :}
;
program_body ::= declarations:decs func_calls:calls {: RESULT = "public static void main(String[] args) {\n";
RESULT += "" + calls;
RESULT += "\n}";
RESULT += "\n" + decs; :}
| declarations:decs {: RESULT = "public static void main(String[] args) {}";
RESULT += "\n" + decs; :}
;
/* Declarations */
declarations ::= declarations:decs declaration:dec {: RESULT = dec + "\n" + decs; :}
| declaration:dec {: RESULT = dec; :}
;
dec_args ::= string_expr:str COMMA dec_args:args {: RESULT = "String " + str + ", " + args; :}
| string_expr:str RPAREN LCURLBRACKET {: RESULT = "String " + str + ") {\n"; :}
;
declaration ::= IDENT:id LPAREN dec_args:args declaration_body:body RCURLBRACKET {: RESULT = "public static String " + id + "(" + args;
RESULT += " " + body + "\n";
RESULT += "}"; :}
| IDENT:id LPAREN RPAREN LCURLBRACKET declaration_body:body RCURLBRACKET {: RESULT = "public static String " + id + "() {\n";
RESULT += "" + body + "\n";
RESULT += "}"; :}
;
declaration_body ::= concat_expr:expr {: RESULT = "return " + expr + ";"; :}
;
/* Function calls */
func_calls ::= func_call:call func_calls:calls {: RESULT = "System.out.println(" + call + ");\n" + calls; :}
| func_call:call {: RESULT = "System.out.println(" + call + ");"; :}
;
func_args ::= func_call:call COMMA func_args:args {: RESULT = call + ", " + args; :}
| string_expr:str COMMA func_args:args {: RESULT = str + ", " + args; :}
| func_call:call RPAREN {: RESULT = call + ")"; :}
| string_expr:str RPAREN {: RESULT = str + ")"; :}
;
func_call ::= IDENT:id LPAREN func_args:args {: RESULT = id + "(" + args; :}
| IDENT:id LPAREN RPAREN {: RESULT = id + "()"; :}
;
/* Expressions */
concat_expr ::= string_expr:str PLUS concat_expr:expr {: RESULT = str + " + " + expr; :}
| string_expr:str {: RESULT = str; :}
;
string_expr ::= REVERSE string_expr:str {: RESULT = "new StringBuilder(" + str + ").reverse().toString()"; :}
| STRING_LITERAL:str {: RESULT = "\"" + str + "\""; :}
| IDENT:id {: RESULT = id; :}
| if_statement:stmnt {: RESULT = stmnt; :}
;
conditional_expr ::= string_expr:str1 PREFIX string_expr:str2 {: RESULT = str2 + ".startsWith(" + str1 + ")"; :}
;
/* If/Else Statements */
if_statement ::= IF LPAREN conditional_expr:cond RPAREN if_else_body:ifbody ELSE if_else_body:elsebody {: RESULT = "(" + cond + ") ?";
RESULT += "(" + ifbody + "):";
RESULT += "(" + elsebody + ")"; :}
;
if_else_body ::= concat_expr:expr {: RESULT = expr; :}
| func_call:call {: RESULT = call; :}
;