Ver Mensaje Individual
  #3 (permalink)  
Antiguo 27/03/2006, 19:38
vvblond
 
Fecha de Ingreso: diciembre-2005
Ubicación: Ciudad de México
Mensajes: 45
Antigüedad: 19 años, 2 meses
Puntos: 1
Mira la primera duda que me surge es que tipo de analizador requieres, y si no te pidieron ningún tipo en especial te recomiendo que utilices una herramienta llamada yacc. Si no puedes utilizar esta herramienta puedes hacer un analizador por decenso recursivo aquí te presento el código de uno de estos:

Código:
#ifndef _DESCREC2_H_
#define _DESCREC2_H_ 

#include "scanner.h"
#include "tokens.h"

class DescRec{
    public:
    int E(float *ApV,Scanner *scan);
    int Ep(float *ApV,Scanner *scan);
    int T(float *ApV,Scanner *scan);
    int Tp(float *ApV,Scanner *scan);
    int S(float *ApV,Scanner *scan);
    int Sp(float *ApV,Scanner *scan);
    int P(float *ApV,Scanner *scan);
    int F(float *ApV,Scanner *scan);
};
#endif
Código:
#include "descrec2.h"

#include <stdlib.h>
#include <gtk/gtk.h>
#include <math.h>

/*
int DescRec::E(float *ApV,Scanner *scan){
    g_print("-------------inicia----------------\n");
    if(T(ApV,scan))
        if(Ep(ApV,scan))
            return 1;
    return 0;
}

int DescRec::Ep(float *ApV,Scanner *scan){
    int t;
    float aux;
    t=scan->yylex();
    g_print("EP -sig %d\n",t);
    if(t==SUMA || t==RESTA){
        if(T(&aux,scan)){
            if(t==SUMA)
                *ApV=(*ApV+aux);
            
            if(t==RESTA)
                *ApV=(*ApV-aux);
                
            return Ep(ApV,scan);
        }
        return 0;
    }
    scan->regresaToken();
    return 1;
}

int DescRec::T(float *ApV,Scanner *scan){
    if(F(ApV,scan))
        if(Tp(ApV,scan))
            return 1;
    return 0;
}

int DescRec::Tp(float *ApV,Scanner *scan){
    int t;
    float aux;
    t=scan->yylex();
    g_print("TP -sig %d\n",t);
    if(t==PROD || t==DIV){
        if(F(&aux,scan)){
            if(t==PROD)
                *ApV=*ApV*aux;
            if(t==DIV)
                *ApV=*ApV/aux;
                
            return Tp(ApV,scan);
        }
        return 0;
    }
    scan->regresaToken();
    return 1;
}

int DescRec::F(float *ApV,Scanner *scan){
    int t;
    t=scan->yylex();
    g_print("F -sig %d\n",t);
    switch(t){
        case PAR_I:if(E(ApV,scan)){
                t=scan->yylex();
                if(t==PAR_D)
                    return 1;
            }
            return 0;
        case NUM:*ApV=atof(scan->yytext);
            return 1;
        
    }
    return 0;
}*/



int DescRec::E(float *ApV,Scanner *scan){
    if(T(ApV,scan))
        if(Ep(ApV,scan)){
            return 1;
        }
    return 0;
}

int DescRec::Ep(float *ApV,Scanner *scan){
    int t;
    float aux;
    t=scan->yylex();
    if(t==SUMA || t==RESTA){
        if(T(&aux,scan)){
            if(t==SUMA)
                *ApV=(*ApV+aux);
            
            if(t==RESTA)
                *ApV=(*ApV-aux);
                
            return Ep(ApV,scan);
        }
        return 0;
    }
    scan->regresaToken();
    return 1;
}

int DescRec::T(float *ApV,Scanner *scan){
    if(S(ApV,scan))
        if(Tp(ApV,scan))
            return 1;
    return 0;
}

int DescRec::Tp(float *ApV,Scanner *scan){
    int t;
    float aux;
    t=scan->yylex();
    if(t==PROD || t==DIV){
        if(F(&aux,scan)){
            if(t==PROD)
                *ApV=*ApV*aux;
            if(t==DIV)
                *ApV=*ApV/aux;
                
            return Tp(ApV,scan);
        }
        return 0;
    }
    scan->regresaToken();
    return 1;
}

int DescRec::S(float *ApV,Scanner *scan){
    if(P(ApV,scan))
        if(Sp(ApV,scan))
            return 1;
    return 0;
}

int DescRec::Sp(float *ApV,Scanner *scan){
    int t;
    float aux;
    t=scan->yylex();
    if(t==POT || t==RAIZ){
        if(P(&aux,scan)){
            if(t==POT)
                *ApV=powf(*ApV,aux);
            
            if(t==RAIZ)
                *ApV=sqrtf(aux);
                
            return Sp(ApV,scan);
        }
        return 0;
    }
    scan->regresaToken();
    return 1;
}

int DescRec::P(float *ApV,Scanner *scan){
    int t,l;
    float aux;
    t=scan->yylex();
    if(t==SIN || t==COS || t==TAN || t==LOG || t==LN || t==EXP){
        if(F(&aux,scan)){
            if(t==SIN)
                *ApV=sinf(aux);
            if(t==COS)
                *ApV=cosf(aux);
            if(t==TAN)
                *ApV=tanf(aux);
            if(t==LOG)
                *ApV=log10f(aux);
            if(t==LN)
                *ApV=logf(aux);
            if(t==EXP)
                *ApV=expf(aux);
            return 1;
        }
        return 0;
    }
    scan->regresaToken();
    return F(ApV,scan);
}

int DescRec::F(float *ApV,Scanner *scan){
    int t;
    t=scan->yylex();
    switch(t){
        case PAR_I:if(E(ApV,scan)){
                t=scan->yylex();
                if(t==PAR_D)
                    return 1;
            }
            return 0;
        case NUM:*ApV=atof(scan->yytext);
            return 1;
        /*case VAR_X:*ApV=atof(scan->yytext);
            return 1;
        case VAR_Y:*ApV=atof(scan->yytext);
            return 1;*/
    }
    return 0;
}
este código corresponde a un analizador sintáctico para realizar algunas operaciones matemáticas