Es war einmal ein Compilerbau-Kurs
95585d357a
Vollständige Grammatik nach Stand der Vereinbarung letzten Termines
126 lines
4.2 KiB
ANTLR
126 lines
4.2 KiB
ANTLR
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;
|
|
|