Related articles |
---|
which syntax-directed definition should I use? wangyin101@hotmail.com (yin wang) (2000-07-23) |
From: | "yin wang" <wangyin101@hotmail.com> |
Newsgroups: | comp.compilers |
Date: | 23 Jul 2000 17:01:27 -0400 |
Organization: | Compilers Central |
Keywords: | parse, code, question |
On syntax-directed translation, I've found that there are two ways of
marking the number of the quadruple in control flow statements.
Example, in grammar G:
S:if E then S
| if E then S else S
| while E do S
| begin L end
| A
L:L;S
|S
One way is to modify the grammar as follows (found in my text book published
in 1998):
S:C S1
|Tp S2
|Wd S3
|begin L end
|A
L:Ls S1
|S2
C:if E then
Tp:C S else
Wd:W E do
W:while
Ls:L;
and the syntax-directed definition is:
S:C S1 {S.nextlist:=merge(C.nextlist,S1.nextlist)}
|Tp S2 {S.nextlist:=merge(Tp.nextlist,S2.nextlist)}
|Wd S3 {backpatch(S3.nextlist, Wd.codebegin);
emit('goto' W.codebegin);
S.nextlist:=Wd.nextlist;
}
|begin L end {S.nextlist:=L.nextlist}
|A {S.nextlist:=Null}
L:Ls S1 {L.nextlist:=S1.nextlist}
|S2 {L.nextlist:=S2.nextlist}
C:if E then {backpatch(E.truelist, nextquad);
C.nextlist:=E.falselist
}
Tp:C S else {q:=nextquad;
emit('goto' - );
backpatch(C.nextlist,nextquad);
Tp.nextlist:=merge(S.nextlist,q)
}
Wd:W E do {W.codebegin:=nextquad}
W:while {W.codebegin:=nexequad}
Ls:L; {backpatch(L.nextlist,nextquad)}
Another method is to insert nonterminals that produce epsilon to the
productions (found in "Compiers - priciples,techniques,and tools" 1985 by
Aho):
S:if E then M1 S1 N else M2 S2
{ backpatch(E.truelist, M1.quad);
backpatch(E.falselist, M2.quad);
S.nextlist:=merge(S1.nextlist,N.nextlist,S2.nextlist)
}
N:epsilon
{N.nextlist:=makelist(nextquad);
emit('goto _')
}
M:epsilon
{M.quad:=nextquad}
S:if E then M S1 {backpatch(E.truelist,M.quad);
S.nextlist:=merge(E.falselist,S1.nextlist)
}
S:while M1 E do M2 S1 { backpatch(S1.nextlist,M1.quad);
backpatch(E.truelist, M2.quad);
S.nextlist:=E.falselist;
emit('goto' M1.quad)
}
S:begin L end {S.nextlist:=L.nextlist}
S:A {S.nextlist:=makelist()}
L:L1; M S {backpatch (L1.nextlist,M.quad);
L.nextlist:=S.nextlist
}
L:S {L.nextlist:=S.nextlist}
The first solution appear in a newer book, but it looks too ugly.
I think Aho's method is better. Can you tell me which solution should be
used?
Return to the
comp.compilers page.
Search the
comp.compilers archives again.