ccccourse / sp110b

課程:系統程式 -- 筆記、習題與報告
1 stars 50 forks source link

習題 1 : 請為編譯器加上 do ... while 語法的程式碼 #8

Open ccckmit opened 2 years ago

ccckmit commented 2 years ago

參考:

  1. https://gitlab.com/ccc110/sp/-/tree/master/03-compiler/03a-compiler
  2. https://gitlab.com/ccc110/sp/-/tree/master/03-compiler/06-compiler-run
  3. https://github.com/ccc-c/rubi/
  4. https://github.com/ccc-c/c4/

範例

s=0;
i=1;
do {
  s = s + i;
  i = i + 1;
} while (i <= 10);
z22756392z commented 2 years ago

https://github.com/z22756392z/sp110b/wiki/homework.md

stereomp3 commented 2 years ago

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)
zhengyucen commented 2 years ago

110810548 鄭宇岑

yucing commented 2 years ago

110910511 蘇郁晴 https://github.com/yucing/sp110b/wiki/e1.md

Mocha777 commented 2 years ago

110910513林孫全 https://github.com/Mocha777/sp110b/wiki/%E7%BF%92%E9%A1%8C1

shaoan901226 commented 2 years ago

110910518 黃紹安 https://github.com/shaoan901226/sp/wiki/HOMEWORK1

RyanChen-01 commented 2 years ago

110910515 陳文吉 https://github.com/RyanChen-01/sp110b/wiki/hw1.md

nakirifumiya commented 2 years ago

110910501 王澤瑋 https://github.com/nakirifumiya/sp110b/wiki/hw1

jifkavnb0205 commented 2 years ago

110810507 李安博

Lin610313 commented 2 years ago

110910506林庭光 https://github.com/Lin610313/sp110b/wiki/work1.md

Paouser commented 2 years ago

110910522 劉煒辰

stayjay commented 2 years ago

110910507 王證傑 https://github.com/stayjay/sp110b/wiki/homework1

qweasd049564 commented 2 years ago

資工三 110810509 蘇乾羽 https://github.com/qweasd049564/sp110b/blob/master/hw/hw1.md

Sakura01210 commented 2 years ago

110910508 王冠文

404HK416 commented 2 years ago

110810403 蘇彥華

zraiz commented 2 years ago

資工二 110911542 邵南翔 https://github.com/zraiz/sp110b/blob/master/Homework/01/Homework01.md

yumao57 commented 2 years ago

資工二 110910529 劉宸羽 https://github.com/yumao57/sp110b/wiki/%E7%BF%92%E9%A1%8C%E4%B8%80

choco427 commented 2 years ago

資工二110910524張詠翔

Roy-Roo commented 2 years ago

資工二 110910519 羅彥翔

kaifeng273 commented 2 years ago

資工二 110910528 吳俊億

patrick901218 commented 2 years ago

資工二 110910548 張昀翰

Kenttsai1 commented 2 years ago

110910509 蔡宗霖

jiajianong commented 2 years ago

資工二 110910535 呂嘉融

WForU commented 2 years ago

資工二 110911543 何文旺 https://github.com/WForU/sp110b/wiki/HW1.md

qwezxca123 commented 2 years ago

資工二 110910533 曾旭宏

wukunru commented 2 years ago

資工二110910510吳昆儒 https://github.com/wukunru/sp110b/wiki/%E7%BF%92%E9%A1%8C1

Uriel58 commented 2 years ago

https://github.com/Uriel58/sp110b/wiki/do...while 資工二 110910503 林成也

gakuplusq commented 2 years ago

資工二 110910530 黃劭騏

Selesfia commented 2 years ago

資工二 110910552 胡禎恩

weiian000 commented 2 years ago

資工三 110813110 李維晏

mozi5269 commented 2 years ago

資工二 110910532 王昱凱

derek120432 commented 2 years ago

資工二110910514李韋德

QuiLinxinag commented 2 years ago

資工二 110910536 邱麟翔

Soober9260 commented 2 years ago

資工二 110910546 施威帆

po-hsiang666 commented 2 years ago

資工二 110910544 張博翔

Vialbum commented 2 years ago

資工二 110910547 林鈺翔

OohelloworldoO commented 2 years ago

資工二 110910539 鄭智陽 https://github.com/OohelloworldoO/sp110b/wiki/exercise-1

al2698 commented 2 years ago

資工二 110910531 姜呈諭

ChiaYunn commented 2 years ago

110810535

YangTiChu commented 2 years ago

資工二 110910527 楊堤筑 習題1

nnnnnnn1266 commented 2 years ago

資工二 110910559潘其恩

daniel74859641 commented 2 years ago

110910563陳威宇 https://github.com/daniel74859641/-/blob/%E7%B6%B2%E9%A0%81%E6%9C%9F%E6%9C%AB%E5%A0%B1%E5%91%8A/%E7%B3%BB%E7%B5%B1%E7%A8%8B%E5%BC%8Fhw1

wei-annn commented 2 years ago

110910504趙唯安 https://github.com/wei-annn/sp110b/wiki/hw1.md