编译原理实验
递归子程序法:
对于每个非终结符,编写一个子程序,由该子程序负责识别该语法单位是否正确。表达式的文法
〈表达式〉∷=[+|-]〈项〉{(+|-)〈项〉}
〈项〉∷=〈因子〉{(*|/)〈因子〉}
〈因子〉∷=〈标识符〉|〈无符号整数〉|‘(’〈表达式〉‘)’
〈表达式〉的递归子程序实现
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