working version <3
This commit is contained in:
85
ExprLexer.g4
85
ExprLexer.g4
@@ -1,32 +1,22 @@
|
|||||||
lexer grammar ExprLexer;
|
lexer grammar ExprLexer;
|
||||||
|
|
||||||
// UNICODE --------------------------------------------------------------------------------------------------------------
|
|
||||||
UnicodeInputCharacter: UnicodeEscape | RawInputCharacter;
|
|
||||||
|
|
||||||
fragment UnicodeEscape: '\\' UnicodeMarker HexDigit HexDigit HexDigit HexDigit;
|
|
||||||
|
|
||||||
fragment UnicodeMarker: 'u'+;
|
|
||||||
|
|
||||||
|
|
||||||
fragment RawInputCharacter: .;
|
|
||||||
//not complete need to ask question from prof.
|
|
||||||
|
|
||||||
// LINE TERMINATORS -----------------------------------------------------------------------------------------------------------------------
|
// LINE TERMINATORS -----------------------------------------------------------------------------------------------------------------------
|
||||||
LineTerminator: '\n' | '\r' | '\r\n';
|
fragment LineTerminator: '\r' '\n'? | '\n';
|
||||||
|
|
||||||
InputCharacter: [^\n\r];
|
|
||||||
|
|
||||||
//WHITE SPACE ----------------------------------------------------------------------------------------------------------------------------------------
|
//WHITE SPACE ----------------------------------------------------------------------------------------------------------------------------------------
|
||||||
WhiteSpace: (' ' | '\t' | '\f' | LineTerminator) -> skip;
|
WhiteSpace: (' ' | '\t' | '\f' | LineTerminator) -> skip;
|
||||||
|
|
||||||
//COMMENTS --------------------------------------------------------------------------------------------------------------------------------------------------
|
//COMMENTS --------------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
|
fragment EndOfLineComment: '//' ~('\r' | '\n')* LineTerminator?;
|
||||||
|
|
||||||
|
|
||||||
Comment: (TraditionalComment | EndOfLineComment) -> skip;
|
Comment: (TraditionalComment | EndOfLineComment) -> skip;
|
||||||
|
|
||||||
fragment TraditionalComment:
|
fragment TraditionalComment:
|
||||||
'/*' NotStar CommentTail;
|
'/*' NotStar CommentTail;
|
||||||
|
|
||||||
fragment EndOfLineComment:
|
//fragment EndOfLineComment:
|
||||||
'//' CharactersInLine? LineTerminator;
|
// '//' CharactersInLine? LineTerminator;
|
||||||
|
|
||||||
fragment CommentTail: '*' CommentTailStar | NotStar ;
|
fragment CommentTail: '*' CommentTailStar | NotStar ;
|
||||||
|
|
||||||
@@ -39,10 +29,22 @@ fragment NotStar:
|
|||||||
fragment NotStarNotSlash:
|
fragment NotStarNotSlash:
|
||||||
[^*/] | LineTerminator;
|
[^*/] | LineTerminator;
|
||||||
|
|
||||||
fragment CharactersInLine: InputCharacter
|
//fragment CharactersInLine: InputCharacter
|
||||||
| InputCharacter;
|
// | InputCharacter;
|
||||||
|
|
||||||
|
|
||||||
|
//// UNICODE --------------------------------------------------------------------------------------------------------------
|
||||||
|
//UnicodeInputCharacter: UnicodeEscape | RawInputCharacter;
|
||||||
|
//
|
||||||
|
//fragment UnicodeEscape: '\\' UnicodeMarker HexDigit HexDigit HexDigit HexDigit;
|
||||||
|
//
|
||||||
|
//fragment UnicodeMarker: 'u'+;
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//fragment RawInputCharacter: .;
|
||||||
|
// //not complete need to ask question from prof.
|
||||||
|
//
|
||||||
|
//
|
||||||
|
//fragment InputCharacter: [^\n\r];
|
||||||
|
|
||||||
// KEYWORDS --------------------------------------------------------------------------------------------------------
|
// KEYWORDS --------------------------------------------------------------------------------------------------------
|
||||||
Abstract: 'abstract';
|
Abstract: 'abstract';
|
||||||
@@ -95,6 +97,7 @@ Void: 'void';
|
|||||||
Volatile: 'volatile';
|
Volatile: 'volatile';
|
||||||
While: 'while';
|
While: 'while';
|
||||||
|
|
||||||
|
Dot: '.';
|
||||||
// LITERALS -------------------------------------------------------------------------------------------------------------------
|
// LITERALS -------------------------------------------------------------------------------------------------------------------
|
||||||
IntegerLiteral: DecimalIntegerLiteral | HexIntegerLiteral | OctalIntegerLiteral;
|
IntegerLiteral: DecimalIntegerLiteral | HexIntegerLiteral | OctalIntegerLiteral;
|
||||||
|
|
||||||
@@ -195,32 +198,18 @@ SquareBracketLeft: '[';
|
|||||||
SquareBracketRight: ']';
|
SquareBracketRight: ']';
|
||||||
Semicolon: ';';
|
Semicolon: ';';
|
||||||
Comma: ',';
|
Comma: ',';
|
||||||
Dot: '.';
|
|
||||||
|
|
||||||
//OPERATORS ---------------------------------------------------------------------------------------------------------------------------------------
|
//OPERATORS ---------------------------------------------------------------------------------------------------------------------------------------
|
||||||
Assignment: '=';
|
UnsignedRightShiftAssign: '>>>=';
|
||||||
BitwiseComplement: '~';
|
|
||||||
LessThan: '<';
|
|
||||||
GreaterThan: '>';
|
|
||||||
LogicalComplement: '!';
|
|
||||||
Question: '?';
|
|
||||||
Colon: ':';
|
|
||||||
EqualTo: '==';
|
EqualTo: '==';
|
||||||
|
NotEqualTo: '!=';
|
||||||
LessThanEqualTo: '<=';
|
LessThanEqualTo: '<=';
|
||||||
GreaterThanEqualTo: '>=';
|
GreaterThanEqualTo: '>=';
|
||||||
NotEqualTo: '!=';
|
|
||||||
ConditionalAND: '&&';
|
ConditionalAND: '&&';
|
||||||
ConditionalOR: '||';
|
ConditionalOR: '||';
|
||||||
Increment: '++';
|
Increment: '++';
|
||||||
Decrement: '--';
|
Decrement: '--';
|
||||||
Addition: '+';
|
|
||||||
Subtraction: '-';
|
|
||||||
Multiplication: '*';
|
|
||||||
Division: '/';
|
|
||||||
BitwiseAND: '&';
|
|
||||||
BitwiseOR: '|';
|
|
||||||
BitwiseXOR: '^';
|
|
||||||
Remainder: '%';
|
|
||||||
LeftShift: '<<';
|
LeftShift: '<<';
|
||||||
SignedRightShift: '>>';
|
SignedRightShift: '>>';
|
||||||
UnsignedRightShift: '>>>';
|
UnsignedRightShift: '>>>';
|
||||||
@@ -234,14 +223,28 @@ BitwiseXORAssign: '^=';
|
|||||||
RemainderAssign: '%=';
|
RemainderAssign: '%=';
|
||||||
LeftShiftAssign: '<<=';
|
LeftShiftAssign: '<<=';
|
||||||
SignedRightShiftAssign: '>>=';
|
SignedRightShiftAssign: '>>=';
|
||||||
UnsignedRightShiftAssign: '>>>=';
|
Assignment: '=';
|
||||||
|
BitwiseComplement: '~';
|
||||||
|
LessThan: '<';
|
||||||
|
GreaterThan: '>';
|
||||||
|
LogicalComplement: '!';
|
||||||
|
Question: '?';
|
||||||
|
Colon: ':';
|
||||||
|
Addition: '+';
|
||||||
|
Subtraction: '-';
|
||||||
|
Multiplication: '*';
|
||||||
|
Division: '/';
|
||||||
|
BitwiseAND: '&';
|
||||||
|
BitwiseOR: '|';
|
||||||
|
BitwiseXOR: '^';
|
||||||
|
Remainder: '%';
|
||||||
|
|
||||||
|
|
||||||
//IDENTIFIERS -------------------------------------------------------------------------------------------------------------------------------------------
|
//IDENTIFIERS -------------------------------------------------------------------------------------------------------------------------------------------
|
||||||
IDENTIFIER: JavaLetter JavaLetterOrDigit;
|
|
||||||
|
|
||||||
|
IDENTIFIER: JavaLetter JavaLetterOrDigit*;
|
||||||
fragment JavaLetter:
|
fragment JavaLetter:
|
||||||
[a-zA-Z_$];
|
[a-zA-Z$_];
|
||||||
|
|
||||||
fragment JavaLetterOrDigit:
|
fragment JavaLetterOrDigit:
|
||||||
JavaLetter | [0-9];
|
[A-Za-z0-9$_];
|
||||||
|
|||||||
191
ExprSyntactic.g4
191
ExprSyntactic.g4
@@ -3,7 +3,7 @@ parser grammar ExprSyntactic;
|
|||||||
options {
|
options {
|
||||||
tokenVocab=ExprLexer;
|
tokenVocab=ExprLexer;
|
||||||
}
|
}
|
||||||
|
prog: compilationUnit EOF;
|
||||||
identifier: IDENTIFIER;
|
identifier: IDENTIFIER;
|
||||||
|
|
||||||
qualifiedIdentifier: identifier (Dot identifier)*;
|
qualifiedIdentifier: identifier (Dot identifier)*;
|
||||||
@@ -15,7 +15,41 @@ literal: IntegerLiteral
|
|||||||
| BooleanLiteral
|
| BooleanLiteral
|
||||||
| NullLiteral;
|
| NullLiteral;
|
||||||
|
|
||||||
expression: expression1 (assignmentOperator expression1)?;
|
expression: unaryExpr assignmentOperator expression | conditionalExpr ;
|
||||||
|
|
||||||
|
//Highest Prescedence _Expr = expression
|
||||||
|
//assignmentExpr: unaryExpr assignmentOperator assignmentExpr | conditionalExpr;
|
||||||
|
|
||||||
|
conditionalExpr: logicalOrExpr '?' conditionalExpr ':' conditionalExpr |logicalOrExpr;
|
||||||
|
|
||||||
|
logicalOrExpr: logicalAndExpr ('||' logicalAndExpr)*;
|
||||||
|
|
||||||
|
logicalAndExpr: equalityExpr ('&&' equalityExpr)*;
|
||||||
|
|
||||||
|
equalityExpr: relationalExpr (('==' | '!=') relationalExpr)*;
|
||||||
|
|
||||||
|
relationalExpr: additiveExpr(('<' | '>' | '<=' | '>=') additiveExpr | 'instanceof' type)*;
|
||||||
|
|
||||||
|
additiveExpr: multiplicativeExpr(('+' | '-') multiplicativeExpr)*;
|
||||||
|
multiplicativeExpr: unaryExpr(('*' | '/' | '%') unaryExpr)*;
|
||||||
|
|
||||||
|
unaryExpr:postfixExpr |('+' | '-' | '!' | '~' | '++' | '--') unaryExpr | parExpression type unaryExpr;
|
||||||
|
|
||||||
|
postfixExpr: primaryExpr ('++'| '--'| '.' IDENTIFIER | '[' expression ']' | arguments)*;
|
||||||
|
|
||||||
|
primaryExpr: parExpression
|
||||||
|
| IntegerLiteral
|
||||||
|
| FloatingPointLiteral
|
||||||
|
| StringLiteral
|
||||||
|
| CharacterLiteral
|
||||||
|
| BooleanLiteral
|
||||||
|
| NullLiteral
|
||||||
|
| IDENTIFIER
|
||||||
|
| 'this'
|
||||||
|
| 'super' ('.' IDENTIFIER)?
|
||||||
|
| 'new' type arguments
|
||||||
|
| primitiveType ('[' ']')* '.' 'class'
|
||||||
|
| type '.' 'class';
|
||||||
|
|
||||||
|
|
||||||
assignmentOperator: Assignment
|
assignmentOperator: Assignment
|
||||||
@@ -32,78 +66,78 @@ assignmentOperator: Assignment
|
|||||||
| UnsignedRightShiftAssign;
|
| UnsignedRightShiftAssign;
|
||||||
|
|
||||||
type: identifier (Dot identifier)* bracketsOpt
|
type: identifier (Dot identifier)* bracketsOpt
|
||||||
| basicType;
|
| primitiveType;
|
||||||
|
|
||||||
statementExpression: expression;
|
statementExpression: expression;
|
||||||
|
|
||||||
constantExpression: expression;
|
constantExpression: expression;
|
||||||
|
|
||||||
expression1: expression2 (expression1Rest)?;
|
//expression1: expression2 (expression1Rest)?;
|
||||||
|
//
|
||||||
|
//expression1Rest: Question expression Colon expression1;
|
||||||
|
//
|
||||||
|
//expression2: expression3 expression2Rest?;
|
||||||
|
//
|
||||||
|
//expression2Rest: (infixop expression3)+
|
||||||
|
// | InstanceOf type;
|
||||||
|
|
||||||
expression1Rest: Question expression Colon expression1;
|
//infixop: ConditionalOR
|
||||||
|
// | ConditionalAND
|
||||||
|
// | BitwiseOR
|
||||||
|
// | BitwiseXOR
|
||||||
|
// | BitwiseAND
|
||||||
|
// | EqualTo
|
||||||
|
// | NotEqualTo
|
||||||
|
// | LessThan
|
||||||
|
// | GreaterThan
|
||||||
|
// | LessThanEqualTo
|
||||||
|
// | GreaterThanEqualTo
|
||||||
|
// | LeftShift
|
||||||
|
// | SignedRightShift
|
||||||
|
// | UnsignedRightShift
|
||||||
|
// | Addition
|
||||||
|
// | Subtraction
|
||||||
|
// | Multiplication
|
||||||
|
// | Division
|
||||||
|
// | Remainder;
|
||||||
|
|
||||||
expression2: expression3 expression2Rest?;
|
//expression3: prefixOp expression3 // Recursion
|
||||||
|
// | ParenthesesLeft type ParenthesesRight expression3
|
||||||
|
// | primary (selector)* (postfixOp)*;
|
||||||
|
|
||||||
expression2Rest: (infixop expression3)+
|
//primary: (expression)
|
||||||
| InstanceOf type;
|
// | This (arguments)?
|
||||||
|
// | Super superSuffix
|
||||||
infixop: ConditionalOR
|
// | literal
|
||||||
| ConditionalAND
|
// | New creator
|
||||||
| BitwiseOR
|
// | identifier (Dot identifier)* (identifierSuffix)?
|
||||||
| BitwiseXOR
|
// | primitiveType bracketsOpt Dot Class
|
||||||
| BitwiseAND
|
// | Void Dot Class;
|
||||||
| EqualTo
|
|
||||||
| NotEqualTo
|
|
||||||
| LessThan
|
|
||||||
| GreaterThan
|
|
||||||
| LessThanEqualTo
|
|
||||||
| GreaterThanEqualTo
|
|
||||||
| LeftShift
|
|
||||||
| SignedRightShift
|
|
||||||
| UnsignedRightShift
|
|
||||||
| Addition
|
|
||||||
| Subtraction
|
|
||||||
| Multiplication
|
|
||||||
| Division
|
|
||||||
| Remainder;
|
|
||||||
|
|
||||||
expression3: prefixOp expression3 // Recursion
|
|
||||||
| ParenthesesLeft type ParenthesesRight expression3
|
|
||||||
| primary (selector)* (postfixOp)*;
|
|
||||||
|
|
||||||
primary: (expression)
|
|
||||||
| This (arguments)?
|
|
||||||
| Super superSuffix
|
|
||||||
| literal
|
|
||||||
| New creator
|
|
||||||
| identifier (Dot identifier)* (identifierSuffix)?
|
|
||||||
| basicType bracketsOpt Dot Class
|
|
||||||
| Void Dot Class;
|
|
||||||
|
|
||||||
identifierSuffix: SquareBracketLeft SquareBracketRight bracketsOpt Dot Class //Case []...'.'class
|
identifierSuffix: SquareBracketLeft SquareBracketRight bracketsOpt Dot Class //Case []...'.'class
|
||||||
|SquareBracketLeft expression SquareBracketRight //arr[5]
|
|SquareBracketLeft expression SquareBracketRight //arr[5]
|
||||||
|arguments
|
|arguments
|
||||||
|Dot (Class | This | Super arguments New innerCreator);
|
|Dot (Class | This | Super arguments New innerCreator);
|
||||||
|
|
||||||
prefixOp: Increment
|
//prefixOp: Increment
|
||||||
| Decrement
|
// | Decrement
|
||||||
| LogicalComplement
|
// | LogicalComplement
|
||||||
| BitWiseComplement
|
// | BitWiseComplement
|
||||||
| Addition
|
// | Addition
|
||||||
| Subtraction;
|
// | Subtraction;
|
||||||
|
|
||||||
postfixOp: Increment | Decrement;
|
postfixOp: Increment | Decrement;
|
||||||
|
|
||||||
selector: Dot identifier (arguments)?
|
//selector: Dot identifier (arguments)?
|
||||||
| Dot This
|
// | Dot This
|
||||||
| Dot Super superSuffix
|
// | Dot Super superSuffix
|
||||||
| Dot New innerCreator
|
// | Dot New innerCreator
|
||||||
| SquareBracketLeft expression SquareBracketRight;
|
// | SquareBracketLeft expression SquareBracketRight;
|
||||||
|
|
||||||
superSuffix: arguments
|
//superSuffix: arguments
|
||||||
| Dot identifier (arguments)?;
|
// | Dot identifier (arguments)?;
|
||||||
//primitives
|
|
||||||
basicType: Byte
|
primitiveType: Byte
|
||||||
| Short
|
| Short
|
||||||
| Char
|
| Char
|
||||||
| Int
|
| Int
|
||||||
@@ -111,6 +145,7 @@ basicType: Byte
|
|||||||
| Float
|
| Float
|
||||||
| Double
|
| Double
|
||||||
| Boolean;
|
| Boolean;
|
||||||
|
//Method arguments
|
||||||
argumentsOpt: (arguments)?;
|
argumentsOpt: (arguments)?;
|
||||||
|
|
||||||
arguments: ParenthesesLeft (expression (Comma expression)*)? ParenthesesRight;
|
arguments: ParenthesesLeft (expression (Comma expression)*)? ParenthesesRight;
|
||||||
@@ -142,21 +177,29 @@ blockStatement: localVariableDeclarationStatement
|
|||||||
|
|
||||||
localVariableDeclarationStatement: (Final)? type variableDeclarators;
|
localVariableDeclarationStatement: (Final)? type variableDeclarators;
|
||||||
|
|
||||||
statement: block
|
statement
|
||||||
| If parExpression statement (Else statement)?
|
: matchedStatement // Case 1: All non-ambiguous statements
|
||||||
| For ParenthesesLeft forInit? Semicolon (expression)? Semicolon forUpdate? ParenthesesRight statement
|
| If parExpression statementNoShortIf // Case 2: The 'Dangling' if (ends with an unmatched if)
|
||||||
| While parExpression statement
|
;
|
||||||
| Do statement While parExpression Semicolon
|
|
||||||
| Try block (catches | (catches)? Finally block)
|
statementNoShortIf: block
|
||||||
| Switch parExpression CurlyBracketLeft switchBlockStatementGroups? CurlyBracketRight
|
| If parExpression matchedStatement Else statementNoShortIf
|
||||||
| Synchronized parExpression block
|
| For ParenthesesLeft forInit? Semicolon (expression)? Semicolon forUpdate? ParenthesesRight statement
|
||||||
| Return (expression)? Semicolon
|
| While parExpression statement
|
||||||
| Throw expression Semicolon
|
| Do statement While parExpression Semicolon
|
||||||
| Break (identifier)? Semicolon
|
| Try block (catches+ Finally block? | Finally block)
|
||||||
| Continue (identifier)? Semicolon
|
| Switch parExpression CurlyBracketLeft switchBlockStatementGroups? CurlyBracketRight
|
||||||
| Semicolon
|
| Synchronized parExpression block
|
||||||
//| expressionStatement
|
| Return (expression)? Semicolon
|
||||||
| identifier Colon statement;
|
| Throw expression Semicolon
|
||||||
|
| Break (identifier)? Semicolon
|
||||||
|
| Continue (identifier)? Semicolon
|
||||||
|
| Semicolon
|
||||||
|
| statementExpression Semicolon
|
||||||
|
| identifier Colon statement;
|
||||||
|
|
||||||
|
matchedStatement: If parExpression matchedStatement Else matchedStatement
|
||||||
|
| statementNoShortIf;
|
||||||
|
|
||||||
catches: catchClause (catchClause)*;
|
catches: catchClause (catchClause)*;
|
||||||
|
|
||||||
@@ -205,11 +248,11 @@ constantDeclaratorRest: bracketsOpt Assignment variableInitializer;
|
|||||||
|
|
||||||
variableDeclaratorId: identifier bracketsOpt;
|
variableDeclaratorId: identifier bracketsOpt;
|
||||||
|
|
||||||
compilationUnit: (Package qualifiedIdentifier Semicolon)? (importDeclaration)*;
|
compilationUnit: (Package qualifiedIdentifier Semicolon)? (importDeclaration)*(typeDeclaration)*;
|
||||||
|
|
||||||
importDeclaration: Import identifier (Dot identifier)* (Dot Multiplication)? Semicolon;
|
importDeclaration: Import identifier (Dot identifier)* (Dot Multiplication)? Semicolon;
|
||||||
|
|
||||||
typeDeclaration: classOrInterfaceDeclaration Semicolon;
|
typeDeclaration: classOrInterfaceDeclaration | Semicolon;
|
||||||
|
|
||||||
classOrInterfaceDeclaration: modifiersOpt (classDeclaration | interfaceDeclaration);
|
classOrInterfaceDeclaration: modifiersOpt (classDeclaration | interfaceDeclaration);
|
||||||
|
|
||||||
@@ -258,7 +301,7 @@ voidInterfaceMethodDeclaratorRest: formalParameters (Throws qualifiedIdentifierL
|
|||||||
|
|
||||||
constructorDeclaratorRest: formalParameters (Throws qualifiedIdentifierList)? methodBody;
|
constructorDeclaratorRest: formalParameters (Throws qualifiedIdentifierList)? methodBody;
|
||||||
|
|
||||||
qualifiedIdentifierList: qualifiedIdentifier (Semicolon qualifiedIdentifier)*;
|
qualifiedIdentifierList: qualifiedIdentifier (Comma qualifiedIdentifier)*;
|
||||||
|
|
||||||
formalParameters: ParenthesesLeft (formalParameter (Comma formalParameter)*)? ParenthesesRight;
|
formalParameters: ParenthesesLeft (formalParameter (Comma formalParameter)*)? ParenthesesRight;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user