Created
August 16, 2017 13:07
-
-
Save nokok/c3e0738faac9b99fc7546561658b55a5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
grammar MiniJava; | |
compileUnit | |
: mainClass classDecl* EOF | |
; | |
mainClass | |
: 'class' id '{' 'public' 'static' 'void' 'main' '(' 'String' '[]' Identifier ')' '{' statement '}' '}' | |
; | |
classDecl | |
: 'class' Identifier '{' varDecl* methodDecl* '}' | |
| 'class' Identifier 'extends' Identifier '{' varDecl* methodDecl* '}' | |
; | |
varDecl | |
: type Identifier ';' | |
; | |
methodDecl | |
: 'public' type Identifier '(' formalList ')' '{' varDecl* statement* 'return' exp ';' '}' | |
; | |
formalList | |
: (type Identifier formalRest*)? | |
; | |
formalRest | |
: ',' type Identifier | |
; | |
type | |
: 'int' '[]' | |
| 'boolean' | |
| 'int' | |
| Identifier | |
; | |
statement | |
: '{' statement* '}' | |
| 'if' '(' exp ')' statement 'else' statement | |
| 'while' '(' exp ')' statement | |
| 'System.out.println' '(' exp ')' ';' | |
| Identifier '=' exp ';' | |
| Identifier '[' exp ']' '=' exp ';' | |
; | |
exp | |
: exp Operator exp | |
| exp '[' exp ']' | |
| exp '.' 'length' | |
| exp '.' Identifier '(' expList ')' | |
| IntegerLiteral | |
| 'true' | |
| 'false' | |
| Identifier | |
| 'this' | |
| 'new' 'int' '[' exp ']' | |
| 'new' Identifier '(' ')' | |
| '!' exp | |
| '(' exp ')' | |
; | |
expList | |
: (exp expRest*)? | |
; | |
expRest | |
: ',' exp | |
; | |
id | |
: Identifier | |
; | |
IntegerLiteral | |
: Digit | |
; | |
Identifier | |
: Letter+ | |
; | |
Digit | |
: NonZeroDigit ('0' .. '9')* | |
; | |
fragment NonZeroDigit | |
: '1' .. '9' | |
; | |
Operator | |
: '&&' | |
| '<' | |
| '+' | |
| '-' | |
| '*' | |
; | |
fragment Letter | |
: 'a' .. 'z' | |
| 'A' .. 'Z' | |
; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment