mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-27 08:38:03 +00:00
add Decaf.g4
This commit is contained in:
parent
d75760192b
commit
04f571a678
69
src/main/antlr/Decaf.g4
Normal file
69
src/main/antlr/Decaf.g4
Normal file
@ -0,0 +1,69 @@
|
||||
grammar Decaf;
|
||||
|
||||
program : (class)+;
|
||||
|
||||
class : PUBLIC? 'class' id '{' (var | meth)* '}';
|
||||
|
||||
var : type id ';' | type id '=' expr';';
|
||||
returntype : type | VOID;
|
||||
type : INT | BOOL;
|
||||
|
||||
meth : PUBLIC? 'static'? returntype id '(' params? ')' block;
|
||||
params : param (',' param)*;
|
||||
param : type id;
|
||||
|
||||
block : '{' (var | stmt)* '}';
|
||||
|
||||
stmt : 'if' '(' expr ')' block ('else' block)? #If
|
||||
| 'for' '(' expr? ';' expr? ';' expr? ')' block #For
|
||||
| 'while' '(' expr ')' block #While
|
||||
| 'do' block 'while' '(' expr ')' #DoWhile
|
||||
| 'return' expr ';' #Return
|
||||
| 'return' ';' #ReturnVoid
|
||||
| 'break' ';' #Break
|
||||
| 'continue' ';' #Continue
|
||||
| stmtexpr #StatementExpressionstmt
|
||||
;
|
||||
|
||||
stmtexpr : id '=' expr ';' #Assign
|
||||
| methCall ';' #MethodCall
|
||||
| NEW type '(' params* ')' #New
|
||||
;
|
||||
|
||||
expr : expr binaryOp expr #BinaryOperation
|
||||
| literal #Constant
|
||||
| '(' expr ')' #Expression
|
||||
| methCall #MethodCallExpression
|
||||
| id #Identifier
|
||||
| stmtexpr #StatementExpressionexpr
|
||||
;
|
||||
|
||||
binaryOp : ADD | SUB | MUL;
|
||||
|
||||
|
||||
methCall : id '(' args? ')';
|
||||
args : expr (',' expr)*;
|
||||
|
||||
literal : NUMBER | boolean;
|
||||
|
||||
boolean : 'true' | 'false' ;
|
||||
|
||||
id : IDENTIFIER;
|
||||
|
||||
|
||||
PUBLIC : 'public';
|
||||
NEW : 'new';
|
||||
|
||||
SUB : '-';
|
||||
ADD : '+';
|
||||
MUL : '*';
|
||||
|
||||
INT : 'int';
|
||||
BOOL : 'bool';
|
||||
VOID : 'void';
|
||||
|
||||
IDENTIFIER : [a-zA-Z]+;
|
||||
NUMBER : [0-9]+;
|
||||
WS : [ \t\r\n] -> skip;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user