File cleand up from unused code or TODO's
This commit is contained in:
20
ExprLexer.g4
20
ExprLexer.g4
@@ -15,9 +15,6 @@ Comment: (TraditionalComment | EndOfLineComment) -> skip;
|
|||||||
fragment TraditionalComment:
|
fragment TraditionalComment:
|
||||||
'/*' NotStar CommentTail;
|
'/*' NotStar CommentTail;
|
||||||
|
|
||||||
//fragment EndOfLineComment:
|
|
||||||
// '//' CharactersInLine? LineTerminator;
|
|
||||||
|
|
||||||
fragment CommentTail: '*' CommentTailStar | NotStar ;
|
fragment CommentTail: '*' CommentTailStar | NotStar ;
|
||||||
|
|
||||||
fragment CommentTailStar:
|
fragment CommentTailStar:
|
||||||
@@ -29,23 +26,6 @@ fragment NotStar:
|
|||||||
fragment NotStarNotSlash:
|
fragment NotStarNotSlash:
|
||||||
[^*/] | LineTerminator;
|
[^*/] | LineTerminator;
|
||||||
|
|
||||||
//fragment CharactersInLine: 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';
|
||||||
Assert: 'assert';
|
Assert: 'assert';
|
||||||
|
|||||||
134
ExprSyntactic.g4
134
ExprSyntactic.g4
@@ -14,28 +14,27 @@ literal: IntegerLiteral
|
|||||||
| StringLiteral
|
| StringLiteral
|
||||||
| BooleanLiteral
|
| BooleanLiteral
|
||||||
| NullLiteral;
|
| NullLiteral;
|
||||||
|
//Section 15.26: Assignment Operators
|
||||||
expression: unaryExpr assignmentOperator expression | conditionalExpr ;
|
expression: unaryExpr assignmentOperator expression | conditionalExpr ;
|
||||||
|
|
||||||
//Highest Prescedence _Expr = expression
|
//Highest Prescedence _Expr = expression
|
||||||
//assignmentExpr: unaryExpr assignmentOperator assignmentExpr | conditionalExpr;
|
//CI cha 6
|
||||||
|
conditionalExpr: logicalOrExpr Question conditionalExpr Colon conditionalExpr | logicalOrExpr;
|
||||||
|
|
||||||
conditionalExpr: logicalOrExpr '?' conditionalExpr ':' conditionalExpr |logicalOrExpr;
|
logicalOrExpr: logicalAndExpr (ConditionalOR logicalAndExpr)*;
|
||||||
|
|
||||||
logicalOrExpr: logicalAndExpr ('||' logicalAndExpr)*;
|
logicalAndExpr: equalityExpr (ConditionalAND equalityExpr)*;
|
||||||
|
|
||||||
logicalAndExpr: equalityExpr ('&&' equalityExpr)*;
|
equalityExpr: relationalExpr ((EqualTo | NotEqualTo) relationalExpr)*;
|
||||||
|
|
||||||
equalityExpr: relationalExpr (('==' | '!=') relationalExpr)*;
|
relationalExpr: additiveExpr((LessThan | GreaterThan | LessThanEqualTo | GreaterThanEqualTo) additiveExpr | InstanceOf type)*;
|
||||||
|
|
||||||
relationalExpr: additiveExpr(('<' | '>' | '<=' | '>=') additiveExpr | 'instanceof' type)*;
|
additiveExpr: multiplicativeExpr((Addition | Subtraction) multiplicativeExpr)*;
|
||||||
|
multiplicativeExpr: unaryExpr((Multiplication | Division | Remainder) unaryExpr)*;
|
||||||
|
|
||||||
additiveExpr: multiplicativeExpr(('+' | '-') multiplicativeExpr)*;
|
unaryExpr:postfixExpr |(Addition | Subtraction | LogicalComplement | BitwiseComplement | Increment | Decrement) unaryExpr | parExpression type unaryExpr;
|
||||||
multiplicativeExpr: unaryExpr(('*' | '/' | '%') unaryExpr)*;
|
|
||||||
|
|
||||||
unaryExpr:postfixExpr |('+' | '-' | '!' | '~' | '++' | '--') unaryExpr | parExpression type unaryExpr;
|
postfixExpr: primaryExpr (Increment| Decrement| Dot IDENTIFIER | SquareBracketLeft expression SquareBracketRight | arguments)*;
|
||||||
|
|
||||||
postfixExpr: primaryExpr ('++'| '--'| '.' IDENTIFIER | '[' expression ']' | arguments)*;
|
|
||||||
|
|
||||||
primaryExpr: parExpression
|
primaryExpr: parExpression
|
||||||
| IntegerLiteral
|
| IntegerLiteral
|
||||||
@@ -45,11 +44,11 @@ primaryExpr: parExpression
|
|||||||
| BooleanLiteral
|
| BooleanLiteral
|
||||||
| NullLiteral
|
| NullLiteral
|
||||||
| IDENTIFIER
|
| IDENTIFIER
|
||||||
| 'this'
|
| This
|
||||||
| 'super' ('.' IDENTIFIER)?
|
| Super (Dot IDENTIFIER)?
|
||||||
| 'new' type arguments
|
| New type arguments
|
||||||
| primitiveType ('[' ']')* '.' 'class'
|
| primitiveType (SquareBracketLeft SquareBracketRight)* Dot Class
|
||||||
| type '.' 'class';
|
| type Dot Class;
|
||||||
|
|
||||||
|
|
||||||
assignmentOperator: Assignment
|
assignmentOperator: Assignment
|
||||||
@@ -65,86 +64,20 @@ assignmentOperator: Assignment
|
|||||||
| SignedRightShiftAssign
|
| SignedRightShiftAssign
|
||||||
| UnsignedRightShiftAssign;
|
| UnsignedRightShiftAssign;
|
||||||
|
|
||||||
type: identifier (Dot identifier)* bracketsOpt
|
type: identifier (Dot identifier)* bracketsOpt | primitiveType;
|
||||||
| primitiveType;
|
|
||||||
|
|
||||||
statementExpression: expression;
|
statementExpression: expression;
|
||||||
|
|
||||||
constantExpression: expression;
|
constantExpression: expression;
|
||||||
|
|
||||||
//expression1: expression2 (expression1Rest)?;
|
identifierSuffix: SquareBracketLeft SquareBracketRight bracketsOpt Dot Class
|
||||||
//
|
|SquareBracketLeft expression SquareBracketRight
|
||||||
//expression1Rest: Question expression Colon expression1;
|
|
||||||
//
|
|
||||||
//expression2: expression3 expression2Rest?;
|
|
||||||
//
|
|
||||||
//expression2Rest: (infixop expression3)+
|
|
||||||
// | InstanceOf type;
|
|
||||||
|
|
||||||
//infixop: ConditionalOR
|
|
||||||
// | ConditionalAND
|
|
||||||
// | BitwiseOR
|
|
||||||
// | BitwiseXOR
|
|
||||||
// | BitwiseAND
|
|
||||||
// | 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)?
|
|
||||||
// | primitiveType bracketsOpt Dot Class
|
|
||||||
// | Void Dot Class;
|
|
||||||
|
|
||||||
identifierSuffix: SquareBracketLeft SquareBracketRight bracketsOpt Dot Class //Case []...'.'class
|
|
||||||
|SquareBracketLeft expression SquareBracketRight //arr[5]
|
|
||||||
|arguments
|
|arguments
|
||||||
|Dot (Class | This | Super arguments New innerCreator);
|
|Dot (Class | This | Super arguments New innerCreator);
|
||||||
|
|
||||||
//prefixOp: Increment
|
|
||||||
// | Decrement
|
|
||||||
// | LogicalComplement
|
|
||||||
// | BitWiseComplement
|
|
||||||
// | Addition
|
|
||||||
// | Subtraction;
|
|
||||||
|
|
||||||
postfixOp: Increment | Decrement;
|
postfixOp: Increment | Decrement;
|
||||||
|
|
||||||
//selector: Dot identifier (arguments)?
|
primitiveType: Byte | Short| Char | Int| Long| Float| Double| Boolean;
|
||||||
// | Dot This
|
|
||||||
// | Dot Super superSuffix
|
|
||||||
// | Dot New innerCreator
|
|
||||||
// | SquareBracketLeft expression SquareBracketRight;
|
|
||||||
|
|
||||||
//superSuffix: arguments
|
|
||||||
// | Dot identifier (arguments)?;
|
|
||||||
|
|
||||||
primitiveType: Byte
|
|
||||||
| Short
|
|
||||||
| Char
|
|
||||||
| Int
|
|
||||||
| Long
|
|
||||||
| Float
|
|
||||||
| Double
|
|
||||||
| Boolean;
|
|
||||||
//Method arguments
|
//Method arguments
|
||||||
argumentsOpt: (arguments)?;
|
argumentsOpt: (arguments)?;
|
||||||
|
|
||||||
@@ -177,34 +110,35 @@ blockStatement: localVariableDeclarationStatement
|
|||||||
|
|
||||||
localVariableDeclarationStatement: (Final)? type variableDeclarators;
|
localVariableDeclarationStatement: (Final)? type variableDeclarators;
|
||||||
|
|
||||||
statement
|
statement:completeIf | If parExpression statementIncompleteIf;
|
||||||
: matchedStatement // Case 1: All non-ambiguous statements
|
|
||||||
| If parExpression statementNoShortIf // Case 2: The 'Dangling' if (ends with an unmatched if)
|
|
||||||
;
|
|
||||||
|
|
||||||
statementNoShortIf: block
|
statementIncompleteIf: block
|
||||||
| If parExpression matchedStatement Else statementNoShortIf
|
| If parExpression completeIf Else statementIncompleteIf
|
||||||
| For ParenthesesLeft forInit? Semicolon (expression)? Semicolon forUpdate? ParenthesesRight statement
|
| For ParenthesesLeft forInit? Semicolon (expression)? Semicolon forUpdate? ParenthesesRight statement
|
||||||
| While parExpression statement
|
| While parExpression statement
|
||||||
| Do statement While parExpression Semicolon
|
| Do statement While parExpression Semicolon
|
||||||
| Try block (catches+ Finally block? | Finally block)
|
| Try block catches
|
||||||
| Switch parExpression CurlyBracketLeft switchBlockStatementGroups? CurlyBracketRight
|
| Try block Finally block
|
||||||
|
| Try block catches Finally block
|
||||||
|
| switchStatement
|
||||||
| Synchronized parExpression block
|
| Synchronized parExpression block
|
||||||
| Return (expression)? Semicolon
|
| Return (expression)? Semicolon
|
||||||
| Throw expression Semicolon
|
| Throw expression Semicolon
|
||||||
| Break (identifier)? Semicolon
|
| Break (identifier)? Semicolon
|
||||||
| Continue (identifier)? Semicolon
|
| Continue (identifier)? Semicolon
|
||||||
| Semicolon
|
|
||||||
| statementExpression Semicolon
|
| statementExpression Semicolon
|
||||||
| identifier Colon statement;
|
| Semicolon;
|
||||||
|
//solve ambiguity over If()else. aka (Dangling else).
|
||||||
matchedStatement: If parExpression matchedStatement Else matchedStatement
|
//this will do the longest check by looking for an else section, fincding the closest else!
|
||||||
| statementNoShortIf;
|
completeIf: If parExpression completeIf Else completeIf | statementIncompleteIf;
|
||||||
|
|
||||||
catches: catchClause (catchClause)*;
|
catches: catchClause (catchClause)*;
|
||||||
|
|
||||||
catchClause: Catch ParenthesesLeft formalParameter ParenthesesRight block;
|
catchClause: Catch ParenthesesLeft formalParameter ParenthesesRight block;
|
||||||
|
|
||||||
|
switchStatement: Switch parExpression CurlyBracketLeft CurlyBracketRight
|
||||||
|
| Switch parExpression CurlyBracketLeft switchBlockStatementGroups? CurlyBracketRight;
|
||||||
|
|
||||||
switchBlockStatementGroups: (switchBlockStatementGroup)*;
|
switchBlockStatementGroups: (switchBlockStatementGroup)*;
|
||||||
|
|
||||||
switchBlockStatementGroup: switchLabel blockStatements;
|
switchBlockStatementGroup: switchLabel blockStatements;
|
||||||
|
|||||||
102
ExprTool.java
102
ExprTool.java
@@ -1,102 +0,0 @@
|
|||||||
import java.io.IOException;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.util.Scanner;
|
|
||||||
|
|
||||||
import org.antlr.v4.runtime.*;
|
|
||||||
import org.antlr.v4.runtime.misc.ParseCancellationException;
|
|
||||||
import org.antlr.v4.runtime.tree.*;
|
|
||||||
|
|
||||||
public class ExprTool {
|
|
||||||
public static void main(String[] args) throws Exception {
|
|
||||||
String inputText;
|
|
||||||
String sourceName = "<stdin>";
|
|
||||||
|
|
||||||
if (args.length > 0) {
|
|
||||||
try {
|
|
||||||
inputText = new String(Files.readAllBytes(Paths.get(args[0])));
|
|
||||||
sourceName = args[0]; // set source name to file path
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.err.println("Error reading file: " + args[0]);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
Scanner scanner = new Scanner(System.in);
|
|
||||||
StringBuilder inputBuilder = new StringBuilder();
|
|
||||||
while (scanner.hasNextLine()) {
|
|
||||||
inputBuilder.append(scanner.nextLine()).append("\n");
|
|
||||||
}
|
|
||||||
inputText = inputBuilder.toString();
|
|
||||||
scanner.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create char stream with source name
|
|
||||||
CharStream input = CharStreams.fromString(inputText, sourceName);
|
|
||||||
|
|
||||||
// Create lexer
|
|
||||||
ExprLexer lexer = new ExprLexer(input);
|
|
||||||
lexer.removeErrorListeners();
|
|
||||||
lexer.addErrorListener(new ThrowingErrorListener());
|
|
||||||
|
|
||||||
// Create token stream
|
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
|
||||||
|
|
||||||
// Create parser
|
|
||||||
ExprSyntactic parser = new ExprSyntactic(tokens);
|
|
||||||
parser.removeErrorListeners();
|
|
||||||
parser.addErrorListener(new ThrowingErrorListener());
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Use compilationUnit as the start rule
|
|
||||||
ParseTree tree = parser.compilationUnit();
|
|
||||||
|
|
||||||
// Walk the tree and print class declarations
|
|
||||||
ParseTreeWalker walker = new ParseTreeWalker();
|
|
||||||
walker.walk(new ClassPrinter(sourceName), tree);
|
|
||||||
|
|
||||||
} catch (RuntimeException e) {
|
|
||||||
System.err.println("Parsing failed: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Listener to print class declarations with line/column
|
|
||||||
public static class ClassPrinter extends ExprSyntacticBaseListener {
|
|
||||||
private final String sourceName;
|
|
||||||
|
|
||||||
public ClassPrinter(String sourceName) {
|
|
||||||
this.sourceName = sourceName;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void enterClassDeclaration(ExprSyntactic.ClassDeclarationContext ctx) {
|
|
||||||
int line = ctx.getStart().getLine();
|
|
||||||
int col = ctx.getStart().getCharPositionInLine();
|
|
||||||
// Get the class name from the identifier rule
|
|
||||||
String className = ctx.identifier().getText();
|
|
||||||
System.out.printf(" file %s, class %s at line %d, column %d%n",
|
|
||||||
sourceName, className, line, col);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Custom error listener for cleaner error handling
|
|
||||||
public static class ThrowingErrorListener extends BaseErrorListener {
|
|
||||||
@Override
|
|
||||||
public void syntaxError(Recognizer<?, ?> recognizer,
|
|
||||||
Object offendingSymbol,
|
|
||||||
int line, int charPositionInLine,
|
|
||||||
String msg,
|
|
||||||
RecognitionException e) {
|
|
||||||
String sourceName = recognizer.getInputStream().getSourceName();
|
|
||||||
if (sourceName == null || sourceName.isEmpty()) {
|
|
||||||
sourceName = "<unknown>";
|
|
||||||
}
|
|
||||||
|
|
||||||
String formatted = String.format(
|
|
||||||
" file %s, line %d, column %d%n%s",
|
|
||||||
sourceName, line, charPositionInLine, msg
|
|
||||||
);
|
|
||||||
|
|
||||||
throw new ParseCancellationException(formatted);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,8 +0,0 @@
|
|||||||
public class Outer {
|
|
||||||
public class Inner1 {
|
|
||||||
}
|
|
||||||
public class Inner2 {
|
|
||||||
public class Inner3 {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user