class Nodo {
    constructor(dato) {
        this.dato = dato;
        this.siguiente = null; 
    }
}


class Pila {
    constructor() {
        this.cima = null;
    }

   
    estaVacia() {
        return this.cima === null;
    }

    // Insertar un elemento en la cima
    push(dato) {
        const nuevo = new Nodo(dato);
        nuevo.siguiente = this.cima; 
        this.cima = nuevo;          
    }

    // Sacar y leer el elemento de la cima
    pop() {
        if (this.estaVacia()) return null;
        
        const dato = this.cima.dato;     
        this.cima = this.cima.siguiente; 
        return dato;
    }
}

// Función validadora
function validarEcuacion(ecuacion) {
    
    console.log(`ANALIZANDO ECUACIÓN: ${ecuacion}`);
    

    const auxiliar = new Pila();
    let hayError = false;

   
    for (let letra of ecuacion) {
        
        // Si es de APERTURA (PUSH)
        if (letra === "(" || letra === "[" || letra === "{") {
            console.log(`PUSH: Entra '${letra}' a la pila`);
            auxiliar.push(letra);
        } 
        // Si es de CIERRE (POP)
        else if (letra === ")" || letra === "]" || letra === "}") {
            
            // Si la pila está vacía y queremos cerrar algo: ¡Error!
            if (auxiliar.estaVacia()) {
                console.log(`ERROR: Pila vacía al intentar cerrar '${letra}'`);
                hayError = true; 
                break;
            }

            // Sacamos al de la cima para compararlo
            let cima = auxiliar.pop();
            console.log(`POP: Sale '${cima}' para emparejar con '${letra}'`);

            // Comparamos si las parejas son inválidas
            if (letra === ")" && cima !== "(") { hayError = true; break; }
            if (letra === "]" && cima !== "[") { hayError = true; break; }
            if (letra === "}" && cima !== "{") { hayError = true; break; }
        }
    } 

    // VEREDICTO FINAL
    
    if (!hayError && auxiliar.estaVacia()) {
        console.log("Sinataxis perfecta\n");
    } else {
        console.log("Error de sintaxis\n");
    }
}



validarEcuacion("[(3+5)*(10+2)]");

validarEcuacion("{[2+5]*(3-2)}");

validarEcuacion("[(3+5]");

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: