-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimplePascal.g4
90 lines (66 loc) · 1.79 KB
/
SimplePascal.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
grammar SimplePascal;
@header {
package ru.ifmo.ctddev.tenishchev.compiler;
}
program : nameOfProg defs block '.';
nameOfProg : 'program' PROGNAME SEMICOLON;
defs : varDef?;
varDef : 'var' variables SEMICOLON (variables SEMICOLON)*;
variables : variable (',' variable)* ':' type;
type : 'longint';
block : 'begin' line SEMICOLON? (SEMICOLON line SEMICOLON?)* 'end';
line : ifStatement
| forLoop
| whileLoop
| assigment
| block
| writeln
| brk;
assigment : VAR ':=' expressionArithmetic;
forLoop : 'for' assigment side expressionArithmetic doWord line; // for i := 1 to i do
whileLoop : 'while' expressionLogic doWord line;
ifStatement : 'if' expressionLogic thenWord line;
writeln : 'writeln' arguments;
brk : 'break' SEMICOLON;
expressionLogic : expressionCompare;
expressionCompare : expressionArithmetic op expressionArithmetic;
expressionArithmetic : expressionValueEmbeded ((plus | minus | modWord | divWord) expressionValueEmbeded)?;
expressionValueEmbeded : variable
| number
;
op : less | lessOrEq | great | greatOrEq | equals | notEq;
variable : VAR;
number : INT;
side : to | downto ;
to : TO;
downto : DOWNTO;
doWord : 'do';
thenWord : 'then';
modWord : 'mod';
divWord : 'div';
minus : MINUS;
plus : PLUS;
less : LESS_SIGN;
lessOrEq : LSEQ_SIGN;
great : GRET_SIGN;
greatOrEq : GREQ_SIGN;
equals : EQUALS;
notEq : NOTEQUALS;
arguments : LPAREN expressionArithmetic RPAREN;
PLUS : '+';
MINUS : '-';
EQUALS : '=';
LPAREN : '(';
RPAREN : ')';
SEMICOLON : ';';
LESS_SIGN : '<';
LSEQ_SIGN : '<=';
GRET_SIGN : '>';
GREQ_SIGN : '>=';
NOTEQUALS : '<>';
TO : 'to';
DOWNTO : 'downto';
VAR : [a-zA-Z_] [a-zA-Z0-9_]*;
PROGNAME : [a-zA-Z]+;
INT : ([1-9] [0-9]+) | [0-9];
WS : [ \t\r\n]+ -> skip;