j3.27.0.0 working
This commit is contained in:
BIN
Assignment-2/.DS_Store
vendored
BIN
Assignment-2/.DS_Store
vendored
Binary file not shown.
3
Assignment-2/.vscode/settings.json
vendored
Normal file
3
Assignment-2/.vscode/settings.json
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"editor.fontSize": 12
|
||||
}
|
||||
BIN
Assignment-2/Java-8/.DS_Store
vendored
Normal file
BIN
Assignment-2/Java-8/.DS_Store
vendored
Normal file
Binary file not shown.
@@ -1,4 +1,4 @@
|
||||
// Generated from Java-8/Java8Lexer.g4 by ANTLR 4.13.2
|
||||
// Generated from /Users/mannpatel/Desktop/University/25-Fall/CPSC499/CPSC-499 Assignment/Assignment-2/Java-8/Java8Lexer.g4 by ANTLR 4.13.1
|
||||
import org.antlr.v4.runtime.Lexer;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.Token;
|
||||
@@ -10,7 +10,7 @@ import org.antlr.v4.runtime.misc.*;
|
||||
|
||||
@SuppressWarnings({"all", "warnings", "unchecked", "unused", "cast", "CheckReturnValue", "this-escape"})
|
||||
public class Java8Lexer extends Lexer {
|
||||
static { RuntimeMetaData.checkVersion("4.13.2", RuntimeMetaData.VERSION); }
|
||||
static { RuntimeMetaData.checkVersion("4.13.1", RuntimeMetaData.VERSION); }
|
||||
|
||||
protected static final DFA[] _decisionToDFA;
|
||||
protected static final PredictionContextCache _sharedContextCache =
|
||||
102
Assignment-2/Java-8/ExprTool.java
Normal file
102
Assignment-2/Java-8/ExprTool.java
Normal file
@@ -0,0 +1,102 @@
|
||||
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.tree.*;
|
||||
import org.antlr.v4.runtime.misc.ParseCancellationException;
|
||||
|
||||
public class ExprTool {
|
||||
public static void main(String[] args) throws Exception {
|
||||
if (args.length == 0) {
|
||||
// If no files given then read from stdin
|
||||
Scanner scanner = new Scanner(System.in);
|
||||
StringBuilder inputBuilder = new StringBuilder();
|
||||
while (scanner.hasNextLine()) {
|
||||
inputBuilder.append(scanner.nextLine()).append("\n");
|
||||
}
|
||||
scanner.close();
|
||||
|
||||
parseAndWalk(inputBuilder.toString(), "<stdin>");
|
||||
} else {
|
||||
// for Loop through all given file paths
|
||||
for (String filePath : args) {
|
||||
try {
|
||||
String inputText = new String(Files.readAllBytes(Paths.get(filePath)));
|
||||
parseAndWalk(inputText, filePath);
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading file: " + filePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
private static void parseAndWalk(String inputText, String sourceName) {
|
||||
try {
|
||||
// Create char stream
|
||||
CharStream input = CharStreams.fromString(inputText, sourceName);
|
||||
Java8Lexer lexer = new Java8Lexer(input);
|
||||
lexer.removeErrorListeners();
|
||||
lexer.addErrorListener(new ThrowingErrorListener());
|
||||
|
||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||
Java8Parser parser = new Java8Parser(tokens);
|
||||
parser.removeErrorListeners();
|
||||
parser.addErrorListener(new ThrowingErrorListener());
|
||||
|
||||
ParseTree tree = parser.compilationUnit();
|
||||
ParseTreeWalker walker = new ParseTreeWalker();
|
||||
walker.walk(new ClassPrinter(sourceName), tree);
|
||||
|
||||
} catch (RuntimeException e) {
|
||||
System.err.println("Parsing File Failed: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static class ClassPrinter extends Java8ParserBaseListener {
|
||||
private final String sourceName;
|
||||
|
||||
public ClassPrinter(String sourceName) {
|
||||
this.sourceName = sourceName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void enterClassDeclaration(Java8Parser.ClassDeclarationContext ctx) {
|
||||
int line = ctx.getStart().getLine();
|
||||
int col = ctx.getStart().getCharPositionInLine();
|
||||
String class_name = "";
|
||||
|
||||
if(ctx.normalClassDeclaration() != null){
|
||||
class_name = ctx.normalClassDeclaration().Identifier().getText();
|
||||
}else if(ctx.enumDeclaration() != null){
|
||||
class_name = ctx.normalClassDeclaration().Identifier().getText();
|
||||
}
|
||||
|
||||
//String className = ctx.identifier().getText();
|
||||
System.out.printf("Class %s, file %s, line %d, column %d%n",
|
||||
class_name, sourceName, line, col);
|
||||
}
|
||||
}
|
||||
|
||||
// Custom error listener
|
||||
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, error-msg: %s",
|
||||
sourceName, line, charPositionInLine, msg
|
||||
);
|
||||
|
||||
throw new ParseCancellationException(formatted);
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user