BuK/IIm12/BuK-KA-WHILE-COMPILER
Aus ProgrammingWiki
Wiki-Quelltext dieser Seite.
<language>Java</language>
== Aufgabenstellung ==
Entwickeln Sie einen Compiler, der als Eingabesprache $L_{WHILE}\cup L_{GOTO}\cup L_{LOOP}$ verwendet und ein zur Eingabe äquivalentes Programm aus $L_{WHILE}$ (Zielsprache) erzeugt. Die Interpretation des jeweils entstandenen $WHILE$-Programms ist nicht gefordert.
Verwenden Sie AtoCC
== Zielsetzung ==
Die Zielsetzung dieses Compiler ist es ein Programm das aus $WHILE$- $LOOP$- und $GOTO$-Befehelen besteht in ein $WHILE$-Prgramm umzuwandeln. Dabei können die Sprachen beliebig verschachtelt werden.
== Ausgangsgrammatik ==
===Definition===
G = (N, T, P, s)
N = {S, P, PNM, Loop_P, While_P, Goto_P, Konstante, Zahl, Ziffer, Variable, Zuweisung, Operationssymbol, Marke, Vergleich}
T = {:, ;, LOOP, DO, END, WHILE, IF, THEN, GOTO, HALT, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, :=, +, -, M, ==}
P = {
S -> P
P -> Marke : PNM | P ; P | PNM
PNM -> While_P | Goto_P | Zuweisung | Loop_P
Loop_P -> LOOP Variable DO P END
While_P -> WHILE Variable DO P END
Goto_P -> IF Vergleich THEN GOTO Marke
| GOTO Marke
| HALT
Konstante -> Zahl
Zahl -> Konstante Ziffer | Ziffer
Ziffer -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Variable -> x Konstante
Zuweisung -> Variable := Konstante
| Variable := Variable Operationssymbol Konstante
Operationssymbol -> + | -
Marke -> M Konstante
Vergleich -> Variable == Konstante
}
s = S
===Beispiel Ableitung===
[[Bild:Fana_Ausgang_ableitung.png|1000px]]
==Compiler==
=== Scanner-Definition ===
[[Bild:Fana_Token.png]]
=== Parser-Definition ===
[[Bild:Fana_Parser.png]]
[http://reichel-ol.de/loop_while_goto_vcc.xml VCC-File download]
=== Vorgehen ===
Zu jedem Programm in $LOOP$-Sprache kann ein äquivalentes Programm in der $WHILE$-Sprache angegeben werden, das gleiche gilt für die $GOTO$-Sprache.
==== LOOP zu WHILE ====
Ein $LOOP$-Programm wird so oft ausgeführt wie es die zugehörige Zählervariable angibt. Um diesen Ablauf mit einem $WHILE$-Programm zu erzeugen muss die zugehörige Zählervariable zyklisch dekrementiert und ansonsten nicht verändert werden.
==== GOTO zu WHILE ====
Um $GOTO$-Programme mittels $WHILE$-Sprache zu realisieren, wird das Programm mit einer $WHILE$-Schleife umschlossen. Allen Anweisungen wird ein $IF$ vorrangestellt und nach deren Abarbeitung die Zählervariable inkrementiert. Bei einem Sprung zu einer Marke wird die Zählervariable auf den zur jeweiligen Anweisung gehörenden Wert gesetzt.
===Beispiel ===
[http://www.michael-hielscher.de/compilerwiki/index.php/Compiler_45694_Demo Compilerwiki]
[http://reichel-ol.de/compiler.rar Download]
== Zielgrammatik ==
===Definition===
G = (N, T, P, s)
N = {S, P, PNM, While_P, IFVergleich, Konstante, Zahl, Ziffer, Variable, Zuweisung, Operationssymbol, Vergleich}
T = {;, WHILE, DO, END, IF, THEN, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, x, :=, +, -, ==}
P = {
S -> P
P -> P ; P | PNM
PNM -> IFVergleich | While_P | Zuweisung
While_P -> WHILE Variable DO P END
IFVergleich -> IF Vergleich THEN P
Konstante -> Zahl
Zahl -> Konstante Ziffer | Ziffer
Ziffer -> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
Variable -> x Konstante
Zuweisung -> Variable := Konstante
| Variable := Variable Operationssymbol Konstante
Operationssymbol -> + | -
Vergleich -> Variable == Konstante
}
s = S
=== Beispiel Ableitung ===
[[Bild:Fana_Ableitung.png|1000px]]