Beispiel für ANTLR Parser

This commit is contained in:
JanUlrich 2016-12-15 16:28:12 +01:00
parent c34b3c4c14
commit 40ba5fb621
2 changed files with 31 additions and 3 deletions

View File

@ -22,9 +22,8 @@ public class RunParser{
CommonTokenStream tokens = new CommonTokenStream(lexer); CommonTokenStream tokens = new CommonTokenStream(lexer);
Java8Parser parser = new Java8Parser(tokens); Java8Parser parser = new Java8Parser(tokens);
ParserRuleContext tree = parser.compilationUnit(); ParserRuleContext tree = parser.compilationUnit();
ParseTreeWalker walker = new ParseTreeWalker(); SyntaxTreeGenerator generator = new SyntaxTreeGenerator(null);
CompilationUnitEvaluator eval = new CompilationUnitEvaluator(); generator.convert((Java8Parser.CompilationUnitContext) tree);
walker.walk(eval, tree);
} }
catch(Exception e){ catch(Exception e){
System.out.println("An exception occured which is unknown and on our TODO list."); System.out.println("An exception occured which is unknown and on our TODO list.");

View File

@ -0,0 +1,29 @@
package de.dhbwstuttgart.parser;
import de.dhbwstuttgart.syntaxtree.SourceFile;
import de.dhbwstuttgart.syntaxtree.Class;
import de.dhbwstuttgart.typecheck.JavaClassRegistry;
import java.util.ArrayList;
import java.util.List;
public class SyntaxTreeGenerator {
public SyntaxTreeGenerator(JavaClassRegistry nameReg){
//TODO: Implement
}
public SourceFile convert(Java8Parser.CompilationUnitContext ctx){
List<Class> classes = new ArrayList<>();
for(Java8Parser.TypeDeclarationContext typeDecl : ctx.typeDeclaration()){
Class newClass = convert(typeDecl.classDeclaration());
classes.add(newClass);
}
return new SourceFile(classes);
}
private Class convert(Java8Parser.ClassDeclarationContext ctx) {
//TODO: Implement
return null;
}
}