[13 - String Templates] Further progress on transformation

Auto stash before merge of "master" and "origin/master"
This commit is contained in:
2020-06-12 14:06:48 +02:00
parent 76597fb4a9
commit fe1fa9154d
10 changed files with 201 additions and 26 deletions

1
.idea/modules.xml generated
View File

@@ -2,6 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/CC-Praxis-Antlr Baumgrammatiken-Leer.iml" filepath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/CC-Praxis-Antlr Baumgrammatiken-Leer.iml" />
<module fileurl="file://$PROJECT_DIR$/CC-Praxis-Antlr Parser fuer X-Leer/CC-Praxis-Antlr Parser fuer X-Leer.iml" filepath="$PROJECT_DIR$/CC-Praxis-Antlr Parser fuer X-Leer/CC-Praxis-Antlr Parser fuer X-Leer.iml" />
<module fileurl="file://$PROJECT_DIR$/ÜB-Praxis-JFlex Scanner für AS-Leer/ÜB-Praxis-JFlex Scanner für AS-Leer.iml" filepath="$PROJECT_DIR$/ÜB-Praxis-JFlex Scanner für AS-Leer/ÜB-Praxis-JFlex Scanner für AS-Leer.iml" />
</modules>

15
.idea/workspace.xml generated
View File

@@ -1,9 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<<<<<<< Updated upstream
<list default="true" id="a11aa5c4-505d-4bfb-9e81-72367c118bbe" name="Default Changelist" comment="" />
=======
<list default="true" id="a11aa5c4-505d-4bfb-9e81-72367c118bbe" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Parser fuer X-Leer/X.tokens" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Parser fuer X-Leer/X.tokens" afterDir="false" />
@@ -16,7 +14,6 @@
<change beforePath="$PROJECT_DIR$/out/production/CC-Praxis-Antlr Parser fuer X-Leer/de/dhbw/compiler/antlrxparser/XParser$program_return.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/CC-Praxis-Antlr Parser fuer X-Leer/de/dhbw/compiler/antlrxparser/XParser$program_return.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/CC-Praxis-Antlr Parser fuer X-Leer/de/dhbw/compiler/antlrxparser/XParser.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/CC-Praxis-Antlr Parser fuer X-Leer/de/dhbw/compiler/antlrxparser/XParser.class" afterDir="false" />
</list>
>>>>>>> Stashed changes
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
@@ -36,7 +33,7 @@
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="aspect.path.notification.shown" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/ÜB-Praxis-JFlex Scanner für AS-Leer" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer" />
<property name="project.structure.last.edited" value="Modules" />
<property name="project.structure.proportion" value="0.15429688" />
<property name="project.structure.side.proportion" value="0.2" />
@@ -221,9 +218,10 @@
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="700" y="183" key="#com.intellij.ide.macro.MacrosDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1591184444196" />
<state x="700" y="233" key="FileChooserDialogImpl" timestamp="1591184758908">
<state x="700" y="233" key="FileChooserDialogImpl" timestamp="1591771754519">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="700" y="233" key="FileChooserDialogImpl/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591771754519" />
<state x="700" y="233" key="FileChooserDialogImpl/0.0.1920.1040@0.0.1920.1040" timestamp="1591184758908" />
<state width="916" height="398" key="GridCell.Tab.0.bottom" timestamp="1591188037708">
<screen x="0" y="0" width="1920" height="1040" />
@@ -245,7 +243,7 @@
</state>
<state width="1398" height="245" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1591173185826" />
<state width="916" height="398" key="GridCell.Tab.0.right/0.0.1920.1040@0.0.1920.1040" timestamp="1591188037708" />
<state x="447" y="180" width="915" height="587" key="NewModule_or_Project.wizard" timestamp="1591184776491">
<state x="447" y="180" width="915" height="587" key="NewModule_or_Project.wizard" timestamp="1591771765782">
<screen x="0" y="0" width="1920" height="1040" />
=======
<state x="184" y="125" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1591184492286">
@@ -287,7 +285,12 @@
>>>>>>> Stashed changes
</state>
<state x="335" y="156" width="915" height="587" key="NewModule_or_Project.wizard/0.0.1440.900@0.0.1440.900" timestamp="1591172753823" />
<state x="447" y="180" key="NewModule_or_Project.wizard/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591771765782" />
<state x="447" y="180" key="NewModule_or_Project.wizard/0.0.1920.1040@0.0.1920.1040" timestamp="1591184776491" />
<state x="551" y="139" key="RollbackChangesDialog" timestamp="1591772152949">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="551" y="139" key="RollbackChangesDialog/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591772152949" />
<state x="277" y="36" key="SettingsEditor" timestamp="1591184318089">
<screen x="0" y="0" width="1920" height="1040" />
</state>

View File

@@ -6,5 +6,9 @@
<option name="TAB_SIZE" value="2" />
</value>
</option>
<JavaCodeStyleSettings>
<option name="ALIGN_MULTILINE_ANNOTATION_PARAMETERS" value="true" />
<option name="ALIGN_MULTILINE_TEXT_BLOCKS" value="true" />
</JavaCodeStyleSettings>
</code_scheme>
</component>

View File

@@ -1,5 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
<option name="PREFERRED_PROJECT_CODE_STYLE" value="mailinfra-formatter" />
</state>
</component>

View File

@@ -87,7 +87,13 @@ public class AntlrXCompiler {
parser.setTreeAdaptor(xTreeAdaptor);
CommonTree tree = parser.program().getTree();
//TODO Weitere Stufen Aufrufen
// Type check
XTypeCheck typeCheck = new XTypeCheck(new CommonTreeNodeStream(xTreeAdaptor, tree));
CommonTree typeCheckedTree = typeCheck.program().getTree();
// X to Java
XtoJava javaConverter = new XtoJava(new CommonTreeNodeStream(xTreeAdaptor, typeCheckedTree));
StringTemplate template = (StringTemplate) javaConverter.program().getTemplate();
System.out.println(template.toString());
}
}

View File

@@ -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"))
);
}
}

View File

@@ -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 <20>bersetzerbau
* Praxis ANTLR-Parser f<>r X
* - Grammatik f<>r Scanner und Parser
* Vorlesung <20>bersetzerbau
* Praxis ANTLR-Parser f<>r X
* - Grammatik f<>r Scanner und Parser
*
* **********************************************
*/
@@ -59,7 +59,7 @@ decl: ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID
decllist: decl* -> ^(DECLLIST decl*);
// Ausdr<64>cke
// Ausdr<64>cke
expr: multexpr (('+'^ | '-'^) multexpr)*;
multexpr: simpleexpr (('*'^ | '/'^) simpleexpr)*;
simpleexpr: '('! expr ')'!

View File

@@ -1,7 +1,21 @@
package de.dhbw.compiler.antlrxcompiler;
public enum XType {
NoType, InvalidType, IntType, FloatType, StringType;
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) {
@@ -12,5 +26,4 @@ public enum XType {
default: return "";
}
}
}

View File

@@ -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 {
@@ -21,8 +32,99 @@ import java.util.HashMap;
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);

View File

@@ -9,6 +9,8 @@ options {
@header {
package de.dhbw.compiler.antlrxcompiler;
import de.dhbw.compiler.antlrxcompiler.StringTemplateBuilder;
}
@members {
@@ -17,5 +19,28 @@ options {
}
program: 'todo' -> template() "Hello World!";
decllist: ^(DECLLIST decl*) -> template(d={StringTemplateBuilder.declList()}) "<d>"
| DECLLIST -> template() "";
expr: ^(op=('+' | '-' | '/' | '*') l=expr r=expr)
-> template(l={$l}, r={$r}, op={$op}) "(<l> <op> <r>)"
| expr=(STRINGCONST | INTCONST | FLOATCONST | ID)
-> template(value={expr.st}) "<value>";
assignstat: ID ':='^ expr -> template(id={$ID.text}, e={$expr.st}) "<id> = <e>;"
whilestat: ^('while' cond stat) -> template(c={$cond.st}, s={$stat.st})
<<
while <c> {
<s>
}
>>;
// program: 'todo' -> template() "Hello World!";
program: ^('program' ID decllist statlist)
-> template(className={$ID.text}, d={$decllist.st}, s={$statlist.st})
<<
class <className> {
<d> = <s>
}
>>;