-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.g
80 lines (67 loc) · 1.01 KB
/
grammar.g
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
Grammar lsbasi;
expr
: term ((MINUS | PLUS) term ) *
;
term
: factor ((MUL | REAL_DIV | INT_DIV ) factor ) *
;
factor
: INTEGER
| REAL
| RPARENT expr LPARENT
| PLUS factor
| MINUS factor
| ID
;
assignment_statement
: ID ASSIGN expr
;
empty:
;
statement
: assignment_statement
| compound_statement
| empty
;
program
: PROGRAM ID SEMI block DOT
;
block
: declarations compound_statement
;
declarations
: VAR (variable_declaration SEMI)+
| empty
;
variable_declaration
: ID (COMMA ID)* COLON TYPE
;
compound_statement
: BEGIN statement_list END
;
statement_list
: statement
| statement SEMI statement_list
;
TYPE_INTEGER: 'INTEGER'
TYPE_REAL: 'REAL'
PROGRAM: 'PROGRAM'
VAR: 'VAR'
BEGIN: 'BEGIN'
END: 'END'
MINUS: '-'
PLUS: '+'
MUL: '*'
REAL_DIV: '/'
INT_DIV: 'DIV'
RPARENT: '('
LPARENT: ')'
ASSIGN: ':='
DOT '.'
COMMA: ','
SEMI: ';'
COLON: ':'
REAL: [0-9]+(.[0-9]+)?
INTEGER: [0-9]+
ID: [a-zA-Z_][a-zA-Z0-9_]*
SPACES: [ \t\n\r]+ -> skip