Skip to content
This repository was archived by the owner on Oct 28, 2023. It is now read-only.

Commit 39c737f

Browse files
committed
continued refactoring of MiniJava grammar
adds more support for issue #18
1 parent f8f6720 commit 39c737f

File tree

5 files changed

+391
-109
lines changed

5 files changed

+391
-109
lines changed

assets/mini-java-1.bnf.png

81.1 KB
Loading
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
(* HPG Grammar for Mini Java *)
2+
(* https://github.com/christophevg/human-parser-generator/wiki/Tutorial-MiniJava *)
3+
4+
program = main-class { class-declaration } ;
5+
6+
main-class = "class" class-name @ identifier "{"
7+
"public" "static" "void" "main" "(" "String" "[" "]" args @ identifier ")" "{"
8+
statement
9+
"}"
10+
"}" ;
11+
12+
class-declaration = "class" class-name @ identifier [ "extends" extends @ identifier ] "{"
13+
{ var-declaration }
14+
{ method-declaration }
15+
"}" ;
16+
17+
var-declaration = type variable @ identifier ";" ;
18+
19+
method-declaration = "public" return-type @ type method-name @ identifier
20+
"(" [ type identifier { "," type identifier } ] ")" "{"
21+
{ var-declaration }
22+
{ statement }
23+
"return" returns @ expression ";"
24+
"}" ;
25+
26+
type = "int" "[" "]"
27+
| "boolean"
28+
| "int"
29+
| identifier
30+
;
31+
32+
statement = "{" { block-statement @ statement } "}"
33+
| "if" "(" condition @ expression ")"
34+
true-statement @ statement
35+
"else"
36+
false-statement @ statement
37+
| "while" "(" expression ")" repeated-statement @ statement
38+
| "System.out.println" "(" output @ expression ")" ";"
39+
| variable @ identifier "=" value @ expression ";"
40+
| indexed-variable @ identifier "[" index @ expression "]"
41+
"=" indexed-value @ expression ";"
42+
;
43+
44+
expression = postfix-expression
45+
| non-postfix-expression
46+
;
47+
48+
postfix-expression = indexed-expression
49+
| method-call-expression
50+
| length-expression
51+
;
52+
53+
indexed-expression = non-postfix-expression "[" expression "]" ;
54+
method-call-expression = non-postfix-expression "." identifier "(" [ expression { "," expression } ] ")" ;
55+
length-expression = non-postfix-expression "." "length" ;
56+
57+
non-postfix-expression = prefix-expression
58+
| non-prefix-expression
59+
;
60+
61+
prefix-expression = not-expression ;
62+
63+
not-expression = "!" prefix-expression ;
64+
65+
non-prefix-expression = times-expression
66+
| non-times-expression
67+
;
68+
69+
times-expression = non-times-expression "*" non-prefix-expression ;
70+
71+
non-times-expression = additive-expression
72+
| non-additive-expression
73+
;
74+
75+
additive-expression = non-additive-expression [ "+" | "-" ] non-times-expression ;
76+
77+
non-additive-expression = less-than-expression
78+
| non-less-than-expression
79+
;
80+
81+
less-than-expression = non-less-than-expression "<" non-additive-expression ;
82+
83+
non-less-than-expression = and-expression
84+
| non-and-expression
85+
;
86+
87+
and-expression = non-and-expression "&&" non-less-than-expression ;
88+
89+
90+
non-and-expression = group-expression
91+
| primary-expression
92+
;
93+
94+
group-expression = "(" expression ")" ;
95+
96+
primary-expression = integer
97+
| "true"
98+
| "false"
99+
| "this"
100+
| "new" "int" "[" size @ expression "]"
101+
| "new" class-name @ identifier "(" ")"
102+
| identifier
103+
;
104+
105+
identifier = ? /([A-Za-z_][A-Za-z0-9-_]*)/ ? ;
106+
integer = ? /(-?[1-9][0-9]*)/ ? ;

example/mini-java/mini-java.bnf

Lines changed: 131 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,131 @@
1-
Program ::= MainClass { ClassDeclaration } ;
2-
3-
MainClass ::= "class" Identifier "{"
4-
"public" "static" "void" "main" "(" "String" "[" "]" Identifier ")" "{"
5-
Statement
6-
"}"
7-
"}" ;
8-
9-
ClassDeclaration ::= "class" Identifier [ "extends" Identifier ] "{"
10-
{ VarDeclaration }
11-
{ MethodDeclaration }
12-
"}" ;
13-
14-
VarDeclaration ::= Type Identifier ";" ;
15-
16-
MethodDeclaration ::= "public" Type Identifier "(" [ Type Identifier { "," Type Identifier } ] ")" "{"
17-
{ VarDeclaration }
18-
{ Statement }
19-
"return" Expression ";"
20-
"}" ;
21-
22-
Type ::= ( "int" "[" "]" )
23-
| "boolean"
24-
| "int"
25-
| Identifier
26-
;
27-
28-
Statement ::= ( "{" { Statement } "}" )
29-
| ( "if" "(" Expression ")" Statement "else" Statement )
30-
| ( "while" "(" Expression ")" Statement )
31-
| ( "System.out.println" "(" Expression ")" ";" )
32-
| ( Identifier "=" Expression ";" )
33-
| ( Identifier "[" Expression "]" "=" Expression ";" )
34-
;
35-
36-
Expression ::= AndExpression ;
37-
AndExpression ::= LessThanExpression { "&&" LessThanExpression } ;
38-
LessThanExpression ::= AdditiveExpression [ "<" AdditiveExpression ] ;
39-
AdditiveExpression ::= TimesExpression { [ "+" | "-" ] TimesExpression } ;
40-
TimesExpression ::= PrefixExpression { "*" PrefixExpression } ;
41-
PrefixExpression ::= NotExpression | PostfixExpression;
42-
NotExpression ::= "!" { "!" } PostfixExpression ;
43-
PostfixExpression ::= PrimaryExpression { ( "[" Expression "]" )
44-
| ( "." Identifier "(" [ Expression { "," Expression } ] ")" )
45-
| ( "." "length" ) } ;
46-
PrimaryExpression ::= Integer
47-
| "true"
48-
| "false"
49-
| "this"
50-
| ( "(" Expression ")" )
51-
| ( "new" "int" "[" Expression "]" )
52-
| ( "new" Identifier "(" ")" )
53-
| Identifier
54-
;
55-
56-
Identifier ::= ? /([A-Za-z_][A-Za-z0-9-_]*)/ ? ;
57-
Integer ::= ? /(-?[1-9][0-9]*)/ ? ;
1+
(* HPG Grammar for Mini Java *)
2+
(* https://github.com/christophevg/human-parser-generator/wiki/Tutorial-MiniJava *)
3+
4+
program = main-class { class-declaration } ;
5+
6+
main-class = "class" class-name @ identifier "{"
7+
"public" "static" "void" "main" "(" "String" "[" "]" args @ identifier ")" "{"
8+
statement
9+
"}"
10+
"}" ;
11+
12+
class-declaration = "class" class-name @ identifier [ "extends" extends @ identifier ] "{"
13+
{ var-declaration }
14+
{ method-declaration }
15+
"}" ;
16+
17+
var-declaration = type variable @ identifier ";" ;
18+
19+
method-declaration = "public" return-type @ type method-name @ identifier
20+
"(" [ type identifier { "," type identifier } ] ")" "{"
21+
{ var-declaration }
22+
{ statement }
23+
"return" returns @ expression ";"
24+
"}" ;
25+
26+
type = primitive-type
27+
| list-type
28+
| complex-type
29+
;
30+
31+
primitive-type = integer-type
32+
| boolean-type
33+
;
34+
35+
integer-type = "int" ;
36+
boolean-type = "boolean" ;
37+
38+
list-type = integer-type "[" "]";
39+
40+
complex-type = type @ identifier ;
41+
42+
statement = block-statement
43+
| if-statement
44+
| while-statement
45+
| print-statement
46+
| assignment-statement
47+
;
48+
49+
block-statement = "{" { statement } "}" ;
50+
if-statement = "if" "(" condition @ expression ")"
51+
true-statement @ statement
52+
"else"
53+
false-statement @ statement
54+
;
55+
while-statement = "while" "(" expression ")" statement ;
56+
print-statement = "System.out.print" [ "ln" ] "(" expression ")" ";" ;
57+
assignment-statement = variable @ identifier [ "[" index @ expression "]" ]
58+
"=" expression ";"
59+
;
60+
61+
expression = postfix-expression
62+
| non-postfix-expression
63+
;
64+
65+
postfix-expression = indexed-expression
66+
| method-call-expression
67+
| length-expression
68+
;
69+
70+
indexed-expression = object @ non-postfix-expression "[" expression "]" ;
71+
method-call-expression = object @ non-postfix-expression "." identifier
72+
"(" [ argument @ expression { "," argument @ expression } ] ")" ;
73+
length-expression = object @ non-postfix-expression "." "length" ;
74+
75+
non-postfix-expression = prefix-expression
76+
| non-prefix-expression
77+
;
78+
79+
prefix-expression = not-expression ;
80+
81+
not-expression = "!" prefix-expression ;
82+
83+
non-prefix-expression = times-expression
84+
| non-times-expression
85+
;
86+
87+
times-expression = non-times-expression "*" non-prefix-expression ;
88+
89+
non-times-expression = additive-expression
90+
| non-additive-expression
91+
;
92+
93+
additive-expression = non-additive-expression [ "+" | "-" ] non-times-expression ;
94+
95+
non-additive-expression = less-than-expression
96+
| non-less-than-expression
97+
;
98+
99+
less-than-expression = non-less-than-expression "<" non-additive-expression ;
100+
101+
non-less-than-expression = and-expression
102+
| non-and-expression
103+
;
104+
105+
and-expression = non-and-expression "&&" non-less-than-expression ;
106+
107+
108+
non-and-expression = group-expression
109+
| primary-expression
110+
;
111+
112+
group-expression = "(" expression ")" ;
113+
114+
primary-expression = integer-expression
115+
| boolean-expression
116+
| this-expression
117+
| constructor-expression
118+
| identifier-expression
119+
;
120+
121+
integer-expression = value @ integer ;
122+
boolean-expression = value @ boolean ;
123+
this-expression = "this" ;
124+
constructor-expression = "new" "int" "[" size @ expression "]"
125+
| "new" class-name @ identifier "(" ")"
126+
;
127+
identifier-expression = name @ identifier ;
128+
129+
identifier = ? /([A-Za-z_][A-Za-z0-9-_]*)/ ? ;
130+
integer = ? /(-?[1-9][0-9]*)/ ? ;
131+
boolean = ? /(true|false)/ ? ;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
(* HPG Grammar for Mini Java *)
2+
(* https://github.com/christophevg/human-parser-generator/wiki/Tutorial-MiniJava *)
3+
4+
program = main-class { class-declaration } ;
5+
6+
main-class = "class" identifier "{"
7+
"public" "static" "void" "main" "(" "String" "[" "]" identifier ")" "{"
8+
statement
9+
"}"
10+
"}" ;
11+
12+
class-declaration = "class" identifier [ "extends" identifier ] "{"
13+
{ var-declaration }
14+
{ method-declaration }
15+
"}" ;
16+
17+
var-declaration = type identifier ";" ;
18+
19+
method-declaration = "public" type identifier
20+
"(" [ type identifier { "," type identifier } ] ")" "{"
21+
{ var-declaration }
22+
{ statement }
23+
"return" expression ";"
24+
"}" ;
25+
26+
type = "int" "[" "]"
27+
| "boolean"
28+
| "int"
29+
| identifier
30+
;
31+
32+
statement = "{" { statement } "}"
33+
| "if" "(" expression ")" statement "else" statement
34+
| "while" "(" expression ")" statement
35+
| "System.out.println" "(" expression ")" ";"
36+
| identifier "=" expression ";"
37+
| identifier "[" expression "]" "=" expression ";"
38+
;
39+
40+
expression = postfix-expression
41+
| non-postfix-expression
42+
;
43+
44+
postfix-expression = indexed-expression
45+
| method-call-expression
46+
| length-expression
47+
;
48+
49+
indexed-expression = non-postfix-expression "[" expression "]" ;
50+
method-call-expression = non-postfix-expression "." identifier "(" [ expression { "," expression } ] ")" ;
51+
length-expression = non-postfix-expression "." "length" ;
52+
53+
non-postfix-expression = prefix-expression
54+
| non-prefix-expression
55+
;
56+
57+
prefix-expression = not-expression ;
58+
59+
not-expression = "!" prefix-expression ;
60+
61+
non-prefix-expression = times-expression
62+
| non-times-expression
63+
;
64+
65+
times-expression = non-times-expression "*" non-prefix-expression ;
66+
67+
non-times-expression = additive-expression
68+
| non-additive-expression
69+
;
70+
71+
additive-expression = non-additive-expression [ "+" | "-" ] non-times-expression ;
72+
73+
non-additive-expression = less-than-expression
74+
| non-less-than-expression
75+
;
76+
77+
less-than-expression = non-less-than-expression "<" non-additive-expression ;
78+
79+
non-less-than-expression = and-expression
80+
| non-and-expression
81+
;
82+
83+
and-expression = non-and-expression "&&" non-less-than-expression ;
84+
85+
86+
non-and-expression = group-expression
87+
| primary-expression
88+
;
89+
90+
group-expression = "(" expression ")" ;
91+
92+
primary-expression = integer
93+
| "true"
94+
| "false"
95+
| "this"
96+
| "new" "int" "[" expression "]"
97+
| "new" identifier "(" ")"
98+
| identifier
99+
;
100+
101+
identifier = ? /([A-Za-z_][A-Za-z0-9-_]*)/ ? ;
102+
integer = ? /(-?[1-9][0-9]*)/ ? ;

0 commit comments

Comments
 (0)