From 04f571a678a5b3eac0511c2518765c9104b6da59 Mon Sep 17 00:00:00 2001 From: Boolean-True Date: Wed, 24 Apr 2024 15:36:08 +0200 Subject: [PATCH] add Decaf.g4 --- src/main/antlr/Decaf.g4 | 69 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/main/antlr/Decaf.g4 diff --git a/src/main/antlr/Decaf.g4 b/src/main/antlr/Decaf.g4 new file mode 100644 index 0000000..07ac5d6 --- /dev/null +++ b/src/main/antlr/Decaf.g4 @@ -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; + +