From fe1fa9154dc797c4c418a95e0cdbdb20e7e89b7c Mon Sep 17 00:00:00 2001 From: Humenius Date: Fri, 12 Jun 2020 14:06:48 +0200 Subject: [PATCH] [13 - String Templates] Further progress on transformation Auto stash before merge of "master" and "origin/master" --- .idea/modules.xml | 1 + .idea/workspace.xml | 15 ++- .../.idea/codeStyles/Project.xml | 4 + .../.idea/codeStyles/codeStyleConfig.xml | 2 +- .../antlrxcompiler/AntlrXCompiler.java | 10 +- .../antlrxcompiler/StringTemplateBuilder.java | 21 ++++ .../src/de/dhbw/compiler/antlrxcompiler/X.g | 14 +-- .../dhbw/compiler/antlrxcompiler/XType.java | 21 +++- .../dhbw/compiler/antlrxcompiler/XTypeCheck.g | 110 +++++++++++++++++- .../de/dhbw/compiler/antlrxcompiler/XtoJava.g | 29 ++++- 10 files changed, 201 insertions(+), 26 deletions(-) create mode 100644 CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/StringTemplateBuilder.java diff --git a/.idea/modules.xml b/.idea/modules.xml index a9a5ba8..0e18337 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,6 +2,7 @@ + diff --git a/.idea/workspace.xml b/.idea/workspace.xml index f913714..4516622 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,9 +1,7 @@ -<<<<<<< Updated upstream -======= @@ -16,7 +14,6 @@ ->>>>>>> Stashed changes \ No newline at end of file diff --git a/CC-Praxis-Antlr Baumgrammatiken-Leer/.idea/codeStyles/codeStyleConfig.xml b/CC-Praxis-Antlr Baumgrammatiken-Leer/.idea/codeStyles/codeStyleConfig.xml index 79ee123..550613e 100644 --- a/CC-Praxis-Antlr Baumgrammatiken-Leer/.idea/codeStyles/codeStyleConfig.xml +++ b/CC-Praxis-Antlr Baumgrammatiken-Leer/.idea/codeStyles/codeStyleConfig.xml @@ -1,5 +1,5 @@ - \ No newline at end of file diff --git a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/AntlrXCompiler.java b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/AntlrXCompiler.java index 2f0acd7..667e783 100644 --- a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/AntlrXCompiler.java +++ b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/AntlrXCompiler.java @@ -86,8 +86,14 @@ public class AntlrXCompiler { XParser parser = new XParser(new CommonTokenStream(lexer)); parser.setTreeAdaptor(xTreeAdaptor); CommonTree tree = parser.program().getTree(); + + // Type check + XTypeCheck typeCheck = new XTypeCheck(new CommonTreeNodeStream(xTreeAdaptor, tree)); + CommonTree typeCheckedTree = typeCheck.program().getTree(); - //TODO Weitere Stufen Aufrufen - + // X to Java + XtoJava javaConverter = new XtoJava(new CommonTreeNodeStream(xTreeAdaptor, typeCheckedTree)); + StringTemplate template = (StringTemplate) javaConverter.program().getTemplate(); + System.out.println(template.toString()); } } diff --git a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/StringTemplateBuilder.java b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/StringTemplateBuilder.java new file mode 100644 index 0000000..531d603 --- /dev/null +++ b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/StringTemplateBuilder.java @@ -0,0 +1,21 @@ +package de.dhbw.compiler.antlrxcompiler; + +import java.util.stream.Collectors; + +import org.antlr.stringtemplate.StringTemplate; + +public class StringTemplateBuilder { + private static SymbolTable symbols = SymbolTable.getInstance(); + + public static StringTemplate declList() { + return new StringTemplate( + symbols.values() + .stream() + .map(symbol -> symbol.type.getJavaType() + " = " + symbol.name + ";") + .collect(Collectors.joining("\n")) + ); + } + + +} + diff --git a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/X.g b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/X.g index 7aa3654..9c12dd6 100644 --- a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/X.g +++ b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/X.g @@ -1,10 +1,10 @@ /* ********************************************** - * Duale Hochschule Baden-Württemberg Karlsruhe - * Prof. Dr. Jörn Eisenbiegler + * Duale Hochschule Baden-W�rttemberg Karlsruhe + * Prof. Dr. J�rn Eisenbiegler * - * Vorlesung Übersetzerbau - * Praxis ANTLR-Parser für X - * - Grammatik für Scanner und Parser + * Vorlesung �bersetzerbau + * Praxis ANTLR-Parser f�r X + * - Grammatik f�r Scanner und Parser * * ********************************************** */ @@ -59,8 +59,8 @@ decl: ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID decllist: decl* -> ^(DECLLIST decl*); -// Ausdrücke -expr: multexpr (('+'^ | '-'^) multexpr)*; +// Ausdr�cke +expr: multexpr (('+'^ | '-'^) multexpr)*; multexpr: simpleexpr (('*'^ | '/'^) simpleexpr)*; simpleexpr: '('! expr ')'! | INTCONST | '-' INTCONST -> ^(UMINUS INTCONST) diff --git a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XType.java b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XType.java index 3f62109..0a0c822 100644 --- a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XType.java +++ b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XType.java @@ -1,8 +1,22 @@ package de.dhbw.compiler.antlrxcompiler; -public enum XType { - NoType, InvalidType, IntType, FloatType, StringType; - +public enum XType { + NoType(""), + InvalidType(""), + IntType("int"), + FloatType("float"), + StringType("String"); + + private final String javaType; + + XType(String javaType) { + this.javaType = javaType; + } + + public String getJavaType() { + return javaType; + } + public String toString() { switch (this) { case InvalidType: return "invalid"; @@ -12,5 +26,4 @@ public enum XType { default: return ""; } } - } \ No newline at end of file diff --git a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g index a4d063c..b936ba0 100644 --- a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g +++ b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g @@ -1,3 +1,14 @@ +/* ********************************************** + * Duale Hochschule Baden-Württemberg Karlsruhe + * Prof. Dr. Jörn Eisenbiegler + * + * Vorlesung Übersetzerbau + * Praxis ANTLR-Übersetzer für X + * - Typ-Prüfung + * + * ********************************************** + */ + tree grammar XTypeCheck; options { @@ -13,16 +24,107 @@ tokens{ @header { package de.dhbw.compiler.antlrxcompiler; - + import java.util.HashMap; } @members { - private SymbolTable symbols = SymbolTable.getInstance(); - + private SymbolTable symbols = SymbolTable.getInstance(); + + private void error(XTree tree, String message) { + System.err.println("Error at "+tree.getLine()+","+tree.getCharPositionInLine()+": "+message); + } } +// Deklarationen +decl: + ^(DECL ID 'int' r='read'? p='print'?) + { symbols.put($ID.text, new Symbol($ID.text, XType.IntType, $r!=null, $p!=null)); } +| ^(DECL ID 'float' r='read'? p='print'?) + { symbols.put($ID.text, new Symbol($ID.text, XType.FloatType, $r!=null, $p!=null)); } +| ^(DECL ID 'string' r='read'? p='print'?) + { symbols.put($ID.text, new Symbol($ID.text, XType.StringType, $r!=null, $p!=null)); } ; -program: 'todo'; +decllist: ^(DECLLIST decl*); + +// Expr und Cond!! +expr: + + ^(op=('+' | '-' | '/' | '*') l=expr r=expr) + { + if ($l.tree.exprType==XType.IntType && $r.tree.exprType==XType.IntType) { + $op.tree.exprType=XType.IntType; + } else if (($l.tree.exprType==XType.IntType || $l.tree.exprType==XType.FloatType) && + ($r.tree.exprType==XType.IntType || $r.tree.exprType==XType.FloatType)) { + $op.tree.exprType=XType.FloatType; + } else if ($l.tree.exprType==XType.StringType && $r.tree.exprType==XType.StringType && $op.type==PLUS) { + $op.tree.exprType=XType.StringType; + } else { + $op.tree.exprType=XType.InvalidType; + error($op,$op.text+" is not valid for operands "+$l.tree.exprType+" and "+$r.tree.exprType+"."); + } + } + +| ^(op=UMINUS e=expr) + { + $op.tree.exprType=$e.tree.exprType; + } + +| INTCONST { $INTCONST.tree.exprType=XType.IntType; } +| FLOATCONST { $FLOATCONST.tree.exprType=XType.FloatType; } +| STRINGCONST { $STRINGCONST.tree.exprType=XType.StringType; } +| ID { if (!symbols.containsKey($ID.text)) { + $ID.tree.exprType=XType.InvalidType; + error($ID,"Variable "+$ID.text+" is not defined."); + } else { + $ID.tree.exprType=symbols.get($ID.text).type; + } + }; + +// Zuweisungen +assignstat: ^(op=':=' ID expr) + { if (!symbols.containsKey($ID.text)) { + $ID.tree.exprType=XType.InvalidType; + $op.tree.exprType=XType.InvalidType; + error($ID,"Variable "+$ID.text+" is not defined."); + } else { + $ID.tree.exprType=symbols.get($ID.text).type; + if ($ID.tree.exprType==XType.FloatType && $expr.tree.exprType==XType.IntType) { + $op.tree.exprType=XType.FloatType; + } else if ($ID.tree.exprType!=$expr.tree.exprType) { + $op.tree.exprType=XType.InvalidType; + error($op,"An expression of type "+$expr.tree.exprType+ + " cannot be assigned to a variable of type "+$ID.tree.exprType+"."); + } else { + $op.tree.exprType=$ID.tree.exprType; + } + } + }; + +// Bedingungen +cond: ^(op=('<' |'>' |'=') l=expr r=expr) + { if ($l.tree.exprType==XType.StringType || $r.tree.exprType==XType.StringType) { + error($op,$op.text+" is not valid for string operands."); + } else if ($l.tree.exprType==XType.IntType && $r.tree.exprType==XType.IntType) { + $op.tree.exprType=XType.IntType; + } else { + $op.tree.exprType=XType.FloatType; + } + }; + +// Bedingte Zuweisung +condstat: ^('if' cond stat stat?); + +// Schleifen +whilestat: ^('while' cond stat); +forstat: ^('for' assignstat cond assignstat stat); + +// Anweisungen +stat: assignstat | condstat | whilestat | forstat | statlist; + +statlist: ^(STATLIST stat*); + +// Programme +program: { symbols.clear(); } ^('program' ID decllist statlist); diff --git a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XtoJava.g b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XtoJava.g index 46c4efb..bf9dbae 100644 --- a/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XtoJava.g +++ b/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XtoJava.g @@ -9,13 +9,38 @@ options { @header { package de.dhbw.compiler.antlrxcompiler; + + import de.dhbw.compiler.antlrxcompiler.StringTemplateBuilder; } @members { - private SymbolTable symbols = SymbolTable.getInstance(); + private SymbolTable symbols = SymbolTable.getInstance(); } -program: 'todo' -> template() "Hello World!"; +decllist: ^(DECLLIST decl*) -> template(d={StringTemplateBuilder.declList()}) "" + | DECLLIST -> template() ""; +expr: ^(op=('+' | '-' | '/' | '*') l=expr r=expr) + -> template(l={$l}, r={$r}, op={$op}) "( )" + | expr=(STRINGCONST | INTCONST | FLOATCONST | ID) + -> template(value={expr.st}) ""; + +assignstat: ID ':='^ expr -> template(id={$ID.text}, e={$expr.st}) " = ;" + +whilestat: ^('while' cond stat) -> template(c={$cond.st}, s={$stat.st}) + << + while { + + } + >>; + +// program: 'todo' -> template() "Hello World!"; +program: ^('program' ID decllist statlist) + -> template(className={$ID.text}, d={$decllist.st}, s={$statlist.st}) + << + class { + = + } + >>; \ No newline at end of file