(13,3) 致命:语法错误,“;”预期但 "ELSE" 找到

(13,3) Fatal: Syntax error, ";" expected but "ELSE" found

Program prueba1;
uses Estructu;
Var Pila1:Pila; Fila1,Completa:Fila;
Begin
    Inicfila (Completa);
    readpila(Pila1);
    readfila(Fila1);
    While (not pilavacia(Pila1) and not filavacia(Fila1)) do
    begin
        if (tope(Pila1) > primero(Fila1)) then
        begin
            agregar(Completa, desapilar(Pila1))
        else
            if (tope(Pila1) < primero(Fila1)) then
            begin
                agregar(Completa, extraer(Fila1))
            else
                if (tope(Pila1) = primero(Fila1)) then
                begin
                    agregar(Completa, desapilar(Pila1));
                    agregar(Completa, extraer(Fila1))
                end
            end
        end
    end
    write('El resultado final de Completa es');
    Writefila(Completa);
End.

该程序的目的是在 Completa 中按从头到尾的顺序组织来自 Pila1 和 Fila1 的所有变量。 我不知道自己做错了什么,希望得到帮助

您没有正确使用 if ... then ... elsebegin ... end

的概念,以begin开始,以end结束。任何需要单个 statement 的地方,您都可以放置 blockif <condition> then <statement> else <statement>;.

也是如此

因此,此代码有效:

if something() then
  stuff
else
  stuff;

...就像这样:

if something() then
  begin
    stuff;
    moreStuff;
  end
else
  begin
    otherStuff;
    moreOtherStuff;
  end;

但是,这个(您正在使用的)不是:

if something() then
  begin
    stuff // I guess here you omitted the semicolon because you correctly remembered
          // that there shouldn't be a semicolon before `else`, but...
else // WRONG, this is in the middle of the block!
    otherStuff;
  end;

为了了解原因,让我们修复缩进以匹配此代码的逻辑解释:

if something() then
  begin
    stuff
    else // ????????
    otherStuff;
  end;

你得到一个错误,因为 begin ... else ... end 不是一个有效的结构。由于 begin 但在 else 之前没有 end,因此您的 else 位于 then 块的中间,这没有任何意义。

确保在开始 else 部分之前 end 你的块,然后 begin 一个新块。