-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cup
82 lines (67 loc) · 2.44 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
package cup.example;
import java_cup.runtime.*;
import cup.example.Lexer;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import ast.*;
import gui.Main;
import Model.Color;
import Model.Direction;
parser code {:
protected Lexer lexer;
ast.Runtime rt = Main.runtime;
:}
init with {:
ComplexSymbolFactory f = new ComplexSymbolFactory();
symbolFactory = f;
File file = new File("input.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (IOException e) {
e.printStackTrace();
}
lexer = new Lexer(f,fis);
:};
scan with {: return lexer.next_token(); :};
terminal IF, WHILE , THEN , ELSE ,BAJARP, SUBIRP , COLORP, DIRP , DO;
terminal AVAZ , TABCOL , BORDE , PLUMADIR , PLUMACOL, PLUMAUP, PLUMADOWN ,AND, OR;
terminal NOT, SEMI, BRACKETL, BRACKETR;
terminal N;
terminal Color c;
terminal Direction d;
terminal Integer num;
non terminal Statement op;
non terminal Expression expr;
non terminal Expression program;
precedence left SEMI;
precedence left AND;
precedence left OR;
start with program;
op ::= BAJARP {: RESULT = new BajarPluma(); ;:}
| SUBIRP {: RESULT = new SubirPluma();;:}
| COLORP c:c1 {:RESULT = new CambiarColor(c1);;:}
| COLORP N {:RESULT = new CambiarColor(Color.BLACK);;:}
| DIRP d:d1 {: RESULT = new CambiarDireccion(d1);;:}
| DIRP N {:RESULT = new CambiarDireccion(Direction.NORTH);;:}
| AVAZ num:num1 {:RESULT = new Avanzar(num1);;:}
| IF expr:e1 THEN BRACKETL op:o1 BRACKETR {:RESULT = new If(e1,o1);;:}
| IF expr:e1 THEN BRACKETL op:o1 BRACKETR ELSE BRACKETL op:o2 BRACKETR {:RESULT = new IfElse(e1,o1,o2);;:}
| WHILE expr:e1 DO BRACKETL op:o1 BRACKETR {: RESULT = new While(e1, o1); ;:}
| op:o1 SEMI op:o2 {:RESULT = new StatementPair(o1,o2);;:}
;
expr ::= TABCOL c:c1 {:RESULT = new TableroCol(c1);:}
| TABCOL N {:RESULT = new TableroCol(Color.BLACK);:}
| BORDE {:RESULT = new Borde();:}
| PLUMADIR d:d1 {:RESULT = new PlumaDir(d1);:}
| PLUMADIR N {:RESULT = new PlumaDir(Direction.NORTH);:}
| PLUMACOL c:c1 {:RESULT = new PlumaCol(c1);:}
| PLUMACOL N {:RESULT = new PlumaCol(Color.BLACK);:}
| PLUMAUP {:RESULT = new PlumaUp();:}
| PLUMADOWN {:RESULT = new PlumaDown();:}
| expr:e1 AND expr:e2 {:RESULT = new And(e1,e2);:}
| expr:e1 OR expr:e2 {:RESULT = new Or(e1,e2);:}
| NOT expr:e1 {:RESULT = new Not(e1);:}
;
program ::= op:o1 {:o1.exec(rt);System.out.println(rt.tablero.toString());:};