end
end
begin
end
如何检查出是否配对正确,错误情形很多,
1、缺少end与之配对
begin
//end
2、缺少begin与之配对
//begin
end
3、多end或者多begin
begin
begin
end
end
end
类似这些错误,如何检查出这些错误呢
...全文
16117打赏收藏
求一配对算法如何检错,集思广益
如:begin与end需配对使用, begin begin end end begin end 如何检查出是否配对正确,错误情形很多, 1、缺少end与之配对 begin //end 2、缺少begin与之配对 //begin end 3、多end或者多begin begin begin end end end 类似这些错误,如何检查出这些错误呢
差不多吧,当栈里面没有东西还要popup就报错,更简单的可以用一个变量
i:=0;
while file is not EOF do
begin
read next word;
if next word is 'begin' then
i:=i+1;
if next word is 'end' then
i:= i-1;
if i<0 then
raise error
end;
麻烦的倒是要先清注释,再清掉字符串常量[/quote]
最后再加一个判断,
if i>0 then //begin太多[/quote]
是的,问题基本解决了
差不多吧,当栈里面没有东西还要popup就报错,更简单的可以用一个变量
i:=0;
while file is not EOF do
begin
read next word;
if next word is 'begin' then
i:=i+1;
if next word is 'end' then
i:= i-1;
if i<0 then
raise error
end;
麻烦的倒是要先清注释,再清掉字符串常量[/quote]
这样还是有问题吧,如
begin
end
end
end
end
begin
begin
begin
检测不出来的,应该是楼上说的就近原则,最近的begin配最近的end,程序用什么算法好实现呢?[/quote]
它在第2个end的地方i就小于0了,然后直接raise error[/quote]
差不多吧,当栈里面没有东西还要popup就报错,更简单的可以用一个变量
i:=0;
while file is not EOF do
begin
read next word;
if next word is 'begin' then
i:=i+1;
if next word is 'end' then
i:= i-1;
if i<0 then
raise error
end;
麻烦的倒是要先清注释,再清掉字符串常量[/quote]
最后再加一个判断,
if i>0 then //begin太多
差不多吧,当栈里面没有东西还要popup就报错,更简单的可以用一个变量
i:=0;
while file is not EOF do
begin
read next word;
if next word is 'begin' then
i:=i+1;
if next word is 'end' then
i:= i-1;
if i<0 then
raise error
end;
麻烦的倒是要先清注释,再清掉字符串常量[/quote]
这样还是有问题吧,如
begin
end
end
end
end
begin
begin
begin
检测不出来的,应该是楼上说的就近原则,最近的begin配最近的end,程序用什么算法好实现呢?[/quote]
它在第2个end的地方i就小于0了,然后直接raise error
差不多吧,当栈里面没有东西还要popup就报错,更简单的可以用一个变量
i:=0;
while file is not EOF do
begin
read next word;
if next word is 'begin' then
i:=i+1;
if next word is 'end' then
i:= i-1;
if i<0 then
raise error
end;
麻烦的倒是要先清注释,再清掉字符串常量[/quote]
这样还是有问题吧,如
begin
end
end
end
end
begin
begin
begin
检测不出来的,应该是楼上说的就近原则,最近的begin配最近的end,程序用什么算法好实现呢?
差不多吧,当栈里面没有东西还要popup就报错,更简单的可以用一个变量
i:=0;
while file is not EOF do
begin
read next word;
if next word is 'begin' then
i:=i+1;
if next word is 'end' then
i:= i-1;
if i<0 then
raise error
end;
麻烦的倒是要先清注释,再清掉字符串常量