-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntactic.cup
103 lines (95 loc) · 4.07 KB
/
syntactic.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
package com.jordanec.calcjec;
/**
* Created by jordan on 10/10/16.
*/
import java_cup.runtime.*;
import com.jordanec.calcjec.operations.*;
parser code {:
public parser (java.io.Reader input) {
super(new LexerAnalyser(input));
}
public void report_error(String message, Object info) {
if (message.equals("Syntax error"))
System.out.println(new VisiCalcJECException(VisiCalcJECException.syntaxError100).error);
}
public void report_fatal_error(String message, Object info) {
if (!message.equals("Couldn't repair and continue parse"))
report_error(message, info);
}
:};
terminal SUM, SUBTRACTION, MULTIPLICATION, DIVISION;
terminal POW, SQUARE_ROOT;
terminal ASSIGN, CONDITIONAL;
terminal LEFT_PAREN, RIGHT_PAREN;
terminal Integer NUMBER;
terminal Float FLOAT;
terminal Boolean BOOLEAN;
terminal String HELP, VERS, LIMP, TERM, DEFI, LIST, CALC, BORR, GRAB, VALI;
terminal String IDENTIFIER, RELATIONAL;
non terminal Expression expr;
precedence left SUM, SUBTRACTION;
precedence left MULTIPLICATION, DIVISION;
precedence left POW, SQUARE_ROOT;
precedence left ASSIGN;
precedence left DEFI, CALC, CONDITIONAL, VALI;
expr ::= SQUARE_ROOT expr:e1
{: RESULT = new SquareRoot(e1); :}
| expr:e1 POW expr:e2
{: RESULT = new Pow(e1, e2); :}
| expr:e1 DIVISION expr:e2
{: RESULT = new Division(e1, e2); :}
| expr:e1 MULTIPLICATION expr:e2
{: RESULT = new Multiplication(e1, e2); :}
| expr:e1 SUBTRACTION expr:e2
{: RESULT = new Subtraction(e1, e2); :}
| SUBTRACTION expr:e2
{: RESULT = new Subtraction(new Literal(0), e2); :}
| expr:e1 SUM expr:e2
{: RESULT = new Sum(e1, e2); :}
| SUM expr:e2
{: RESULT = new Sum(new Literal(0), e2); :}
| LEFT_PAREN expr:e RIGHT_PAREN
{: RESULT = e; :}
| IDENTIFIER:s ASSIGN expr:e
{: RESULT = new Assignment(s, e); :}
| IDENTIFIER:s
{: RESULT = new Identifier(s); :}
| NUMBER:n
{: RESULT = new Literal(n); :}
| FLOAT:n
{: RESULT = new Literal(n); :}
| BOOLEAN:n
{: RESULT = new Literal(n); :}
| HELP:c
{: RESULT = new Command(c); :}
| VERS:c
{: RESULT = new Command(c); :}
| LIMP:c
{: RESULT = new Command(c); :}
| TERM:c
{: RESULT = new Command(c); :}
| DEFI:c IDENTIFIER:s ASSIGN expr:e
{: RESULT = new Command(c, new Assignment(s, e)); :}
| IDENTIFIER:s ASSIGN expr:e DEFI:c
{: RESULT = new Command(c, new Assignment(s, e)); :}
| LIST:c
{: RESULT = new Command(c); :}
| CALC:c expr:e
{: RESULT = new Command(c, e); :}
| expr:e CALC:c
{: RESULT = new Command(c, e); :}
| BORR:c IDENTIFIER:s
{: RESULT = new Command(c, new Identifier(s)); :}
| IDENTIFIER:s BORR:c
{: RESULT = new Command(c, new Identifier(s)); :}
| GRAB:c IDENTIFIER:s
{: RESULT = new Command(c, new Identifier(s)); :}
| IDENTIFIER:s GRAB:c
{: RESULT = new Command(c, new Identifier(s)); :}
| expr:e1 RELATIONAL:r expr:e2 CONDITIONAL
{: RESULT = new Relational(e1, e2, r); :}
| VALI expr:e1 RELATIONAL:r expr:e2 CONDITIONAL
{: RESULT = new Relational(e1, e2, r); :}
| expr:e1 RELATIONAL:r expr:e2 CONDITIONAL VALI
{: RESULT = new Relational(e1, e2, r); :}
;