Download presentation

Presentation is loading. Please wait.

Published byStella Williard Modified over 2 years ago

1
CS 338 Project: Phase 4

2
Grammar Start stmt eof Stmt id = expr | if (expr) then stmt | while (expr) do stmt | begin CS end CS stmt ; CS | ote: expr has the same rules seen in Phase 3

3
Translation rules: 1- Translation of id=expr: translation of expr pop id.lexeme 2- Translation of if (expr) then stmt: translation of expr pop r2 cmp r2, 0 be else translation of stmt else: 3- Translation of while (expr) do stmt : while: translation of expr pop r2 cmp r2, 0 be endwhile translation of stmt b while endwhile: 4- Translation of begin CS end is the translation of CS.

4
If (1) then X = 1 + 2 – 5; Compiler Input file Push 1 Pop r2 Cmp r2,0 Be else Push 1 Push 2 Pop r1 Pop r2 Add r2, r1 Push r2 Push 5 Pop r1 Pop r2 Sub r2, r1 Push r2 Pop x else Output (Intermediate code)

5
New intermediate instructions: be: branch if equal b : unconditional branch Translation scheme Start stmt eof Stmt id = expr {printf(“pop %s”, id.lexeme)} | if (expr) {if1()} then stmt {if2()} | while {w1()} (expr) {w2()} do stmt {w3()} | begin CS end CS stmt ; CS |

6
Actions If1() { printf(“pop r2\ncmp r2,0\nbe else\n”); } If2() { printf(“else\n”); }

7
Actions (Cond) w1() { printf(“while\n”); } w2() { printf(“pop r2\ncmp r2,0\nbe endwhile\n”); } w1() { printf(“b while\nendwhile\n”); }

Similar presentations

OK

1 Lex. 2 Lex is a lexical analyzer Var = 12 + 9; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:

1 Lex. 2 Lex is a lexical analyzer Var = 12 + 9; if (test > 20) temp = 0; else while (a < 20) temp++; Lex Ident: Var Integer: 12 Oper: + Integer: 9 Semicolumn:

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Cell surface display ppt online Unified communications microsoft ppt online Ppt on geography of world Ppt on annual review meeting Download ppt on indus valley civilization images Ppt on earth dam spillway Ppt on search engine of internet Ppt on condition based maintenance system Chemistry ppt on hydrogen Ppt on computer science and technology