-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyParser.g4
129 lines (104 loc) · 3.42 KB
/
MyParser.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
// $antlr-format columnLimit 120, useTab false, minEmptyLines 1
// $antlr-format allowShortRulesOnASingleLine false, allowShortBlocksOnASingleLine true
// $antlr-format alignSemicolons hanging, alignColons hanging
parser grammar MyParser;
options {
tokenVocab = MyLexer;
}
program
: statement* EOF
;
statement
: OPEN_BRACKET_CURLY statement* CLOSE_BRACKET_CURLY # scopeStatement
| ifThenElse # scopeStatement
| forLoop # scopeStatement
| whileLoop # scopeStatement
| assignment # simpleStatement
| print # simpleStatement
| return # simpleStatement
| break # simpleStatement
| continue # simpleStatement
;
ifThenElse
: if then else?
;
if
: IF OPEN_BRACKET_ROUND comparison CLOSE_BRACKET_ROUND
;
then
: statement
;
else
: ELSE statement
;
forLoop
: FOR id ASSIGN range statement
;
range
: expression RANGE_OP expression
;
whileLoop
: WHILE OPEN_BRACKET_ROUND comparison CLOSE_BRACKET_ROUND statement
;
comparison
: expression (EQ | NEQ | LT | GT | LEQ | GEQ) expression
;
assignment
: (id | elementReference) ASSIGN expression SEMICOLON # simpleAssignment
| (id | elementReference) (
ASSIGN_PLUS
| ASSIGN_MINUS
| ASSIGN_MULTIPLY
| ASSIGN_DIVIDE
) expression SEMICOLON # compoundAssignment
;
print
: PRINT expression (COMMA expression)* SEMICOLON
;
return
: RETURN expression? SEMICOLON
;
expression
: OPEN_BRACKET_ROUND expression CLOSE_BRACKET_ROUND # parenthesesExpression
| MINUS expression # minusExpression
| expression MAT_TRANSPOSE_OP # transposeExpression
| expression op = (MULTIPLY | DIVIDE) expression # binaryExpression
| expression op = (MAT_MULTIPLY | MAT_DIVIDE) expression # binaryExpression
| expression op = (PLUS | MINUS) expression # binaryExpression
| expression op = (MAT_PLUS | MAT_MINUS) expression # binaryExpression
| specialMatrixFunction # singleExpression
| id # singleExpression
| int # singleExpression
| float # singleExpression
| string # singleExpression
| elementReference # singleExpression
| vector # singleExpression
;
specialMatrixFunction
: EYE OPEN_BRACKET_ROUND expression CLOSE_BRACKET_ROUND
| (ZEROS | ONES) OPEN_BRACKET_ROUND expression (COMMA expression)* CLOSE_BRACKET_ROUND
;
break
: BREAK SEMICOLON
;
continue
: CONTINUE SEMICOLON
;
vector
: OPEN_BRACKET_SQUARE expression (COMMA expression)* CLOSE_BRACKET_SQUARE
;
elementReference
: id OPEN_BRACKET_SQUARE expression (COMMA expression)* CLOSE_BRACKET_SQUARE
;
id
: ID
;
int
: INT
;
float
: FLOAT
;
string
: STRING
;