[13 - String Templates] Further progress on transformation
Auto stash before merge of "master" and "origin/master"
This commit is contained in:
1
.idea/modules.xml
generated
1
.idea/modules.xml
generated
@@ -2,6 +2,7 @@
|
|||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ProjectModuleManager">
|
<component name="ProjectModuleManager">
|
||||||
<modules>
|
<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$/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" />
|
<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>
|
</modules>
|
||||||
|
|||||||
15
.idea/workspace.xml
generated
15
.idea/workspace.xml
generated
@@ -1,9 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ChangeListManager">
|
<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="" />
|
||||||
=======
|
|
||||||
<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$/.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" />
|
<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$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" />
|
<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>
|
</list>
|
||||||
>>>>>>> Stashed changes
|
|
||||||
<option name="SHOW_DIALOG" value="false" />
|
<option name="SHOW_DIALOG" value="false" />
|
||||||
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
<option name="HIGHLIGHT_CONFLICTS" value="true" />
|
||||||
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
|
||||||
@@ -36,7 +33,7 @@
|
|||||||
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
|
<property name="SHARE_PROJECT_CONFIGURATION_FILES" value="true" />
|
||||||
<property name="WebServerToolWindowFactoryState" value="false" />
|
<property name="WebServerToolWindowFactoryState" value="false" />
|
||||||
<property name="aspect.path.notification.shown" value="true" />
|
<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.last.edited" value="Modules" />
|
||||||
<property name="project.structure.proportion" value="0.15429688" />
|
<property name="project.structure.proportion" value="0.15429688" />
|
||||||
<property name="project.structure.side.proportion" value="0.2" />
|
<property name="project.structure.side.proportion" value="0.2" />
|
||||||
@@ -221,9 +218,10 @@
|
|||||||
<screen x="0" y="0" width="1920" height="1040" />
|
<screen x="0" y="0" width="1920" height="1040" />
|
||||||
</state>
|
</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="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" />
|
<screen x="0" y="0" width="1920" height="1040" />
|
||||||
</state>
|
</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 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">
|
<state width="916" height="398" key="GridCell.Tab.0.bottom" timestamp="1591188037708">
|
||||||
<screen x="0" y="0" width="1920" height="1040" />
|
<screen x="0" y="0" width="1920" height="1040" />
|
||||||
@@ -245,7 +243,7 @@
|
|||||||
</state>
|
</state>
|
||||||
<state width="1398" height="245" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1591173185826" />
|
<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 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" />
|
<screen x="0" y="0" width="1920" height="1040" />
|
||||||
=======
|
=======
|
||||||
<state x="184" y="125" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1591184492286">
|
<state x="184" y="125" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1591184492286">
|
||||||
@@ -287,7 +285,12 @@
|
|||||||
>>>>>>> Stashed changes
|
>>>>>>> Stashed changes
|
||||||
</state>
|
</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="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="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">
|
<state x="277" y="36" key="SettingsEditor" timestamp="1591184318089">
|
||||||
<screen x="0" y="0" width="1920" height="1040" />
|
<screen x="0" y="0" width="1920" height="1040" />
|
||||||
</state>
|
</state>
|
||||||
|
|||||||
@@ -6,5 +6,9 @@
|
|||||||
<option name="TAB_SIZE" value="2" />
|
<option name="TAB_SIZE" value="2" />
|
||||||
</value>
|
</value>
|
||||||
</option>
|
</option>
|
||||||
|
<JavaCodeStyleSettings>
|
||||||
|
<option name="ALIGN_MULTILINE_ANNOTATION_PARAMETERS" value="true" />
|
||||||
|
<option name="ALIGN_MULTILINE_TEXT_BLOCKS" value="true" />
|
||||||
|
</JavaCodeStyleSettings>
|
||||||
</code_scheme>
|
</code_scheme>
|
||||||
</component>
|
</component>
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
<component name="ProjectCodeStyleConfiguration">
|
<component name="ProjectCodeStyleConfiguration">
|
||||||
<state>
|
<state>
|
||||||
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
|
<option name="PREFERRED_PROJECT_CODE_STYLE" value="mailinfra-formatter" />
|
||||||
</state>
|
</state>
|
||||||
</component>
|
</component>
|
||||||
@@ -86,8 +86,14 @@ public class AntlrXCompiler {
|
|||||||
XParser parser = new XParser(new CommonTokenStream(lexer));
|
XParser parser = new XParser(new CommonTokenStream(lexer));
|
||||||
parser.setTreeAdaptor(xTreeAdaptor);
|
parser.setTreeAdaptor(xTreeAdaptor);
|
||||||
CommonTree tree = parser.program().getTree();
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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"))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,10 +1,10 @@
|
|||||||
/* **********************************************
|
/* **********************************************
|
||||||
* Duale Hochschule Baden-W<>rttemberg Karlsruhe
|
* Duale Hochschule Baden-W<>rttemberg Karlsruhe
|
||||||
* Prof. Dr. J<>rn Eisenbiegler
|
* Prof. Dr. J<>rn Eisenbiegler
|
||||||
*
|
*
|
||||||
* Vorlesung <20>bersetzerbau
|
* Vorlesung <20>bersetzerbau
|
||||||
* Praxis ANTLR-Parser f<>r X
|
* Praxis ANTLR-Parser f<>r X
|
||||||
* - Grammatik f<>r Scanner und Parser
|
* - Grammatik f<>r Scanner und Parser
|
||||||
*
|
*
|
||||||
* **********************************************
|
* **********************************************
|
||||||
*/
|
*/
|
||||||
@@ -59,8 +59,8 @@ decl: ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID
|
|||||||
|
|
||||||
decllist: decl* -> ^(DECLLIST decl*);
|
decllist: decl* -> ^(DECLLIST decl*);
|
||||||
|
|
||||||
// Ausdr<64>cke
|
// Ausdr<64>cke
|
||||||
expr: multexpr (('+'^ | '-'^) multexpr)*;
|
expr: multexpr (('+'^ | '-'^) multexpr)*;
|
||||||
multexpr: simpleexpr (('*'^ | '/'^) simpleexpr)*;
|
multexpr: simpleexpr (('*'^ | '/'^) simpleexpr)*;
|
||||||
simpleexpr: '('! expr ')'!
|
simpleexpr: '('! expr ')'!
|
||||||
| INTCONST | '-' INTCONST -> ^(UMINUS INTCONST)
|
| INTCONST | '-' INTCONST -> ^(UMINUS INTCONST)
|
||||||
|
|||||||
@@ -1,8 +1,22 @@
|
|||||||
package de.dhbw.compiler.antlrxcompiler;
|
package de.dhbw.compiler.antlrxcompiler;
|
||||||
|
|
||||||
public enum XType {
|
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() {
|
public String toString() {
|
||||||
switch (this) {
|
switch (this) {
|
||||||
case InvalidType: return "invalid";
|
case InvalidType: return "invalid";
|
||||||
@@ -12,5 +26,4 @@ public enum XType {
|
|||||||
default: return "";
|
default: return "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -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;
|
tree grammar XTypeCheck;
|
||||||
|
|
||||||
options {
|
options {
|
||||||
@@ -13,16 +24,107 @@ tokens{
|
|||||||
|
|
||||||
@header {
|
@header {
|
||||||
package de.dhbw.compiler.antlrxcompiler;
|
package de.dhbw.compiler.antlrxcompiler;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
}
|
}
|
||||||
|
|
||||||
@members {
|
@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);
|
||||||
|
|
||||||
|
|||||||
@@ -9,13 +9,38 @@ options {
|
|||||||
|
|
||||||
@header {
|
@header {
|
||||||
package de.dhbw.compiler.antlrxcompiler;
|
package de.dhbw.compiler.antlrxcompiler;
|
||||||
|
|
||||||
|
import de.dhbw.compiler.antlrxcompiler.StringTemplateBuilder;
|
||||||
}
|
}
|
||||||
|
|
||||||
@members {
|
@members {
|
||||||
|
|
||||||
private SymbolTable symbols = SymbolTable.getInstance();
|
private SymbolTable symbols = SymbolTable.getInstance();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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>
|
||||||
|
}
|
||||||
|
>>;
|
||||||
Reference in New Issue
Block a user