-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGsaGrammar.g4
132 lines (106 loc) · 3.73 KB
/
GsaGrammar.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
grammar GsaGrammar;
@header{
package antlr2.antlr;
}
prog : (decl | expr | statement | methodInvocation)+ EOF # Program
;
decl: INT ID ASSIGN expr # IntDeclaration
| STRING ID ASSIGN STRG # StringDeclaration
| BOOLEAN ID ASSIGN BOOL # BoolDeclaration
| CHAR_T ID ASSIGN CHAR # CharDeclaration
| DEFINE ID method # MethodDeclaration
| INT ID SQUAREBRACKET1 (INT)? SQUAREBRACKET2 (ASSIGN CURLYBRACKET1 array_init CURLYBRACKET2)? #IntArrayDeclaration
| ID ASSIGN CAT_OPERATOR PARANTESSES1 methodInvocation COMMA NUM PARANTESSES2 # CatOperatorDeclaration
;
array_init: NUM ( ',' NUM )* #IntArray;
array_elem: ID SQUAREBRACKET1 expr SQUAREBRACKET2;
expr : expr MULT expr # Multiplication
| expr DIV expr # Division
| expr ADD expr # Addition
| expr SUBT expr # Subtraction
| ID # Variable
| NUM # Number
| STRG # String
| CHAR # Char
| BOOL # Bool
| expr EQ expr # Equals
| expr GT expr # GreaterThan
| expr LT expr # LessThan
| expr EQGT expr # EqualsOrGreaterThan
| expr EQLT expr # EqualsOrLessThan
| expr ASSIGN expr # Assignment
| array_elem # CallArrayMember
;
statement : conditionalStatement # Condition
| iterationStatement # Iteration
| returnStatement # Return
;
conditionalStatement : ifStmt # OnlyIfStatement
| ifStmt elseStmt? # IfElseStatement
| ifStmt+ elifStmt elseStmt? # IfElifStatement
;
ifStmt: IF conditionBlock ifBody #IfStatement;
elifStmt: ELIF PARANTESSES1 expr PARANTESSES2 CURLYBRACKET1 (expr | statement)? CURLYBRACKET2 #ElifStatement;
elseStmt: ELSE ifBody #ElseStatement;
ifBody: CURLYBRACKET1 (expr | statement | decl)* CURLYBRACKET2 #ConditionBody;
conditionBlock: PARANTESSES1 expr PARANTESSES2;
iterationStatement : FOR PARANTESSES1 NUM TO NUM PARANTESSES2 CURLYBRACKET1 blockStatement CURLYBRACKET2 #ForToStatement
| FOR PARANTESSES1 UNTIL NUM PARANTESSES2 CURLYBRACKET1 blockStatement CURLYBRACKET2 #ForUntilStatement
;
blockStatement : (expr | decl | statement | methodInvocation)+ #LocalStatement
;
block: CURLYBRACKET1 blockStatement* CURLYBRACKET2 #LocalStatements;
methodArgs: PARANTESSES1 args? PARANTESSES2 #MethodArguments;
methodArg: (INT | STRING | BOOL | CHAR) ID #Parameter
;
args: methodArg #SingleArg
| (methodArg COMMA)+ methodArg #MultipleArgs
;
argumentList: argument #SingleArgument
| (argument COMMA)+ argument #MultipleArguments
;
argument: ID
| expr
;
method: methodArgs block #MethodDefinition;
returnStatement: RETURN expr #ReturnExpression
;
methodInvocation: ID PARANTESSES1 argumentList* PARANTESSES2 #MethodCall;
/* TOKENAI */
ASSIGN : '=';
INT : 'int';
STRING : 'string';
BOOLEAN : 'bool';
CHAR_T: 'char';
DEFINE: 'def';
NUM : '0' | '-'?[1-9][0-9]*;
STRG : ["][a-zA-Z0-9]*["];
CHAR: ['][a-zA-Z0-9]['];
BOOL : 'true' | 'false';
MULT : '*';
DIV : '/';
ADD : '+';
SUBT : '-';
EQ : '==';
GT : '>';
LT : '<';
EQGT : '>=';
EQLT : '<=';
PARANTESSES1 : '(';
PARANTESSES2 : ')';
CURLYBRACKET1 : '{';
CURLYBRACKET2 : '}';
SQUAREBRACKET1 :'[';
SQUAREBRACKET2 :']';
SCOL: ';';
COMMA : ',';
RETURN: 'return';
CAT_OPERATOR: 'cat';
IF : 'if';
ELIF : 'elif';
ELSE : 'else';
FOR: 'for';
TO: 'to';
UNTIL: 'until';
ID : [a-zA-Z0-9]+;
WS : [ \t\n\r]+ -> skip;