BuK-KA-WHILE-COMPILER

Aus ProgrammingWiki

Inhaltsverzeichnis

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

Grammatik

G = (N, T, P, s)


N={S, P_GOTO, Anweisung, P_LOOP, P_WHILE, Wertezuweisung, Variable, Marke, Konstante, Digit, Operation}
T={;, HALT, :, IF, ==, THEN, GOTO, LOOP, DO, END, WHILE, !=, 0, :=, x, M, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -}
P={
S               -> P_GOTO ; HALT | P_LOOP | P_WHILE
P_GOTO          -> Marke : Anweisung | Anweisung | P_GOTO ; P_GOTO
Anweisung       -> Wertezuweisung | GOTO Marke 
                 | IF Variable == Konstante THEN GOTO Marke    
P_LOOP          -> Wertezuweisung | P_LOOP ; P_LOOP | LOOP Variable DO P_LOOP END
P_WHILE         -> Wertezuweisung | P_WHILE ; P_WHILE 
                 | WHILE Variable != 0 DO P_WHILE END      
Wertezuweisung	-> Variable ':=' Variable Operation Konstante
Variable        -> x Konstante
Marke           -> M Konstante
Konstante       -> Digit Konstante | Digit
Digit           -> 0|1|...|9
Operation       -> + | -
}

Beispielprogramme

LOOP-Programm:

x_1 := x_1 + 1;
LOOP x_0 DO x_1 := x_1 + 1 END

Zugehöriger Ableitungsbaum für das Eingabewort x1 := x1 + 1; LOOP x0 DO x1 := x1 + 1 END:

Sirokrau_Loop_wort_ableitungsbaum.png

WHILE-Programm:

x_1 := x_1 +1;
WHILE x_1 != 0 DO x_1 := x_1 + 1 END

Zugehöriger Ableitungsbaum für das Eingabewort x1 := x1 +1;WHILE x1 != 0 DO x1 := x1 + 1 END:

Sirokrau_While_wort_ableitungsbaum.png

GOTO-Programm:

M1: IF x_1 = 1 THEN GOTO M2;
GOTO M2;
HALT

Zugehöriger Ableitungsbaum für das Eingabewort M1: IF x1 == 1 THEN GOTO M2;GOTO M2;HALT:

Sirokrau_Goto_wort_ableitungsbaum.png

Scanner

Definition:

Sirokrau_Scanner_def.png

Simulation:

Sirokrau_Loop_scanner_simulation.png

Parser

Manuel_Parser.png

Compiler

http://www.michael-hielscher.de/compilerwiki/index.php/ComilerBuK_WHILE