7.2说明语句 处理套过程中的说明语句 P→MD{ addwidth(tp( (lptr),top(O历e)) pop(tblptr); popoffset)) M→8 Rt: =mutable(niy) D1,'6 push(t, tlprt); push(0, offset)) D- proc id; NDi;sit: top(tblptr); addwidth(t, top(offset)); pop(tblptr); pop(offset); enterproc(top(tblptr), id name, t) D-id T enter(top(tblptr), id name, T type, top(offset); topoffset):=top(offset)+ Twidth j N>8t:mktable(top(tblptr)) push(t, tblptr); push(0, offset))
7.2 说 明 语 句 处理嵌套过程中的说明语句 P → M D {addwidth (top (tblptr), top (offset) ); pop(tblptr); pop (offset) } M → {t := mktable (nil); push(t, tblprt); push (0, offset) } D → D1 ; D2 D → proc id ; N D1 ; S { t := top(tblptr); addwidth(t, top(offset) ); pop(tblptr); pop(offset); enterproc(top(tblptr), id.name, t ) } D→id : T {enter(top(tblptr), id.name, T.type, top(offset)); top(offset) := top(offset) + T.width } N → {t := mktable(top(tblptr) ); push(t, tblptr); push(0, offset) }
1) program sort(input, output) 2) var a: array[0.10]of integer (3) X: Integer (4) procedure readarray (5) var 1: Integer (6) begin.a.end readarray j (7) procedure exchange(i,j: integer) (8) b egIn Q x:=aj; a]: =al; a[]: =x (10) end exchange) (11) procedure quicksort(m, n; integer) (12) var k,v: integer (13) function partition(y, z: integer integer (14) var 1.J Integer (15) begin.a (16) (17) exchange(ij)2… (18) end i partition (19) begin. end quicksort) (20) begin.end sort)
• (1) program sort(input,output) • (2) var a: array[0..10] of integer; • (3) x: integer; • (4) procedure readarray • (5) var i: integer; • (6) begin…a…end{readarray} • (7) procedure exchange(i,j:integer); • (8) begin • (9) x:=a[i]; a[i]:=a[j]; a[j]:=x • (10) end {exchange}; • (11) procedure quicksort(m,n;integer); • (12) var k,v: integer; • (13) function partition(y,z:integer):integer; • (14) var i,j: integer; • (15) begin…a… • (16) …v… • (17) …exchange(i,j); … • (18) end {partition}; • (19) begin… end {quicksort}; • (20) begin…end {sort}
72说明语句 sort 空表头 a readarray 指向 readarray exchange 指向 exchange quicksort readarrar exchange quicksort 表头[表头 表头 partition partition
7.2 说 明 语 句 exchange readarray x a 空 表 头 sort quicksort 指向readarray partition v k 表 头 readarrary quicksort i 表 头 exchange 表 头 指向exchange partition
72说明语句 723记录的域名 T→ record d end T→ recordD end &T type: =record(top(tblptr)); Twidth top(offset); pop(tolptr); pop(offset)) L-8 t: =mktable(nil); push(t, tolprt); push(0, offset))
7.2 说 明 语 句 7.2.3 记录的域名 T → record D end T → record L D end {T.type := record (top(tblptr) ); T.width := top(offset); pop(tblptr); pop(offset)} L → {t := mktable (nil); push(t, tblprt); push(0, offset) }
73赋值语句 731简单算术表达式及赋值语句 S->id: e p: lookup(idname); fp≠ nil then emit(p,‘:=',E,plce) ese error E→>E1+E2 REplace: =newtemp; emit(E place,: =,,Er place, + E2Place))
7.3 赋 值 语 句 7.3.1 简单算术表达式及赋值语句 S → id := E {p := lookup(id.name); if p nil then emit ( p, ‘:=’, E.place) else error } E → E1 + E2 {E.place := newtemp; emit (E.place, ‘:=’, E1 .place, ‘+’, E2 .place) }