-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.grammar
More file actions
77 lines (61 loc) · 1.68 KB
/
Copy pathpath.grammar
File metadata and controls
77 lines (61 loc) · 1.68 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
%class "Parser";
%terminals SEMI, COMMA, DOT;
%terminals CLASS, PUBLIC, STATIC, VOID, MAIN, NEW, EXTENDS;
%terminals PRINT, LENGTH;
%terminals PLUS, MINUS, TIMES;
%terminals LPAREN, RPAREN, LBRACK, RBRACK, LBRACE, RBRACE;
%terminals IF, ELSE, WHILE;
%terminals TRUE, FALSE, NOT, AND;
%terminals RETURN, THIS;
%terminals STRING, INT, BOOLEAN;
%terminals EQUALS, LESS;
%terminals INT_LIT; // our scanner provides numbers as integers
%terminals ID;
%left DOT;
%left LBRACK;
%left AND;
%left PLUS, MINUS;
%left TIMES;
%left LESS;
%left ID;
%goal Program;
Program = MainClass ClassDecl*;
MainClass = CLASS ID LBRACE PUBLIC STATIC VOID MAIN LPAREN STRING LBRACK RBRACK ID RPAREN LBRACE Statement RBRACE RBRACE;
ClassDecl = CLASS ID LBRACE VarDecl* MethodDecl* RBRACE
| CLASS ID EXTENDS ID LBRACE VarDecl* MethodDecl* RBRACE;
VarDecl = Type ID;
MethodDecl = PUBLIC Type ID LPAREN FormalList RPAREN LBRACE VarDecl* Statement* RETURN Exp SEMI RBRACE;
FormalList = Type ID FormalRest*
|;
FormalRest = COMMA Type ID;
Type = INT LBRACK RBRACK
| BOOLEAN
| INT
| ID;
Statement = LBRACE Statement* RBRACE
| IF LPAREN Exp RPAREN Statement ELSE Statement
| WHILE LPAREN Exp RPAREN Statement
| PRINT LPAREN Exp RPAREN SEMI
| ID EQUALS Exp SEMI
| ID LBRACK Exp RBRACK EQUALS Exp SEMI;
Exp = Exp Op Exp
| Exp LBRACK Exp RBRACK
| Exp DOT LENGTH
| Exp DOT ID LPAREN ExpList RPAREN
| INT_LIT
| TRUE
| FALSE
| ID
| THIS
| NEW INT LBRACK Exp RBRACK
| NEW ID LPAREN RPAREN
| NOT Exp
| LPAREN Exp RPAREN;
ExpList = Exp ExpRest*
|;
ExpRest = COMMA Exp;
Op = TIMES
| PLUS
| MINUS
| LESS
| AND;