procedure block(fsys: symset; isfun: boolean; level: integer);
ints,chars,bools: (i: integer);
var dx: integer; (*data allocation index*)
prt: integer; (*t-index of this procedure*)
prb: integer; (*b-index of this procedure*)
procedure skip(fsys: symset; n: integer);
while not (sy in fsys) do insymbol
procedure test(s1,s2: symset; n: integer);
begin if not (sy in s1) then
if sy = semicolon then insymbol else
if sy in [comma,colon] then insymbol
test([ident]+blockbegsys, fsys, 6)
procedure enter(id: alfa; k: object);
begin if t = tmax then fatal(1) else
j := btab[display[level]].last; l := j;
while tab[j].name <> id do j := tab[j].link;
if j <> 0 then error(1) else
begin name := id; link := l;
obj := k; typ := notyp; ref := 0; lev := level; adr := 0
btab[display[level]].last := t
function loc(id: alfa): integer;
var i,j: integer; (*locate id in table*)
begin i := level; tab[0].name := id; (*sentinel*)
repeat j := btab[display[i]].last;
while tab[j].name <> id do j := tab[j].link;
if j = 0 then error(0); loc := j
begin enter(id,variable); insymbol
procedure constant(fsys: symset; var c: conrec);
begin c.tp := notyp; c.i := 0;
test(constbegsys, fsys, 50);
if sy in constbegsys then
begin c.tp := chars; c.i := inum; insymbol
if sy in [plus,minus] then
begin if sy = minus then sign := -1;
if tab[x].obj <> konstant then error(25) else
begin c.tp := tab[x].typ;
if c.tp = reals then c.r := sign*rconst[tab[x].adr]
else c.i := sign*tab[x].adr
begin c.tp := ints; c.i := sign*inum; insymbol
begin c.tp := reals; c.r := sign*rnum; insymbol
procedure typ(fsys: symset; var tp: types; var rf, sz: integer);
eltp: types; elrf: integer;
elsz, offset, t0,t1: integer;
procedure arraytyp(var aref,arsz: integer);
begin constant([colon,rbrack,rparent,ofsy]+fsys, low);
begin error(27); low.tp := ints; low.i := 0
if sy = colon then insymbol else error(13);
constant([rbrack,comma,rparent,ofsy]+fsys, high);
if high.tp <> low.tp then
begin error(27); high.i := low.i
enterarray(low.tp, low.i, high.i); aref := a;
begin insymbol; eltp := arrays; arraytyp(elrf,elsz)
if sy = rbrack then insymbol else
if sy = rparent then insymbol
if sy = ofsy then insymbol else error(8);
begin arsz := (high-low+1)*elsz; size := arsz;
eltyp := eltp; elref := elrf; elsize := elsz
begin (*typ*) tp := notyp; rf := 0; sz := 0;
test(typebegsys, fsys, 10);
if obj <> type1 then error(29) else
begin tp := typ; rf := ref; sz := adr;
if tp = notyp then error(30)
if sy = lbrack then insymbol else
if sy = lparent then insymbol
tp := arrays; arraytyp(rf,sz)
begin (*records*) insymbol;
enterblock; tp := records; rf := b;
if level = lmax then fatal(5);
level := level+1; display[level] := b; offset := 0;
begin t0 := t; entervariable;
begin insymbol; entervariable
if sy = colon then insymbol else error(5);
typ(fsys+[semicolon,endsy,comma,ident],eltp,elrf,elsz);
begin typ := eltp; ref := elrf; normal := true;
adr := offset; offset := offset + elsz
begin if sy = semicolon then insymbol else
if sy = comma then insymbol
test([ident,endsy,semicolon], fsys, 6)
btab[rf].vsize := offset; sz := offset; btab[rf].psize := 0;
insymbol; level := level-1
procedure parameterlist; (*formal parameter list*)
begin insymbol; tp := notyp; rf := 0; sz := 0;
test([ident, varsy], fsys+[rparent], 7);
while sy in [ident,varsy] do
begin if sy <> varsy then valpar := true else
begin insymbol; valpar := false
begin insymbol; entervariable;
if sy <> ident then error(2) else
begin x := loc(id); insymbol;
if obj <> type1 then error(29) else
begin tp := typ; rf := ref;
if valpar then sz := adr else sz := 1
test([semicolon,rparent], [comma,ident]+fsys, 14)
begin typ := tp; ref := rf;
normal := valpar; adr := dx; lev := level;
begin if sy = semicolon then insymbol else
if sy = comma then insymbol
test([ident,varsy], [rparent]+fsys, 6)
test([semicolon,colon], fsys, 6)
procedure constantdeclaration;
test([ident], blockbegsys, 2);
begin enter(id,konstant); insymbol;
if sy = eql then insymbol else
if sy = becomes then insymbol
constant([semicolon,comma,ident]+fsys,c);
tab[t].typ := c.tp; tab[t].ref := 0;
begin enterreal(c.r); tab[t].adr := c1 end
end (*constantdeclaration*) ;
procedure typedeclaration;
var tp: types; rf, sz, t1: integer;
test([ident], blockbegsys, 2);
begin enter(id,type1); t1 := t; insymbol;
if sy = eql then insymbol else
if sy = becomes then insymbol
typ([semicolon,comma,ident]+fsys, tp, rf, sz);
begin typ := tp; ref := rf; adr := sz
end (*typedeclaration*) ;
procedure variabledeclaration;
var t0, t1, rf, sz: integer;
begin t0 := t; entervariable;
begin insymbol; entervariable;
if sy = colon then insymbol else error(5);
typ([semicolon,comma,ident]+fsys, tp, rf, sz);
begin typ := tp; ref := rf;
lev := level; adr := dx; normal := true;
end (*variabledeclaration*) ;
procedure procdeclaration;
begin isfun := sy = functionsy; insymbol;
begin error(2); id := ' '
if isfun then enter(id,funktion) else enter(id,prozedure);
insymbol; block([semicolon]+fsys, isfun, level+1);
if sy = semicolon then insymbol else error(14);
emit(32+ord(isfun)) (*exit*)
end (*proceduredeclaration*) ;
(*---------------------------------------------------------statement--*)
procedure statement(fsys: symset);
procedure expression(fsys: symset; var x: item); forward;
procedure selector(fsys: symset; var v:item);
var x: item; a,j: integer;
begin (*sy in [lparent, lbrack, period]*)
begin insymbol; (*field selector*)
if sy <> ident then error(2) else
if v.typ <> records then error(31) else
begin (*search field identifier*)
j := btab[v.ref] .last; tab[0].name := id;
while tab[j].name <> id do j := tab[j].link;
v.typ := tab[j].typ; v.ref := tab[j].ref;
a := tab[j].adr; if a <> 0 then emit1(9,a)
if sy <> lbrack then error(11);
expression(fsys+[comma,rbrack], x);
if v.typ <> arrays then error(28) else
if atab[a].inxtyp <> x.typ then error(26) else
if atab[a].elsize = 1 then emit1(20,a) else emit1(21,a);
v.typ := atab[a].eltyp; v.ref := atab[a].elref
if sy = rbrack then insymbol else
begin error(12); if sy = rparent then insymbol
until not (sy in [lbrack,lparent,period]);
procedure call(fsys: symset; i: integer);
begin emit1(18,i); (*mark stack*)
lastp := btab[tab[i].ref].lastpar; cp := i;
begin (*actual parameter list*)
if cp >= lastp then error(39) else
begin (*value parameter*)
expression(fsys+[comma,colon,rparent], x);
if x.typ=tab[cp].typ then
if x.ref <> tab[cp].ref then error(36) else
if x.typ = arrays then emit1(22,atab[x.ref].size) else
if x.typ = records then emit1(22,btab[x.ref].vsize)
if (x.typ=ints) and (tab[cp].typ=reals) then
if x.typ<>notyp then error(36);
begin (*variable parameter*)
if sy <> ident then error(2) else
begin k := loc(id); insymbol;
begin if tab[k].obj <> variable then error(37);
x.typ := tab[k].typ; x.ref := tab[k].ref;
if tab[k].normal then emit2(0,tab[k].lev,tab[k].adr)
else emit2(1,tab[k].lev,tab[k].adr);
if sy in [lbrack,lparent,period] then
selector(fsys+[comma,colon,rparent], x);
if (x.typ<>tab[cp].typ) or (x.ref<>tab[cp].ref) then
test([comma,rparent], fsys, 6)
if sy = rparent then insymbol else error(4)
if cp < lastp then error(39); (*too few actual parameters*)
emit1(19, btab[tab[i].ref].psize-1);
if tab[i].lev < level then emit2(3, tab[i].lev, level)
function resulttype(a,b: types): types;
if (a>reals) or (b>reals) then
begin error(33); resulttype := notyp
if (a=notyp) or (b=notyp) then resulttype := notyp else
if b=ints then resulttype := ints else
begin resulttype := reals; emit1(26,1)
begin resulttype := reals;
if b=ints then emit1(26,0)
procedure simpleexpression(fsys:symset; var x:item);
procedure term(fsys:symset; var x:item);
procedure factor(fsys:symset; var x:item);
procedure standfct(n: integer);
begin (*standard function no. n*)
if sy = lparent then insymbol else error(9);
begin expression(fsys+[rparent],x);
(*abs,sqr*) 0,2: begin ts := [ints,reals]; tab[i].typ := x.typ;
if x.typ = reals then n := n+1
(*odd,chr*) 4,5: ts := [ints];
if x.typ = ints then n := 19;
ts := [ints,bools,chars];
(*succ,pred*) 7,8: ts := [chars];
(*round,trunc*) 9,10,11,12,13,14,15,16:
(*sin,cos,...*) begin ts := [ints,reals];
if x.typ = ints then emit1(26,0)
if x.typ in ts then emit1(8,n) else
if x.typ <> notyp then error(48);
(*eof,eoln*) begin (*n in [17,18]*)
if sy <> ident then error(2) else
if id <> 'input ' then error(0) else insymbol;
if sy = rparent then insymbol else error(4)
begin (*factor*) x.typ := notyp; x.ref := 0;
test(facbegsys, fsys, 58);
begin i := loc(id); insymbol;
konstant: begin x.typ := typ; x.ref := 0;
variable: begin x.typ := typ; x.ref := ref;
if sy in [lbrack,lparent,period] then
begin if normal then f := 0 else f := 1;
if x.typ in stantyps then emit(34)
if x.typ in stantyps then
if normal then f := 1 else f := 2
if normal then f := 0 else f := 1;
type1, prozedure: error(44);
funktion :begin x.typ := typ;
if lev <> 0 then call(fsys, i)
if sy in [charcon,intcon,realcon] then
begin x.typ := reals; enterreal(rnum);
if sy = charcon then begin
begin insymbol; expression(fsys+[rparent], x);
if sy = rparent then insymbol else error(4)
begin insymbol; factor(fsys,x);
if x.typ=bools then emit(35) else
if x.typ<>notyp then error(32)
factor(fsys+[times,rdiv,idiv,imod,andsy], x);
while sy in [times,rdiv,idiv,imod,andsy] do
begin op := sy; insymbol;
factor(fsys+[times,rdiv,idiv,imod,andsy], y);
begin x.typ := resulttype(x.typ, y.typ);
begin emit1(26,1); x.typ := reals
begin emit1(26,0); y.typ := reals
if (x.typ=reals) and (y.typ=reals) then emit(61) else
begin if (x.typ<>notyp) and (y.typ<>notyp) then
begin if (x.typ=bools) and (y.typ=bools) then
begin if (x.typ<>notyp) and (y.typ<>notyp) then
begin (*op in [idiv,imod]*)
if (x.typ=ints) and (y.typ=ints) then
begin if (x.typ<>notyp) and (y.typ<>notyp) then
begin (*simpleexpression*)
if sy in [plus,minus] then
begin op := sy; insymbol;
term(fsys+[plus,minus], x);
if x.typ > reals then error(33) else
term(fsys+[plus,minus,orsy], x);
while sy in [plus,minus,orsy] do
begin op := sy; insymbol;
term(fsys+[plus,minus,orsy], y);
if (x.typ=bools) and (y.typ=bools) then emit(51) else
begin if (x.typ<>notyp) and (y.typ<>notyp) then
begin x.typ := resulttype(x.typ, y.typ);
ints : if op = plus then emit(52)
reals: if op = plus then emit(54)
end (*simpleexpression*) ;
simpleexpression(fsys+[eql,neq,lss,leq,gtr,geq], x);
if sy in [eql,neq,lss,leq,gtr,geq] then
begin op := sy; insymbol;
simpleexpression(fsys, y);
if (x.typ in [ notyp,ints,bools,chars]) and (x.typ = y.typ) then
begin if x.typ = ints then
begin x.typ := reals; emit1(26,1)
begin y.typ := reals; emit1(26,0)
if (x.typ=reals) and (y.typ=reals) then
procedure assignment(lv,ad: integer);
var x,y: item; f: integer;
(*tab[i].obj in [variable,prozedure]*)
begin x.typ := tab[i].typ; x.ref := tab[i].ref;
if tab[i].normal then f := 0 else f := 1;
if sy in [lbrack,lparent,period] then
selector([becomes,eql]+fsys, x);
if sy = becomes then insymbol else
begin error(51); if sy = eql then insymbol
if x.typ in stantyps then emit(38) else
if x.ref <> y.ref then error(46) else
if x.typ = arrays then emit1(23, atab[x.ref].size)
else emit1(23, btab[x.ref].vsize)
if (x.typ=reals) and (y.typ=ints) then
begin emit1(26,0); emit(38)
if (x.typ<>notyp) and (y.typ<>notyp) then error(46)
procedure compoundstatement;
statement([semicolon,endsy]+fsys);
while sy in [semicolon]+statbegsys do
begin if sy = semicolon then insymbol else error(14);
statement([semicolon,endsy]+fsys)
if sy = endsy then insymbol else error(57)
end (*compoundstatemenet*) ;
var x: item; lc1,lc2: integer;
expression(fsys+[thensy,dosy], x);
if not (x.typ in [bools,notyp]) then error(17);
lc1 := lc; emit(11); (*jmpc*)
if sy = thensy then insymbol else
begin error(52); if sy = dosy then insymbol
statement(fsys+[elsesy]);
begin insymbol; lc2 := lc; emit(10);
code[lc1].y := lc; statement(fsys); code[lc2].y := lc
casetab: array [1..csmax] of
packed record val, lc: index end ;
exittab: array [1..csmax] of integer;
var lab: conrec; k: integer;
begin constant(fsys+[comma,colon], lab);
if lab.tp <> x.typ then error(47) else
if i = csmax then fatal(6) else
casetab[i].val := lab.i; casetab[i].lc := lc;
repeat k := k+1 until casetab[k].val = lab.i;
if k < i then error(1); (*multiple definition*)
begin if sy in constbegsys then
begin insymbol; caselabel
if sy = colon then insymbol else error(5);
statement([semicolon,endsy]+fsys);
j := j+1; exittab[j] := lc; emit(10)
begin insymbol; i := 0; j := 0;
expression(fsys+[ofsy,comma,colon], x);
if not (x.typ in [ints,bools,chars,notyp]) then error(23);
lc1 := lc; emit(12); (*jmpx*)
if sy = ofsy then insymbol else error(8);
begin emit1(13,casetab[k].val); emit1(13,casetab[k].lc)
for k := 1 to j do code[exittab[k]].y := lc;
if sy = endsy then insymbol else error(57)
procedure repeatstatement;
var x: item; lc1: integer;
insymbol; statement([semicolon,untilsy]+fsys);
while sy in [semicolon]+statbegsys do
begin if sy = semicolon then insymbol else error(14);
statement([semicolon,untilsy]+fsys)
begin insymbol; expression(fsys, x);
if not (x.typ in [bools,notyp]) then error(17);
end (*repeatstatement*) ;
procedure whilestatement;
var x: item; lc1,lc2: integer;
begin insymbol; lc1 := lc;
expression(fsys+[dosy], x);
if not (x.typ in [bools,notyp]) then error(17);
if sy = dosy then insymbol else error(54);
statement(fsys); emit1(10,lc1); code[lc2].y := lc
begin i := loc(id); insymbol;
if i = 0 then cvt := ints else
if tab[i].obj = variable then
if not tab[i].normal then error(37) else
emit2(0, tab[i].lev, tab[i].adr);
if not (cvt in [notyp,ints,bools,chars]) then error(18)
begin error(37); cvt := ints
end else skip([becomes,tosy,downtosy,dosy]+fsys, 2);
begin insymbol; expression([tosy,downtosy,dosy]+fsys, x);
if x.typ <> cvt then error(19);
end else skip([tosy,downtosy,dosy]+fsys, 51);
if sy in [tosy, downtosy] then
begin if sy = downtosy then f := 16;
insymbol; expression([dosy]+fsys, x);
if x.typ <> cvt then error(19)
end else skip([dosy]+fsys, 55);
if sy = dosy then insymbol else error(54);
lc2 := lc; statement(fsys);
emit1(f+1,lc2); code[lc1].y := lc
procedure standproc(n: integer);
begin error(20); iflag := true
if sy <> ident then error(2) else
begin i := loc(id); insymbol;
if tab[i].obj <> variable then error(37) else
begin x.typ := tab[i].typ; x.ref := tab[i].ref;
if tab[i].normal then f := 0 else f := 1;
emit2(f, tab[i].lev, tab[i].adr);
if sy in [lbrack,lparent,period] then
selector(fsys+[comma,rparent], x);
if x.typ in [ints,reals,chars,notyp] then
emit1(27, ord(x.typ)) else error(40)
test([comma,rparent], fsys, 6);
if sy = rparent then insymbol else error(4)
begin emit1(24,sleng); emit1(28,inum); insymbol
begin expression(fsys+[comma,colon,rparent], x);
if not (x.typ in stantyps) then error(41);
expression(fsys+[comma,colon,rparent], y);
if y.typ <> ints then error(43);
begin if x.typ <> reals then error(42);
insymbol; expression(fsys+[comma,rparent], y);
if y.typ <> ints then error(43);
else emit1(30, ord(x.typ))
else emit1(29, ord(x.typ))
if sy = rparent then insymbol else error(4)
if sy in statbegsys+[ident] then
ident: begin i := loc(id); insymbol;
konstant, type1: error(45);
variable: assignment(tab[i].lev, tab[i].adr);
if tab[i].lev <> 0 then call(fsys, i)
else standproc(tab[i].adr);
if tab[i].ref = display[level] then
assignment(tab[i].lev+1, 0) else error(45)
beginsy: compoundstatement;
repeatsy: repeatstatement;
begin (*block*) dx := 5; prt := t;
if level > lmax then fatal(5);
test([lparent,colon,semicolon], fsys, 7);
enterblock; display[level] := b; prb := b;
tab[prt].typ := notyp; tab[prt].ref := prb;
if sy = lparent then parameterlist;
btab[prb].lastpar := t; btab[prb].psize := dx;
begin insymbol; (*function type*)
begin x := loc(id); insymbol;
if tab[x].obj <> type1 then error(29) else
if tab[x].typ in stantyps then tab[prt].typ := tab[x].typ
end else skip([semicolon]+fsys, 2)
if sy = semicolon then insymbol else error(14);
if sy = constsy then constantdeclaration;
if sy = typesy then typedeclaration;
if sy = varsy then variabledeclaration;
while sy in [proceduresy,functionsy] do procdeclaration;
test([beginsy], blockbegsys+statbegsys, 56)
insymbol; statement([semicolon,endsy]+fsys);
while sy in [semicolon]+statbegsys do
begin if sy = semicolon then insymbol else error(14);
statement([semicolon,endsy]+fsys)
if sy = endsy then insymbol else error(57);
test(fsys+[period], [], 6)