46 lines
1.4 KiB
Plaintext
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>
|
|
}
|
|
>>; |