-
Notifications
You must be signed in to change notification settings - Fork 1
/
VYP.g4
150 lines (121 loc) · 4.43 KB
/
VYP.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
/*
use this command to generate grammar with antlr
java org.antlr.v4.Tool -Dlanguage=Python3 VYP.g4
use this command for lexer testing
java org.antlr.v4.runtime.misc.TestRig VYP r -tokens
*/
grammar VYP;
program: (function_definition | class_definition)+;
statement
: if_else_block
| while_block
| variable_assignment
| instance_assignment
| variable_definition
| return_statement
| expression ';'
;
function_definition: function_header function_body;
function_header: variable_type ID '(' parameter_list ')';
function_body: '{' statement* '}';
class_definition: class_header class_body;
class_header: CLASS class_id=ID ':' parent_id=ID;
class_body: '{' class_members* '}';
class_members
: field_definition #class_field_definition
| function_definition #method_definition;
field_definition: variable_type ID multiple_field_definition* ';';
multiple_field_definition: ',' ID;
// Might be extended with visibility modificators
variable_definition: variable_type ID multiple_variable_definition* ';';
multiple_variable_definition: ',' ID;
variable_assignment: ID '=' expression ';';
instance_assignment: instance_expression '=' expression ';';
return_statement: RETURN expression? ';';
code_block: '{' statement* '}';
if_else_block: if_part else_part;
if_expression: IF '(' expression ')';
if_part: if_expression code_block;
else_part: ELSE code_block;
while_expression: WHILE '(' expression ')';
while_block: while_expression code_block;
expression
: '(' cast=(INT | STRING | ID) ')' expression #castExpression
| '(' expression ')' #bracket_expression
| MINUS expression #negative_expression
| '!' expression #negation_expression
| expression operator=('*' | '/') expression #muldiv_expression
| expression operator=('+' | MINUS) expression #plusminus_expression
| expression operator=(LE | LEQ | GT | GTQ) expression #comparison_expression
| expression operator=(LOGICAL_EQUAL | LOGICAL_NEQUAL) expression #equality_expression
| expression operator=LOGICAL_AND expression #and_expression
| expression operator=LOGICAL_OR expression #or_expression
| instance_expression #instance_expression_value
| instance_creation #new_expression
| function_call #function_expression
| literal_value #literal_expression
| ID #variable_expression;
literal_value
: INTEGER_LITERAL
| STRING_LITERAL;
first_instance: (reference=(SUPER | THIS | ID) | function_call);
instance_expression: first_instance nested_object;
instance_creation: NEW ID;
nested_object
: (final_field_expression | final_method_expression) next_final*;
next_final: (final_field_expression | final_method_expression);
final_field_expression: '.' ID;
final_method_expression: '.' function_call;
function_call: ID '(' expression_list? ')';
expression_list: expression next_expression*;
next_expression
: ',' expression;
variable_type: INT | STRING | VOID | ID;
parameter_list
: VOID
| function_parameters;
function_parameter_definition: variable_type ID;
function_parameters: function_parameter_definition next_parameter*;
next_parameter: ',' function_parameter_definition;
CLASS: 'class';
ELSE: 'else';
IF: 'if';
INT: 'int';
NEW: 'new';
RETURN: 'return';
STRING: 'string';
SUPER: 'super';
THIS: 'this';
VOID: 'void';
WHILE: 'while';
LE: '<';
LEQ: '<=';
GT: '>';
GTQ: '>=';
MINUS: '-';
LOGICAL_EQUAL: '==';
LOGICAL_NEQUAL: '!=';
LOGICAL_AND: '&&';
LOGICAL_OR: '||';
INTEGER_LITERAL: ([1-9] DIGIT* | '0');
ID: (CILETTER | UNDERSCORE)(CILETTER | UNDERSCORE | DIGIT)*;
STRING_LITERAL: '"' STRING_CHARACTER* '"';
/* Lexemes to ignore */
WHITE_SPACE: [ \t\r\n]+ -> skip;
LINE_COMMENT: '//' .*? '\r' ? '\n' -> skip;
BLOCK_COMMENT: '/*' .*? '*/' -> skip;
fragment CILETTER: [a-zA-Z]; // Case In-sensitive letter
fragment UNDERSCORE: [_];
fragment DIGIT: [0-9];
fragment OCTAL_DIGIT: [0-7];
fragment HEXADECIMAL_DIGIT: [0-9A-Fa-f];
fragment PRINTABLE_CHARACTER: [ !#-~];
fragment STRING_CHARACTER: (PRINTABLE_CHARACTER | ESCAPE_SEQUENCE);
fragment ESCAPE_SEQUENCE
: SIMPLE_ESCAPE_SEQUENCE
| UTF8_ESCAPE_SEQUENCE;
// | OCTAL_ESCAPE_SEQUENCE
// | HEXA_ESCAPE_SEQUENCE;
fragment SIMPLE_ESCAPE_SEQUENCE: BACKSLASH [nt\\"?];
fragment UTF8_ESCAPE_SEQUENCE: BACKSLASH HEXADECIMAL_DIGIT HEXADECIMAL_DIGIT HEXADECIMAL_DIGIT HEXADECIMAL_DIGIT HEXADECIMAL_DIGIT HEXADECIMAL_DIGIT;
fragment BACKSLASH: '\\';