add Decaf.g4

This commit is contained in:
Boolean-True 2024-04-24 15:36:08 +02:00
parent d75760192b
commit 04f571a678

69
src/main/antlr/Decaf.g4 Normal file
View 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;