From 95585d357a921f0ce061dbfd3f527895eac96184 Mon Sep 17 00:00:00 2001 From: Es war einmal ein Compilerbau-Kurs Date: Sun, 5 May 2024 21:09:45 +0000 Subject: [PATCH] Dateien nach "Source" hochladen MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Vollständige Grammatik nach Stand der Vereinbarung letzten Termines --- Source/Decaf.g4 | 125 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 Source/Decaf.g4 diff --git a/Source/Decaf.g4 b/Source/Decaf.g4 new file mode 100644 index 0000000..f40a9f8 --- /dev/null +++ b/Source/Decaf.g4 @@ -0,0 +1,125 @@ +grammar Decaf; + +program: classdecl+; + +//class identifier{...} +classdecl: (AccessModifierPrivate|AccessModifierPublic|AccessModifierProtected)? 'class' Identifier OpenCurlyBracket (constuctorDecl|fieldDecl|methodDecl)* ClosedCurlyBracket; +constuctorDecl: (AccessModifierPrivate|AccessModifierPublic|AccessModifierProtected)? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; //Maybe not needed + +//Method and FieldVar +methodDecl: MainMethodDecl block | (AccessModifierPrivate|AccessModifierPublic|AccessModifierProtected)? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; +fieldDecl: (AccessModifierPrivate|AccessModifierPublic|AccessModifierProtected)? type Identifier Semicolon; + +//Parameters +parameterList: parameter(Comma parameter)*; +parameter: type Identifier; + +//property, object.a, 3+1, a = 3 +expression: subExpression | binaryExpr; +//subExpression to dissolve left-recusion +subExpression: This | Identifier | instVar | value | stmtExpr | notExpr | OpenRoundBracket expression ClosedRoundBracket; +notExpr: Not expression; +assignableExpr: Identifier | instVar; +instVar: This Dot Identifier | (This Dot)? (Identifier Dot)+ Identifier; + +//.trim().toLength().toLowerCase().count ... +methodCall: receiver? receivingMethod* Identifier OpenRoundBracket argumentList ClosedRoundBracket; +argumentList: expression? | expression (Comma expression)*?; + +binaryExpr: calcExpr | nonCalcExpr; + +calcExpr: calcExpr LineOperator dotExpr | dotExpr; +dotExpr: dotExpr DotOperator dotSubExpr | dotSubExpr; +dotSubExpr: IntValue | Identifier | instVar | methodCall | OpenRoundBracket calcExpr ClosedRoundBracket; +nonCalcExpr: subExpression nonCalcOperator expression; +nonCalcOperator: LogicalOpertor | ComparisonOperator; + +//Statement but also expression +//a = expr, new Object(), method(param1) +stmtExpr: assign | newDecl | methodCall; + +//Statements +//int a, {...}, while(a > 10){...}, if(...){...} else if{...} else{...} +statement: returnStmt Semicolon | localVarDecl Semicolon | block | whileStmt | ifElseStmt | stmtExpr Semicolon; +returnStmt: Return (expression)?; +localVarDecl: type Identifier (Assign expression)?; +block: OpenCurlyBracket statement* ClosedCurlyBracket; +whileStmt: While OpenRoundBracket expression ClosedRoundBracket block; +ifElseStmt: ifStmt elseStmt?; +ifStmt: If OpenRoundBracket expression ClosedRoundBracket statement; +elseStmt: Else statement; +assign: assignableExpr Assign expression; +newDecl: New Identifier OpenRoundBracket argumentList ClosedRoundBracket; +receiver: ((This | instVar | newDecl | Identifier) Dot); +receivingMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot; +type: Int | Boolean | Char | Identifier; +value: IntValue | BooleanValue | CharValue | NullValue; + +//Access modifier +AccessModifierPublic : 'public' ; +AccessModifierPrivate : 'private'; +AccessModifierProtected : 'protected'; +MainMethodDecl : 'public static void main(String[] args)'; + +//Types +Void : 'void'; +Int : 'int'; +Boolean : 'bool'; +Char : 'char'; + +//Operators +DotOperator : Multipilkation | Division | Modulo; +LineOperator : Plus | Minus; +ComparisonOperator : Greater | Less | GreaterEqual | LessEqual | Equal | NotEqual; +LogicalOpertor : And | Or; + +Assign : '='; +Minus : '-'; +Plus : '+'; +Multipilkation : '*'; +Division : '/'; +Modulo : '%'; +Greater : '>'; +Less : '<'; +GreaterEqual : '>='; +LessEqual : '<='; +Equal : '=='; +NotEqual : '!='; +Not : '!'; +And : '&&'; +Or : '||'; + +//Symbols +Dot : '.'; +OpenRoundBracket : '('; +ClosedRoundBracket : ')'; +OpenCurlyBracket : '{'; +ClosedCurlyBracket : '}'; +Semicolon : ';'; +Comma : ','; + +//Keywords +Class : 'class'; +This : 'this'; +While : 'while'; +If : 'if'; +Else : 'else'; +For : 'for'; +Return : 'return'; +New : 'new'; + +//Identifier +fragment Alpabetic : [a-zA-Z]; +fragment Numeric: [0-9]; +fragment ValidIdentSymbols : Alpabetic|Numeric|'$'|'_'; +Identifier: Alpabetic ValidIdentSymbols*; + +//Values +IntValue : ('+'|'-')*[0-9]+; +CharValue: '\''~[\r\n]?'\''; +BooleanValue: 'true'|'false'; +NullValue: 'null'; + +//Whitespace? Right into the trash it gooeesss +WS : [ \t\r\n] -> skip; +