-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMini.g4
More file actions
110 lines (102 loc) · 3.19 KB
/
Mini.g4
File metadata and controls
110 lines (102 loc) · 3.19 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
grammar Mini;
@header
{
/* package declaration here */
}
/*
Parser Rules
*/
program
: types declarations functions EOF
;
types
: typeDeclaration*
|
;
typeDeclaration
: 'struct' ID '{' nestedDecl '}' ';'
;
nestedDecl
: (decl ';')+
;
decl
: type ID
;
type
: 'int' # IntType
| 'bool' # BoolType
| 'struct' ID # StructType
| 'int_array' # ArrayType
;
declarations
: (declaration)*
;
declaration
: type ID (',' ID)* ';'
;
functions
: function*
;
function
: 'fun' ID parameters returnType '{' declarations statementList '}'
;
parameters
: '(' (decl (',' decl)*)? ')'
;
returnType
: type # ReturnTypeReal
| 'void' # ReturnTypeVoid
;
statement
: block # NestedBlock
| lvalue '=' (expression | 'read') ';' # Assignment
| 'print' expression ';' # Print
| 'print' expression 'endl' ';' # PrintLn
| 'if' '(' expression ')' thenBlock=block
('else' elseBlock=block)? # Conditional
| 'while' '(' expression ')' block # While
| 'delete' expression ';' # Delete
| 'return' (expression)? ';' # Return
| ID '(' arguments ')' ';' # Invocation
;
block
: '{' statementList '}'
;
statementList
: statement*
;
lvalue
: ID # LvalueId
| lvalue '.' ID # LvalueDot
| lvalue '[' expression ']' # LvalueIndex
;
expression
: ID '(' arguments ')' # InvocationExpr
| expression ('.' ID) # DotExpr
| lft=expression '[' idx=expression ']' # IndexExpr
| op=('-' | '!') expression # UnaryExpr
| lft=expression op=('*' | '/') rht=expression # BinaryExpr
| lft=expression op=('+' | '-') rht=expression # BinaryExpr
| lft=expression op=('<' | '>' | '<=' | '>=') rht=expression # BinaryExpr
| lft=expression op=('==' | '!=') rht=expression # BinaryExpr
| lft=expression op='&&' rht=expression # BinaryExpr
| lft=expression op='||' rht=expression # BinaryExpr
| ID # IdentifierExpr
| INTEGER # IntegerExpr
| 'true' # TrueExpr
| 'false' # FalseExpr
| 'new' ID # NewExpr
| 'new int_array[' INTEGER ']' # NewArrayExpr
| 'null' # NullExpr
| '(' expression ')' # NestedExpr
;
arguments
: (expression (',' expression)*)?
;
/*
Lexer Rules
*/
ID : [a-zA-Z][a-zA-Z0-9]* ;
INTEGER : '0' | [1-9][0-9]* ;
WS : [ \t\n\r\f]+ -> skip;
COMMENT : '#' .*? '\n' -> skip;