This repository has been archived by the owner on Nov 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
General.g4
181 lines (151 loc) · 4.16 KB
/
General.g4
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
grammar General;
@parser::members{
public static final SymbolTable map = new SymbolTable();
}
/**** SYNTACTIC RULES ****/
/**
* General grammar main rule
*/
main: (importQuantities)* statList EOF;
/**
* List of zero or more statements.
* All statements should end with a semi-colon.
*/
statList : (stat TERM)*;
/**
* Import of a file with all quantities defined by the programmer.
*/
importQuantities: 'import' ID TERM;
/**
* Different variations of a statement.
*/
stat: print | assign | declaration | conditionalExpr | dimCheck;
/**
* Declaration of a variable using a primitive type and identifier.
*/
declaration: type ID;
/**
* Rule for the assignement of a value to a variable.
* It can also declare the variable.
*/
assign: declaration '=' expr #declareAndAssign
| ID '=' expr #justAssign
;
/**
* Prints out the value of an expression to the console.
*/
print: 'print' '(' expr ')';
/**
* Input rule for a dynamic interaction with the user.
*/
input: 'input' '(' STRING ')';
/**
* Different primitive types accepted by our language.
*/
type returns[Type res]:
'Integer' #intType
| 'Real' #realType
| 'Boolean' #boolType
| 'String' #strType
| TYPE_ID #customType
;
/**
* Rule for all the expressions accepted by our language.
* It returns the type of the expression, (completem o resto sff)
* Meaning of the expressions from top-down order:
* - Pow expression
* - Absolute value expression
* - Binary operator expression
* - Multiplication, division and remainder expressions
* - Addition and subtraction expressions
* - Conditional negation expression
* - Conditional equality expression
* - Conditional relational expression
* - Conditional 'and/or' expression
* - Parenthesis expression
* - Input expression
* - Identifier expression
* - String expression
* - Boolean expression
* - Integer expression
* - Real expression
*/
expr returns[Type exprType, String varName, String dimension, String unit]
: <assoc=right> e1=expr '^' e2=expr # Pow
| 'abs' '(' expr ')' # AbsoluteValue
| sign = ('+'|'-') expr # binaryOperator
| e1 = expr op = ('*' | '/' | '%') e2 = expr # multDiv
| e1 = expr op = ('+' | '-') e2 = expr # addSub
| 'not' expr # conditionalNegation
| e1=expr op=('==' | '!=' | '===' | '!==') e2=expr # conditionalEquality
| e1=expr op=('<' | '>' | '<=' | '>=') e2=expr # conditionalRelational
| e1=expr op=('or' | 'and') e2=expr # conditionalAndOr
| '(' expr ')' # parentheses
| input # inputValue
| ID # IDvalue
| STRING # StringValue
| BOOLEAN # BooleanValue
| INT unitID? # IntValue
| REAL unitID? # RealValue
;
dimCheck: dimensionCheck
| unitCheck
;
/**
* Check the dimension of a customized type.
*/
dimensionCheck : '(' expr ').dimension';
/**
* Check the unit of a customized type.
*/
unitCheck : '(' expr ').unit';
conditionalExpr: conditional
| whileConditional
| forConditional
;
/**
* Conditional 'if' statement, with 'else if' and 'else' options
*/
conditional: 'if' expr '{' trueStats=statList '}'('else' falseStats = elseConditon)? ;
elseConditon: conditional | '{' statList '}';
/**
* Conditional 'while' statement
*/
whileConditional: 'while' expr '{' trueStats=statList '}'; //Semantic Check whether expr is boolean
/**
* Conditional 'for' statement
*/
forConditional: 'for' incVarDec=assign ',' breakCond=expr ',' incCond=expr '{' trueStats=statList '}'; //Semantic Check whether breakCond is boolean AND incCond is NOT boolean
/**
* Declaration of a new unit.
*/
unitID: '[' ID (op = ('/' | '*') ID)? ']';
/**** LEXICAL RULES ****/
/**
* Boolean type options.
*/
BOOLEAN: 'true' | 'false';
/**
* Identifiers definition.
*/
TYPE_ID: [A-Z]LETTER+;
ID: LETTER (LETTER | DIGIT)*;
fragment LETTER: [a-zA-Z_];
/**
* Numbers definition.
*/
INT: DIGIT+;
REAL: DIGIT+ '.' DIGIT+ | '.' DIGIT+;
fragment DIGIT: [0-9];
/**
* Comment and white-spaces definition.
*/
SINGLE_LINE_COMMENT: '//' .*? '\n' -> skip;
MULTI_LINE_COMMENT: '/*' .*? '*/' -> skip;
WS: [ \r\n\t]+ -> skip;
/**
* String definition.
*/
STRING: '"' .*? '"';
TERM: ';';
ERROR: .;