-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSyn.g
More file actions
141 lines (112 loc) · 2.5 KB
/
Syn.g
File metadata and controls
141 lines (112 loc) · 2.5 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// COMS22201: Syntax analyser
parser grammar Syn;
options {
tokenVocab = Lex;
output = AST;
}
@members
{
private String cleanString(String s){
String tmp;
tmp = s.replaceAll("^'", "");
s = tmp.replaceAll("'$", "");
tmp = s.replaceAll("''", "'");
return tmp;
}
// TODO
//Search RecognitionException and displayREcognitionError in ANTLR3
//Key part of error handling
public void displayRecognitionError(String[] tokenNames, RecognitionException e){
String hdr = getErrorHeader(e);
String msg = getErrorMessage(e, tokenNames);
String errorType = e.toString();
int syntaxErrorNumber = getNumberOfSyntaxErrors();
System.err.println("Error "+syntaxErrorNumber+":");
System.err.println("Error type: "+errorType);
System.err.println("Error occured at : "+hdr);
System.err.println("The error message is : "+msg);
}
}
program :
statements
;
statements :
statement ( SEMICOLON ^ statement )*
;
statement :
writestatement
|writelnstatement
|skipstatement
|ifstatement
|whilestatement
|readstatement
|assignstatement
;
assignstatement :
variable ASSIGN^ exp
;
writestatement :
WRITE^ OPENPAREN! ( (boolexp) => boolexp | exp | string ) CLOSEPAREN!
;
writelnstatement :
WRITELN
;
skipstatement :
SKIP
;
ifstatement :
IF^ boolexp THEN! ( statement | compoundstatement ) ELSE! ( statement | compoundstatement)
;
compoundstatement :
OPENPAREN! statements CLOSEPAREN!
;
whilestatement :
WHILE^ boolexp DO! (statement | compoundstatement)
;
readstatement :
READ^ OPENPAREN! variable CLOSEPAREN!
;
string
scope { String tmp; }
:
s=STRING { $string::tmp = cleanString($s.text); }-> STRING[$string::tmp]
;
variable :
IDENTIFIER
;
constant :
INTNUM
;
realnum :
FLOAT
;
factor :
variable
|(constant) => constant
|(realnum) => realnum
| OPENPAREN! exp CLOSEPAREN!
;
exp :
term ((ADD|SUB|BITXOR)^ term)*
;
term :
factor ( (MULTIPLY|DIVIDE)^ factor)* // DIV is extra language feature
;
boolterm :
NOT^ booleanValue
|booleanValue
;
boolexp :
boolterm ( (AND|OR)^ boolterm )* // OR is extra language feature
;
booleanValue :
TRUE
|FALSE
|(exp EQ exp) => exp EQ^ exp
|(exp LEQ exp) => exp LEQ^ exp
| OPENPAREN! boolexp CLOSEPAREN!
|(exp LES exp) => exp LES^ exp // LES is extra language feature
|(exp GRT exp) => exp GRT^ exp // GRT is extra language feature
|(exp GEQ exp) => exp GEQ^ exp // GEQ is extra language feature
|(exp NEQ exp) => exp NEQ^ exp // NEQ is extra language feature
;