[12 - Tree Grammar] Add type check

This commit is contained in:
2020-06-13 19:16:35 +02:00
parent 04c18d4fdf
commit 54d6ea874e
10 changed files with 1726 additions and 173 deletions

78
.idea/workspace.xml generated
View File

@@ -2,22 +2,16 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="a11aa5c4-505d-4bfb-9e81-72367c118bbe" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/runConfigurations/Run_Antlr_builder.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/runConfigurations/Run_Antlr_builder.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 Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/AntlrXTreeGrammarMain.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/AntlrXTreeGrammarMain.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/AssignCount.g" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/AssignCount.g" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/AssignCount.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/AssignCount.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/AssignCount.tokens" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/AssignCount.tokens" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XLexer.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XLexer.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XParser.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XParser.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XTreeGrammar.g" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XTreeGrammar.g" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XTreeGrammar.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XTreeGrammar.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XTreeGrammar.tokens" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/XTreeGrammar.tokens" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/test/AssignCountTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/test/AssignCountTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/test/ParseTreeTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/test/ParseTreeTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/test/TokenStreamTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/test/TokenStreamTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/test/TreeGrammarTreeTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr Baumgrammatiken-Leer/src/de/dhbw/compiler/antlrxtreegrammar/test/TreeGrammarTreeTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/AntlrXCompiler.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/AntlrXCompiler.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/X.g" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/X.g" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XOptimizer.g" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XOptimizer.g" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.g" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.tokens" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XTypeCheck.tokens" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/test/OptimizerTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/test/OptimizerTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/test/ParseTreeTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/test/ParseTreeTest.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/test/TypeCheckTest.java" beforeDir="false" afterPath="$PROJECT_DIR$/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/test/TypeCheckTest.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -43,7 +37,7 @@
<property name="project.structure.proportion" value="0.15429688" />
<property name="project.structure.side.proportion" value="0.2" />
</component>
<component name="RunManager" selected="JUnit.TestAntlrXAssignCount1">
<component name="RunManager" selected="JUnit.TestAntlrXTypeCheck">
<configuration name="AntlrXTreeGrammarMain" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="de.dhbw.compiler.antlrxtreegrammar.AntlrXTreeGrammarMain" />
<module name="CC-Praxis-Antlr Baumgrammatiken-Leer" />
@@ -144,19 +138,22 @@
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="TestAntlrXScanner1.tokenStringConst" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="CC-Praxis-Antlr Parser fuer X-Leer" />
<configuration name="TestAntlrXTypeCheck" type="JUnit" factoryName="JUnit" temporary="true" nameIsGenerated="true">
<module name="CC-Praxis-Antlr X Uebersetzer-Leer" />
<useClassPathOnly />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="de.dhbw.compiler.antlrxparser.test.*" />
<option name="PATTERN" value="de.dhbw.compiler.antlrxcompiler.test.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<option name="PACKAGE_NAME" value="de.dhbw.compiler.antlrxparser.test" />
<option name="MAIN_CLASS_NAME" value="de.dhbw.compiler.antlrxparser.test.TestAntlrXScanner1" />
<option name="METHOD_NAME" value="tokenStringConst" />
<option name="TEST_OBJECT" value="method" />
<option name="PACKAGE_NAME" value="de.dhbw.compiler.antlrxcompiler.test" />
<option name="MAIN_CLASS_NAME" value="de.dhbw.compiler.antlrxcompiler.test.TestAntlrXTypeCheck" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="class" />
<option name="PARAMETERS" value="" />
<method v="2">
<option name="RunConfigurationTask" enabled="true" run_configuration_name="Run Antlr Builder" run_configuration_type="Application" />
<option name="Make" enabled="true" />
</method>
</configuration>
@@ -166,17 +163,17 @@
<item itemvalue="JUnit.TestAntlrXScanner1" />
<item itemvalue="JUnit.TestAntlrXScanner1.tokenASSIGN" />
<item itemvalue="JUnit.TestAntlrXParser1" />
<item itemvalue="JUnit.TestAntlrXScanner1.tokenStringConst" />
<item itemvalue="JUnit.TestAntlrXAssignCount" />
<item itemvalue="JUnit.TestAntlrXAssignCount1" />
<item itemvalue="JUnit.TestAntlrXTypeCheck" />
</list>
<recent_temporary>
<list>
<item itemvalue="JUnit.TestAntlrXTypeCheck" />
<item itemvalue="JUnit.TestAntlrXAssignCount1" />
<item itemvalue="JUnit.TestAntlrXAssignCount" />
<item itemvalue="Application.AntlrXTreeGrammarMain" />
<item itemvalue="JUnit.TestAntlrXParser1" />
<item itemvalue="JUnit.TestAntlrXScanner1.tokenStringConst" />
</list>
</recent_temporary>
</component>
@@ -197,7 +194,8 @@
<workItem from="1591771652663" duration="1715000" />
<workItem from="1591962621113" duration="4935000" />
<workItem from="1591970247448" duration="49000" />
<workItem from="1591970308877" duration="300000" />
<workItem from="1591970308877" duration="728000" />
<workItem from="1592059055121" duration="9317000" />
</task>
<servers />
</component>
@@ -213,11 +211,11 @@
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="570" y="121" key="#TestAntlrXScanner1.tokenASSIGN/0.0.1920.1040@0.0.1920.1040" timestamp="1591175187591" />
<state x="-1471" y="200" width="1471" height="691" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1591969874169">
<state x="-1471" y="200" width="1471" height="691" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1592066371011">
<screen x="-1920" y="0" width="1920" height="1040" />
</state>
<state x="184" y="112" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1440.900@0.0.1440.900" timestamp="1591172899381" />
<state x="-1471" y="200" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591969874169" />
<state x="-1471" y="200" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1592066371011" />
<state x="-37" y="200" width="1471" height="691" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1591184444701" />
<state x="92" y="92" width="1736" height="856" key="#com.intellij.execution.junit2.states.ComparisonFailureState$DiffDialog" timestamp="1591185724270">
<screen x="0" y="0" width="1920" height="1040" />
@@ -232,38 +230,38 @@
</state>
<state x="1003" y="171" key="#com.intellij.ide.macro.MacrosDialog/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591969389618" />
<state x="700" y="183" key="#com.intellij.ide.macro.MacrosDialog/0.0.1920.1040@0.0.1920.1040" timestamp="1591184444196" />
<state x="690" y="267" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1591969353358">
<state x="690" y="267" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1592059799792">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="690" y="267" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591969353358" />
<state x="690" y="267" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1592059799792" />
<state x="700" y="233" key="FileChooserDialogImpl" timestamp="1591968457509">
<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="1591968457509" />
<state x="700" y="233" key="FileChooserDialogImpl/0.0.1920.1040@0.0.1920.1040" timestamp="1591184758908" />
<state width="1877" height="178" key="GridCell.Tab.0.bottom" timestamp="1591970233761">
<state width="1877" height="321" key="GridCell.Tab.0.bottom" timestamp="1592068345609">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1398" height="245" key="GridCell.Tab.0.bottom/0.0.1440.900@0.0.1440.900" timestamp="1591173185826" />
<state width="1877" height="178" key="GridCell.Tab.0.bottom/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591970233761" />
<state width="1877" height="321" key="GridCell.Tab.0.bottom/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1592068345609" />
<state width="916" height="398" key="GridCell.Tab.0.bottom/0.0.1920.1040@0.0.1920.1040" timestamp="1591188037708" />
<state width="1877" height="178" key="GridCell.Tab.0.center" timestamp="1591970233761">
<state width="1877" height="321" key="GridCell.Tab.0.center" timestamp="1592068345609">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1398" height="245" key="GridCell.Tab.0.center/0.0.1440.900@0.0.1440.900" timestamp="1591173185825" />
<state width="1877" height="178" key="GridCell.Tab.0.center/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591970233761" />
<state width="1877" height="321" key="GridCell.Tab.0.center/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1592068345609" />
<state width="916" height="398" key="GridCell.Tab.0.center/0.0.1920.1040@0.0.1920.1040" timestamp="1591188037708" />
<state width="1877" height="178" key="GridCell.Tab.0.left" timestamp="1591970233761">
<state width="1877" height="321" key="GridCell.Tab.0.left" timestamp="1592068345609">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1398" height="245" key="GridCell.Tab.0.left/0.0.1440.900@0.0.1440.900" timestamp="1591173185825" />
<state width="1877" height="178" key="GridCell.Tab.0.left/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591970233761" />
<state width="1877" height="321" key="GridCell.Tab.0.left/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1592068345609" />
<state width="916" height="398" key="GridCell.Tab.0.left/0.0.1920.1040@0.0.1920.1040" timestamp="1591188037708" />
<state width="1877" height="178" key="GridCell.Tab.0.right" timestamp="1591970233761">
<state width="1877" height="321" key="GridCell.Tab.0.right" timestamp="1592068345609">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state width="1398" height="245" key="GridCell.Tab.0.right/0.0.1440.900@0.0.1440.900" timestamp="1591173185826" />
<state width="1877" height="178" key="GridCell.Tab.0.right/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591970233761" />
<state width="1877" height="321" key="GridCell.Tab.0.right/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1592068345609" />
<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="688" height="753" key="NewModule_or_Project.wizard" timestamp="1591968463391">
<screen x="0" y="0" width="1920" height="1040" />
@@ -280,9 +278,9 @@
</state>
<state x="208" y="31" key="SettingsEditor/0.0.1440.900@0.0.1440.900" timestamp="1591172804318" />
<state x="277" y="36" key="SettingsEditor/0.0.1920.1040@0.0.1920.1040" timestamp="1591184318089" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1591969820924">
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup" timestamp="1592063655606">
<screen x="0" y="0" width="1920" height="1040" />
</state>
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1591969820924" />
<state x="623" y="225" width="672" height="678" key="search.everywhere.popup/0.0.1920.1040/-1920.0.1920.1040@0.0.1920.1040" timestamp="1592063655606" />
</component>
</project>

View File

@@ -91,6 +91,8 @@ public class AntlrXCompiler {
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();

View File

@@ -60,7 +60,7 @@ decl: ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID
decllist: decl* -> ^(DECLLIST decl*);
// Ausdr<64>cke
expr: multexpr (('+'^ | '-'^) multexpr)*;
expr: multexpr (('+'^ | '-'^) multexpr)*;
multexpr: simpleexpr (('*'^ | '/'^) simpleexpr)*;
simpleexpr: '('! expr ')'!
| INTCONST | '-' INTCONST -> ^(UMINUS INTCONST)

View File

@@ -1,14 +1,3 @@
/* **********************************************
* 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 {
@@ -23,108 +12,105 @@ tokens{
}
@header {
package de.dhbw.compiler.antlrxcompiler;
package de.dhbw.compiler.antlrxcompiler;
import java.util.HashMap;
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);
}
private void logError(XTree tree, String message) {
System.err.println(message + "\n\t" + tree);
}
}
// 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)); } ;
^(DECL ID type='int' read='read'? print='print'?)
{ symbols.put($ID.text, new Symbol($ID.text, XType.IntType, ($read != null), ($print != null))); }
| ^(DECL ID type='float' read='read'? print='print'?)
{ symbols.put($ID.text, new Symbol($ID.text, XType.FloatType, ($read != null), ($print != null))); }
| ^(DECL ID type='string' read='read'? print='print'?)
{ symbols.put($ID.text, new Symbol($ID.text, XType.StringType, ($read != null), ($print != null))); }
;
decllist: ^(DECLLIST decl*);
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=('+' | '-' | '/' | '*') left=expr right=expr)
{
if ($left.tree.exprType == XType.IntType && $right.tree.exprType == XType.IntType) {
$op.tree.exprType = XType.IntType;
} else if (($left.tree.exprType == XType.FloatType || $left.tree.exprType == XType.IntType) &&
($right.tree.exprType == XType.FloatType || $right.tree.exprType == XType.IntType)) {
$op.tree.exprType = XType.FloatType;
} else if ($left.tree.exprType == XType.StringType && $right.tree.exprType == XType.StringType && $op.type == PLUS) {
$op.tree.exprType = XType.StringType;
} else {
$op.tree.exprType = XType.InvalidType;
logError($op, "Invalid operator '" + $op.text + "' for '" + $left.tree.exprType + "' and '" + $right.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 = symbols.get($ID.text).type;
} else {
$ID.tree.exprType = XType.InvalidType;
logError($ID, "'" + $ID.text + "' is not defined.");
}
}
;
| ^(op=UMINUS e=expr)
{
$op.tree.exprType=$e.tree.exprType;
}
assignstat:
^(op=':=' ID e=expr)
{
if (!symbols.containsKey($ID.text)) {
$ID.tree.exprType = XType.InvalidType;
$op.tree.exprType = XType.InvalidType;
logError($ID, "Undeclared var '" + $ID.text + "'");
} else {
$ID.tree.exprType = symbols.get($ID.text).type;
| 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;
}
// float <- int
if ($ID.tree.exprType == XType.FloatType && $e.tree.exprType == XType.IntType) {
$op.tree.exprType = XType.FloatType;
} else if ($ID.tree.exprType != $expr.tree.exprType) {
$op.tree.exprType = XType.InvalidType;
logError($op, "Cannot assign <" + $expr.tree.exprType + "> to <" + $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;
cond:
^(op=('<' | '>' | '=') left=expr right=expr)
{
if ($left.tree.exprType == XType.StringType || $right.tree.exprType == XType.StringType) {
$op.tree.exprType = XType.InvalidType;
logError($op, "Cannot use operator '" + $op.text + "' on '" + $left.text + "' and '" + $right.text + "'");
} else if ($left.tree.exprType == XType.IntType && $right.tree.exprType == XType.IntType) {
$op.tree.exprType = XType.IntType;
} else {
$op.tree.exprType = XType.FloatType;
}
}
};
;
// Bedingte Zuweisung
condstat: ^('if' cond stat stat?);
condstat: ^(op='if' cond stat stat?);
// Schleifen
whilestat: ^('while' cond stat);
forstat: ^('for' assignstat cond assignstat stat);
whilestat: ^('while' cond stat);
forstat: ^('for' assignstat cond assignstat stat);
// Anweisungen
stat: assignstat | condstat | whilestat | forstat | statlist;
stat: assignstat | condstat | whilestat | forstat | statlist;
statlist: ^(STATLIST stat*);
statlist: ^(STATLIST stat*);
// Programme
program: { symbols.clear(); } ^('program' ID decllist statlist);
program: { symbols.clear(); } ^('program' ID decllist statlist);

View File

@@ -40,7 +40,6 @@ STRINGCONST=16
UMINUS=17
WS=18
ZERO=19
T__46=46
PLUS=23
'('=20
')'=21
@@ -68,4 +67,3 @@ PLUS=23
'string'=43
'then'=44
'while'=45
'todo'=46

View File

@@ -16,17 +16,11 @@ import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import de.dhbw.compiler.antlrxcompiler.*;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import de.dhbw.compiler.antlrxcompiler.XLexer;
import de.dhbw.compiler.antlrxcompiler.XOptimizer;
import de.dhbw.compiler.antlrxcompiler.XParser;
import de.dhbw.compiler.antlrxcompiler.XTree;
import de.dhbw.compiler.antlrxcompiler.XTreeAdaptor;
import de.dhbw.compiler.antlrxcompiler.XTypeCheck;
public abstract class OptimizerTest {
protected void testTypeCheckTree(String in, String expected) throws Exception {

View File

@@ -16,6 +16,7 @@ import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import de.dhbw.compiler.antlrxcompiler.XTreeAdaptor;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.ParserRuleReturnScope;
@@ -23,7 +24,6 @@ import org.antlr.runtime.tree.CommonTree;
import de.dhbw.compiler.antlrxcompiler.XLexer;
import de.dhbw.compiler.antlrxcompiler.XParser;
import de.dhbw.compiler.antlrxcompiler.XTreeAdaptor;
public abstract class ParseTreeTest {

View File

@@ -16,16 +16,11 @@ import static org.junit.Assert.assertEquals;
import java.io.ByteArrayInputStream;
import de.dhbw.compiler.antlrxcompiler.*;
import org.antlr.runtime.ANTLRInputStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.tree.CommonTreeNodeStream;
import de.dhbw.compiler.antlrxcompiler.XLexer;
import de.dhbw.compiler.antlrxcompiler.XParser;
import de.dhbw.compiler.antlrxcompiler.XTree;
import de.dhbw.compiler.antlrxcompiler.XTreeAdaptor;
import de.dhbw.compiler.antlrxcompiler.XTypeCheck;
public abstract class TypeCheckTest {
protected void testTypeCheckTree(String in, String expected) throws Exception {