-
Notifications
You must be signed in to change notification settings - Fork 0
/
grammar
131 lines (91 loc) · 4.38 KB
/
grammar
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// A grammar for j--, suitable for parsing by recursive descent
// Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
compilationUnit ::= [PACKAGE qualifiedIdentifier SEMI]
{IMPORT qualifiedIdentifier SEMI}
{typeDeclaration} EOF
qualifiedIdentifier ::= IDENTIFIER {DOT IDENTIFIER}
typeDeclaration ::= modifiers classDeclaration
modifiers ::= {PUBLIC | PROTECTED | PRIVATE | STATIC | ABSTRACT}
classDeclaration ::= CLASS IDENTIFIER
[EXTENDS qualifiedIdentifier]
classBody
classBody ::= LCURLY {modifiers memberDecl} RCURLY
memberDecl ::= IDENTIFIER // constructor
formalParameters block
| (VOID | type) IDENTIFIER // method
formalParameters
(block | SEMI)
| type variableDeclarators SEMI // field
block ::= LCURLY {blockStatement} RCURLY
blockStatement ::= localVariableDeclarationStatement
| statement
statement ::= block
| IF parExpression statement [ELSE statement]
| WHILE parExpression statement
| RETURN [expression] SEMI
| SEMI
| statementExpression SEMI
formalParameters ::= LPAREN
[formalParameter {COMMA formalParameter}]
RPAREN
formalParameter ::= type IDENTIFIER
parExpression ::= LPAREN expression RPAREN
localVariableDeclarationStatement ::= type variableDeclarators SEMI
variableDeclarators ::= variableDeclarator {COMMA variableDeclarator}
variableDeclarator ::= IDENTIFIER [ASSIGN variableInitializer]
variableInitializer ::= arrayInitializer | expression
arrayInitializer ::= LCURLY
[variableInitializer {COMMA variableInitializer}]
RCURLY
arguments ::= LPAREN [expression {COMMA expression}] RPAREN
type ::= referenceType | basicType
basicType ::= BOOLEAN | CHAR | INT
referenceType ::= basicType LBRACK RBRACK {LBRACK RBRACK}
| qualifiedIdentifier {LBRACK RBRACK}
statementExpression ::= expression // but must have side-effect, eg i++
expression ::= assignmentExpression
assignmentExpression ::= conditionalAndExpression // must be a valid lhs
[(ASSIGN | PLUS_ASSIGN) assignmentExpression]
conditionalAndExpression ::= bitwiseAndExpression // level 10
{LAND bitwiseAndExpression}
bitwiseAndExpression ::= equalityExpression // level 7
{BITWISE_AND equalityExpression}
equalityExpression ::= relationalExpression // level 6
{EQUAL relationalExpression}
relationalExpression ::= additiveExpression // level 5
[(GT | LE) additiveExpression
| INSTANCEOF referenceType]
additiveExpression ::= multiplicativeExpression // level 3
{(PLUS | MINUS) multiplicativeExpression}
multiplicativeExpression ::= unaryExpression // level 2
{(STAR | DIV) unaryExpression}
unaryExpression ::= INC unaryExpression // level 1
| MINUS unaryExpression
| simpleUnaryExpression
| BITWISE_COMP unaryExpression
simpleUnaryExpression ::= LNOT unaryExpression
| LPAREN basicType RPAREN unaryExpression //casted
| LPAREN // casted
referenceType
RPAREN simpleUnaryExpression
| postfixExpression
postfixExpression ::= primary {selector} {DEC}
selector ::= DOT qualifiedIdentifier [arguments]
| LBRACK expression RBRACK
primary ::= parExpression
| THIS [arguments]
| SUPER ( arguments
| DOT IDENTIFIER [arguments]
)
| literal
| NEW creator
| qualifiedIdentifier [arguments]
creator ::= (basicType | qualifiedIdentifier)
( arguments
| LBRACK RBRACK {LBRACK RBRACK} [arrayInitializer]
| newArrayDeclarator
)
newArrayDeclarator ::= LBRACK expression RBRACK
{LBRACK expression RBRACK} {LBRACK RBRACK}
literal ::= INT_LITERAL | CHAR_LITERAL | STRING_LITERAL
| TRUE | FALSE | NULL