-
Notifications
You must be signed in to change notification settings - Fork 10
/
Pascal.g4
135 lines (105 loc) · 2.23 KB
/
Pascal.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
grammar Pascal;
program:
('program' ID SEMI)? variableDeclarationPart blockStatement DOT;
variableDeclarationPart:
'var' variableDeclaration (SEMI variableDeclaration)* SEMI;
variableDeclaration:
identifierList COLON varType;
identifierList:
ID (COMMA ID)*;
varType:
'byte'
| 'longint'
| 'integer'
| 'int64'
| 'real'
| 'string'
| 'boolean'
;
blockStatement:
'begin' statements SEMI? 'end';
statements:
statement (SEMI statement)*;
statement:
blockStatement
| assignmentStatement
| ifStatement
| whileStatement
| forStatement
| breakStatement
| writelnReadln
| readln
| writeln
| write
| inc
;
writelnReadln:
'writeln' LPAREN CONST_STR RPAREN SEMI
'readln' LPAREN ID RPAREN;
readln:
('read' | 'readln') LPAREN identifierList RPAREN;
writeln:
'writeln' LPAREN expressions RPAREN;
write:
'write' LPAREN expressions RPAREN;
inc:
'inc' LPAREN ID RPAREN;
assignmentStatement:
ID ASSIGN expression;
expressions:
expression (COMMA expression)*;
expression:
( LPAREN expression RPAREN
| MINUS? CONST_INT
| CONST_STR
| ID
| ID LPAREND expression RPAREND
) (operators expression)*;
operators:
EQUAL | NOT_EQUAL | LT | LE | GE | GT | OR | AND | DIV | MOD | PLUS | MINUS | STAR | SLASH;
ifStatement:
'if' expression 'then' blockOrFakeBlock elseStatement?;
elseStatement:
'else' blockOrFakeBlock;
whileStatement:
'while' expression 'do' blockOrFakeBlock;
forStatement:
'for' ID ASSIGN expression 'to' expression 'do' blockOrFakeBlock;
breakStatement:
'break';
blockOrFakeBlock:
blockStatement
| fakeblockStatement
;
// нужно для правильных отступов в питоне
fakeblockStatement:
statement;
SEMI: ';';
COLON: ':';
COMMA: ',';
DOT: '.';
LPAREN: '(';
RPAREN: ')';
LPAREND: '[';
RPAREND: ']';
ASSIGN: ':=';
EQUAL: '=';
NOT_EQUAL: '<>';
LT: '<';
LE: '<=';
GE: '>=';
GT: '>';
OR: 'or';
AND: 'and';
PLUS: '+';
MINUS: '-';
STAR: '*';
SLASH: '/';
DIV: '//';
MOD: '%';
ID: [a-zA-Z][a-zA-Z0-9_]*;
CONST_INT: [0-9]+;
CONST_STR: '\'' ('\'\'' | ~ ('\''))* '\'';
WS: [ \t\r\n]+ -> skip;
COMMENT1: '(*' .*? '*)' -> skip;
COMMENT2: '{' .*? '}' -> skip;