-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeatherweightJavaScript.g4
More file actions
80 lines (64 loc) · 2.32 KB
/
Copy pathFeatherweightJavaScript.g4
File metadata and controls
80 lines (64 loc) · 2.32 KB
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 FeatherweightJavaScript;
@header { package edu.sjsu.fwjs.parser; }
// Reserved words
IF : 'if' ;
ELSE : 'else' ;
WHILE : 'while' ;
FUNCTION : 'function' ;
VAR : 'var' ;
PRINT : 'print' ;
// Literals
INT : [1-9][0-9]* | '0' ;
BOOL : 'true' | 'false' ;
NULL : 'null' ;
// Symbols
MUL : '*' ;
DIV : '/' ;
ADD : '+' ;
SUB : '-' ;
MOD : '%' ;
GT : '>' ;
LT : '<' ;
GE : '>=' ;
LE : '<=' ;
EQ : '==' ;
ASSIGN : '=' ;
SEPARATOR : ';' ;
// Identifier: first [A-Za-z_] then [A-Za-z_0-9]*
ID : [A-Za-z_] [A-Za-z_0-9]* ;
// Whitespace and comments
NEWLINE : '\r'? '\n' -> skip ;
BLOCK_COMMENT : '/*' .*? '*/' -> skip ;
LINE_COMMENT : '//' ~[\n\r]* -> skip ;
WS : [ \t]+ -> skip ; // ignore whitespace
// ***Parsing rules ***
/** The start rule */
prog: stat+ ;
stat: expr SEPARATOR # bareExpr
| IF '(' expr ')' block ELSE block # ifThenElse
| IF '(' expr ')' block # ifThen
| WHILE '(' expr ')' stat # while
| PRINT '(' expr ')' SEPARATOR # print
| '{' stat* '}' # blockExpr
;
block: '{' stat* '}' # fullBlock
| stat # simpBlock
;
expr: '(' expr ')' # parens
| expr '(' argList? ')' # call
| FUNCTION '(' paramList? ')' block # func
| INT # int
| BOOL # bool
| NULL # null
| ID # id
| '{' stat* '}' # blockVal
| expr op=( '*' | '/' | '%' ) expr # MulDivMod
| expr op=('+' | '-') expr # AddSub
| expr op=(LT | LE | GT | GE | EQ) expr # Compare
| ID ASSIGN expr # assign
| VAR ID (ASSIGN expr)? # varDecl
;
paramList: ID (',' ID)*
;
argList: expr (',' expr)*
;