Files
dhbw-compilerbau/CC-Praxis-Antlr X Uebersetzer-Leer/src/de/dhbw/compiler/antlrxcompiler/XtoJava.g
Humenius fe1fa9154d [13 - String Templates] Further progress on transformation
Auto stash before merge of "master" and "origin/master"
2020-06-12 14:06:48 +02:00

46 lines
1.4 KiB
Plaintext

tree grammar XtoJava;
options {
language = Java;
output = template;
tokenVocab = XOptimizer;
ASTLabelType = XTree;
}
@header {
package de.dhbw.compiler.antlrxcompiler;
import de.dhbw.compiler.antlrxcompiler.StringTemplateBuilder;
}
@members {
private SymbolTable symbols = SymbolTable.getInstance();
}
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>
}
>>;