[13 - String Templates] Further progress on transformation
Auto stash before merge of "master" and "origin/master"
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
* 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,8 +59,8 @@ decl: ID ':' (type='int' | type='float' | type='string') ';' -> ^(DECL ID
|
||||
|
||||
decllist: decl* -> ^(DECLLIST decl*);
|
||||
|
||||
// Ausdr<64>cke
|
||||
expr: multexpr (('+'^ | '-'^) multexpr)*;
|
||||
// Ausdr<64>cke
|
||||
expr: multexpr (('+'^ | '-'^) multexpr)*;
|
||||
multexpr: simpleexpr (('*'^ | '/'^) simpleexpr)*;
|
||||
simpleexpr: '('! expr ')'!
|
||||
| INTCONST | '-' INTCONST -> ^(UMINUS INTCONST)
|
||||
|
||||
@@ -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 "";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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()}) "<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