编译原理实验
递归子程序法:
对于每个非终结符,编写一个子程序,由该子程序负责识别该语法单位是否正确。表达式的文法
〈表达式〉∷=[+|-]〈项〉{(+|-)〈项〉}
〈项〉∷=〈因子〉{(*|/)〈因子〉}
〈因子〉∷=〈标识符〉|〈无符号整数〉|‘(’〈表达式〉‘)’
〈表达式〉的递归子程序实现
procedure expr;
begin
if sym in [ plus, minus ] then
begin
getsym; term;
end
else term;
while sym in [plus, minus] do
begin
getsym; term;
end
end;
〈项〉∷=〈因子〉{(*|/)〈因子〉}
〈项〉的递归子程序实现
procedure term;
begin
factor;
while sym in [ times, slash ] do
begin
getsym; factor;
end
end;
〈因子〉∷=〈标识符〉|〈无符号整数〉|‘(’〈表达式〉‘)’
〈因子〉的递归子程序实现
procedure factor;
begin
if sym <> ident then
if sym <> number then
if sym = ‘(‘ then
begin
getsym;
expr;
if sym = ‘)’ then getsym
else error
end
else error
else getsym
else getsym
end;
10
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说教育文库编译原理_实验报告(10)在线全文阅读。
相关推荐: