Changes to the Grammar

This commit is contained in:
David Mueller 2024-05-09 16:08:07 +02:00
parent 1cd4d87a94
commit 9166d87afd
3 changed files with 34 additions and 31 deletions

4
.idea/compiler.xml generated
View File

@ -6,8 +6,10 @@
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="NichtHaskell" />
</profile>
</annotationProcessing>
<bytecodeTargetLevel>
<module name="NichtHaskell" target="20" />
</bytecodeTargetLevel>
</component>
</project>

1
.idea/misc.xml generated
View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">

View File

@ -3,32 +3,33 @@ grammar Decaf;
program: classdecl+;
//class identifier{...}
classdecl: AccessModifierPublic? 'class' Identifier OpenCurlyBracket (constuctorDecl|fieldDecl|methodDecl)*(MainMethodDecl block)? ClosedCurlyBracket;
constuctorDecl: AccessModifierPublic? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block; //Method without
classdecl: AccessModifierPublic? 'class' Identifier OpenCurlyBracket (constuctorDecl|localVarDecl|methodDecl)*(MainMethodDecl block)? ClosedCurlyBracket;
constuctorDecl: AccessModifierPublic? Identifier OpenRoundBracket parameterList? ClosedRoundBracket block;
//Method and FieldVar
methodDecl: AccessModifierPublic? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket block;
fieldDecl: AccessModifierPublic? type Identifier Semicolon;
//Parameters
parameterList: parameter(Comma parameter)*;
parameter: type Identifier;
argumentList: expression? | expression (Comma expression)+;
//property, object.a, 3+1, a = 3
expression: subExpression | binaryExpr;
//subExpression to dissolve left-recusion
subExpression: This | assignableExpr | stmtExpr | OpenRoundBracket subExpression ClosedRoundBracket;
assignableExpr: Identifier | instVar;
instVar: subReceiver? receivingMethod* Identifier;
//.trim().toLength().toLowerCase().count ...
methodCall: receiver? receivingMethod* Identifier OpenRoundBracket argumentList ClosedRoundBracket;
argumentList: expression? | expression (Comma expression)+;
statement: returnStmt Semicolon | localVarDecl Semicolon | block | whileStmt | ifElseStmt | stmtExpr Semicolon;
stmtExpr: assign | newDecl | methodCall;
assignableExpr: Identifier | instVar;
subReceiver: ((This | newDecl | Identifier) Dot);
receiver: ((This | instVar | newDecl | Identifier) Dot);
receivingMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot;
instVar: subReceiver? receivingMethod* Identifier;
binaryExpr: calcExpr | nonCalcExpr| value | Not binaryExpr;
@ -38,15 +39,11 @@ dotSubExpr: IntValue | Identifier | instVar | methodCall | OpenRoundBracket calc
nonCalcExpr: subExpression nonCalcOperator expression;
nonCalcOperator: LogicalOpertor | ComparisonOperator;
//Statement but also expression
//a = expr, new Object(), method(param1)
stmtExpr: assign | newDecl | methodCall;
BooleanValue: 'true'|'false';
NullValue: 'null';
//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)?;
localVarDecl: AccessModifierPublic? type Identifier (Assign expression)? Semicolon;
block: OpenCurlyBracket statement* ClosedCurlyBracket;
whileStmt: While OpenRoundBracket expression ClosedRoundBracket statement;
ifElseStmt: ifStmt elseStmt?;
@ -54,6 +51,8 @@ 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;
@ -63,7 +62,11 @@ value: IntValue | BooleanValue | CharValue | NullValue;
AccessModifierPublic : 'public' ;
MainMethodDecl : 'public static void main(String[] args)';
//Types
Void : 'void';
Int : 'int';
Boolean : 'bool';
Char : 'char';
//Operators
DotOperator : Multipilkation | Division | Modulo;
@ -105,24 +108,23 @@ Else : 'else';
Return : 'return';
New : 'new';
//Values
IntValue : ('+'|'-')*[0-9]+;
CharValue: '\''~[\r\n]?'\'';
//Identifier
fragment Alpabetic : [a-zA-Z];
fragment Numeric: [0-9];
fragment ValidIdentSymbols : Alpabetic|Numeric|'$'|'_';
Identifier: Alpabetic ValidIdentSymbols*;
//Types
Void : 'void';
Int : 'int';
Boolean : 'bool';
Char : 'char';
//Values
IntValue : ('+'|'-')*[0-9]+;
CharValue: '\''~[\r\n]?'\'';
BooleanValue: 'true'|'false';
NullValue: 'null';
//Whitespace? Right into the trash it gooeesss
WS : [ \t\r\n] -> skip;