-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
習題 1 : 請為編譯器加上 do ... while 語法的程式碼 #8
Comments
dowhile#include <assert.h>
#include "compiler.h"
int E();
void STMT();
void IF();
void BLOCK();
int tempIdx = 0, labelIdx = 0;
#define nextTemp() (tempIdx++)
#define nextLabel() (labelIdx++)
#define emit printf
int isNext(char *set) { //...}
int isEnd() { //...}
char *next() { //...}
char *skip(char *set) { //...}
// F = (E) | Number | Id
int F() { //...}
// E = F (op E)*
int E() { //...}
// ASSIGN = id '=' E;
void ASSIGN() { //...}
// WHILE = while (E) STMT
void WHILE() { //...}
// DO WHILE
void DO(){
int DoBegin = nextLabel();
int DoEnd = nextLabel();
emit("(L%d)\n", DoBegin);
skip("do");
STMT(); // block call STMTS , } to end
skip("while");
skip("(");
int e = E();
emit("if not T%d goto L%d\n", e, DoEnd);
skip(")");
skip(";"); // 需要過濾掉,不然一開始在main() 執行的 STMTS會觸發 ASSIGN() 裡面的 skip("=") 導致錯誤
emit("goto L%d\n", DoBegin);
emit("(L%d)\n", DoEnd);
}
// STMT = WHILE | BLOCK | ASSIGN
void STMT() {
if (isNext("do")){
DO();
}
else if(isNext("while"))
WHILE();
// else if (isNext("if"))
// IF();
else if (isNext("{"))
BLOCK();
else
ASSIGN();
}
// STMTS = STMT*
void STMTS() {//...}
// BLOCK = { STMTS }
void BLOCK() { //...}
// PROG = STMTS
void PROG() { //...}
void parse() {
printf("============ parse =============\n");
tokenIdx = 0;
PROG();
}
============ parse =============
t0 = 0
s = t0
t1 = 1
i = t1
(L0)
t2 = s
t3 = i
t4 = t2 + t3
s = t4
t5 = i
t6 = 1
t7 = t5 + t6
i = t7
t8 = i
t9 = 10
t10 = t8 < t9
if not T10 goto L1
goto L0
(L1) |
110910511 蘇郁晴 |
110910518 黃紹安 |
110910515 陳文吉 |
110910501 王澤瑋 |
110910506林庭光 |
110910507 王證傑 |
資工三 110810509 蘇乾羽 |
資工二 110911542 邵南翔 |
資工二 110910529 劉宸羽 |
資工二 110911543 何文旺 |
資工二110910510吳昆儒 |
https://github.com/Uriel58/sp110b/wiki/do...while |
資工二 110910539 鄭智陽 |
資工二 110910527 楊堤筑 習題1 |
110910504趙唯安 |
參考:
範例
The text was updated successfully, but these errors were encountered: