-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforth.g4
More file actions
53 lines (41 loc) · 982 Bytes
/
forth.g4
File metadata and controls
53 lines (41 loc) · 982 Bytes
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
grammar forth;
// Regla principal
root
: (statement | wordDef)* EOF
;
// Statements bàsics
statement
: NUM # pushNumber
| word # wordCall
;
word
: ID | SYMBOL
;
wordDef
: ':' ID block ';'
;
block
: (statement | conditional | recurse)*
;
conditional
: 'if' block ('else' block)? 'endif'
;
recurse
: 'recurse'
;
// Tokens lexics
NUM : '-'? [0-9]+ ; // nombres enters
ID // identificadors de words (built-ins o definits per l'usuari)
: [a-zA-Z_][a-zA-Z_0-9-]* // dup, dup2, my-word
| [0-9]+ [a-zA-Z_][a-zA-Z_0-9-]* // 2dup, 3dup, 10swap
;
SYMBOL // simbols d'operadors
: ('.' | '.s')
| ('+'|'-'|'*'|'/'|'mod')
| ('<'|'>'|'='|'<>'|'<='|'>=')
;
WS : [ \t\r\n]+ -> skip ; // whitespaces
// Comentaris
COMMENT : '(' ~[)]* ')' -> skip ;
// Capturem qualsevol símbol no reconegut per la gramàtica
LEXICAL_ERROR : . ;