-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
123 lines (89 loc) · 1.97 KB
/
parser.y
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
%{
import java.io.*;
%}
%start P
%token DEF IF THEN ELSE WHILE DO FOR
%token Punctuation_element
%token Operator
%token EQL_EQL GREATER_THAN_OR_EQUALS LESS_THAN_OR_EQUALS NOT_EQUALS
%token Identifier
%token Literal
/* solving ambiguities */
%left '-' '+'
%left '*' '/' '%'
%left DEF IF THEN WHILE DO FOR
%right ELSE
%%
P : D ';' P
| D
;
D : DEF Identifier '(' ARGS ')' '=' E ';'
;
ARGS : Identifier ',' ARGS
| Identifier
;
E: Literal
| Identifier
| IF E OP E THEN E ELSE E
| IF E '%' E THEN E ELSE E
| FOR E DO E
| DO E WHILE E
| E '+' E
| E '-' E
| E '*' E
| E '/' E
| E '%' E
| Identifier '(' C ')'
;
C: E ',' C
| E
;
OP: EQL_EQL
| '>'
| '<'
| GREATER_THAN_OR_EQUALS
| LESS_THAN_OR_EQUALS
| NOT_EQUALS
;
%%
private static boolean IS_ERROR = false;
/* a reference to the lexer object */
private Lexer lexer;
/* interface to the lexer */
private int yylex() {
int yyl_return = -1;
try {
yyl_return = lexer.yylex();
}
catch (IOException e) {
System.err.println("IO error :"+e);
}
return yyl_return;
}
/* error reporting */
public void yyerror (String error) {
IS_ERROR = true;
System.err.println("ERROR at line: " + lexer.GetLine() + " | with error name: " + error + " | at the word: \"" + lexer.GetWord() + "\"");
}
/* lexer is created in the constructor */
public Parser(Reader r) {
lexer = new Lexer(r , this);
}
public static void main(String args[]) throws IOException {
Parser yyparser;
if ( args.length > 0 ) {
// parse a file
yyparser = new Parser(new FileReader(args[0]));
System.out.println("Start Parsing...");
yyparser.yyparse();
if(IS_ERROR){
System.out.println("Done with errors");
}
else{
System.out.println("Done without errors");
}
}
else {
System.out.println("ERROR: Provide an input file as Parser argument");
}
}