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:
|
||||
'/*' NotStar CommentTail;
|
||||
|
||||
//fragment EndOfLineComment:
|
||||
// '//' CharactersInLine? LineTerminator;
|
||||
|
||||
fragment CommentTail: '*' CommentTailStar | NotStar ;
|
||||
|
||||
fragment CommentTailStar:
|
||||
@@ -29,23 +26,6 @@ fragment NotStar:
|
||||
fragment NotStarNotSlash:
|
||||
[^*/] | 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 --------------------------------------------------------------------------------------------------------
|
||||
Abstract: 'abstract';
|
||||
Assert: 'assert';
|
||||
|
||||
134
ExprSyntactic.g4
134
ExprSyntactic.g4
@@ -14,28 +14,27 @@ literal: IntegerLiteral
|
||||
| StringLiteral
|
||||
| BooleanLiteral
|
||||
| NullLiteral;
|
||||
|
||||
//Section 15.26: Assignment Operators
|
||||
expression: unaryExpr assignmentOperator expression | conditionalExpr ;
|
||||
|
||||
//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)*;
|
||||
multiplicativeExpr: unaryExpr(('*' | '/' | '%') unaryExpr)*;
|
||||
unaryExpr:postfixExpr |(Addition | Subtraction | LogicalComplement | BitwiseComplement | Increment | Decrement) unaryExpr | parExpression type unaryExpr;
|
||||
|
||||
unaryExpr:postfixExpr |('+' | '-' | '!' | '~' | '++' | '--') unaryExpr | parExpression type unaryExpr;
|
||||
|
||||
postfixExpr: primaryExpr ('++'| '--'| '.' IDENTIFIER | '[' expression ']' | arguments)*;
|
||||
postfixExpr: primaryExpr (Increment| Decrement| Dot IDENTIFIER | SquareBracketLeft expression SquareBracketRight | arguments)*;
|
||||
|
||||
primaryExpr: parExpression
|
||||
| IntegerLiteral
|
||||
@@ -45,11 +44,11 @@ primaryExpr: parExpression
|
||||
| BooleanLiteral
|
||||
| NullLiteral
|
||||
| IDENTIFIER
|
||||
| 'this'
|
||||
| 'super' ('.' IDENTIFIER)?
|
||||
| 'new' type arguments
|
||||
| primitiveType ('[' ']')* '.' 'class'
|
||||
| type '.' 'class';
|
||||
| This
|
||||
| Super (Dot IDENTIFIER)?
|
||||
| New type arguments
|
||||
| primitiveType (SquareBracketLeft SquareBracketRight)* Dot Class
|
||||
| type Dot Class;
|
||||
|
||||
|
||||
assignmentOperator: Assignment
|
||||
@@ -65,86 +64,20 @@ assignmentOperator: Assignment
|
||||
| SignedRightShiftAssign
|
||||
| UnsignedRightShiftAssign;
|
||||
|
||||
type: identifier (Dot identifier)* bracketsOpt
|
||||
| primitiveType;
|
||||
type: identifier (Dot identifier)* bracketsOpt | primitiveType;
|
||||
|
||||
statementExpression: expression;
|
||||
|
||||
constantExpression: expression;
|
||||
|
||||
//expression1: expression2 (expression1Rest)?;
|
||||
//
|
||||
//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]
|
||||
identifierSuffix: SquareBracketLeft SquareBracketRight bracketsOpt Dot Class
|
||||
|SquareBracketLeft expression SquareBracketRight
|
||||
|arguments
|
||||
|Dot (Class | This | Super arguments New innerCreator);
|
||||
|
||||
//prefixOp: Increment
|
||||
// | Decrement
|
||||
// | LogicalComplement
|
||||
// | BitWiseComplement
|
||||
// | Addition
|
||||
// | Subtraction;
|
||||
|
||||
postfixOp: Increment | Decrement;
|
||||
|
||||
//selector: Dot identifier (arguments)?
|
||||
// | 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;
|
||||
primitiveType: Byte | Short| Char | Int| Long| Float| Double| Boolean;
|
||||
//Method arguments
|
||||
argumentsOpt: (arguments)?;
|
||||
|
||||
@@ -177,34 +110,35 @@ blockStatement: localVariableDeclarationStatement
|
||||
|
||||
localVariableDeclarationStatement: (Final)? type variableDeclarators;
|
||||
|
||||
statement
|
||||
: matchedStatement // Case 1: All non-ambiguous statements
|
||||
| If parExpression statementNoShortIf // Case 2: The 'Dangling' if (ends with an unmatched if)
|
||||
;
|
||||
statement:completeIf | If parExpression statementIncompleteIf;
|
||||
|
||||
statementNoShortIf: block
|
||||
| If parExpression matchedStatement Else statementNoShortIf
|
||||
statementIncompleteIf: block
|
||||
| If parExpression completeIf Else statementIncompleteIf
|
||||
| For ParenthesesLeft forInit? Semicolon (expression)? Semicolon forUpdate? ParenthesesRight statement
|
||||
| While parExpression statement
|
||||
| Do statement While parExpression Semicolon
|
||||
| Try block (catches+ Finally block? | Finally block)
|
||||
| Switch parExpression CurlyBracketLeft switchBlockStatementGroups? CurlyBracketRight
|
||||
| Try block catches
|
||||
| Try block Finally block
|
||||
| Try block catches Finally block
|
||||
| switchStatement
|
||||
| Synchronized parExpression block
|
||||
| Return (expression)? Semicolon
|
||||
| Throw expression Semicolon
|
||||
| Break (identifier)? Semicolon
|
||||
| Continue (identifier)? Semicolon
|
||||
| Semicolon
|
||||
| statementExpression Semicolon
|
||||
| identifier Colon statement;
|
||||
|
||||
matchedStatement: If parExpression matchedStatement Else matchedStatement
|
||||
| statementNoShortIf;
|
||||
| Semicolon;
|
||||
//solve ambiguity over If()else. aka (Dangling else).
|
||||
//this will do the longest check by looking for an else section, fincding the closest else!
|
||||
completeIf: If parExpression completeIf Else completeIf | statementIncompleteIf;
|
||||
|
||||
catches: catchClause (catchClause)*;
|
||||
|
||||
catchClause: Catch ParenthesesLeft formalParameter ParenthesesRight block;
|
||||
|
||||
switchStatement: Switch parExpression CurlyBracketLeft CurlyBracketRight
|
||||
| Switch parExpression CurlyBracketLeft switchBlockStatementGroups? CurlyBracketRight;
|
||||
|
||||
switchBlockStatementGroups: (switchBlockStatementGroup)*;
|
||||
|
||||
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