Visualizzazione dei risultati da 1 a 8 su 8
  1. #1
    Utente di HTML.it
    Registrato dal
    Aug 2017
    Messaggi
    7

    [FLEX+BISON] Calcolo di espressioni booleane

    Salve, qualcuno di voi conosce questi due linguaggi? Ho bisogno di aiuto al riguardo per un progetto perchè non riesco a trovare un errore.
    HELP!!! Sono disperata!!

  2. #2
    Utente di HTML.it L'avatar di Scara95
    Registrato dal
    Jul 2009
    residenza
    Zimella (VR)
    Messaggi
    2,590
    Non c'è nessuna informazione utile per darti una mano, leggi il regolamento
    "Quid enim est, quod contra vim sine vi fieri possit?" - Cicerone, Ad Familiares

  3. #3
    Utente di HTML.it
    Registrato dal
    Aug 2017
    Messaggi
    7
    Il mio progetto è questo: Si costruisca, utilizzando la coppia di programmi FLEX e BISON, un programma in grado diriconoscere un linguaggio che permette di calcolare delle espressioni booleane.Il file in input (specificato come parametro nella riga di comando) è
    composto da un’intestazione seguita da una lista di operazioni eseguite su variabili booleane.
    Un esempio di intestazione è il seguente:
    Data: 30/maggio/2016;

    Nell’intestazione è contenuta una data.


    Il mio codice in FLEX è questo:
    %{
    #include "boolean.tab.h"
    #include <string.h>
    %}


    %option noyywrap


    %%


    [0-9]+"/"[0-9]+"/"[0-9]+ return DATANUMERICA;
    Data return DATA;
    VAR { yylval.string = strdup(yytext); return VAR; }
    AND return AND;
    OR return OR;
    NOT return NOT;
    FALSE return FALSE;
    TRUE return TRUE;
    [_a-zA-Z0-9]+ return ID;
    [():;,/] return *yytext;
    [ \n\t\r] ;


    %%
    Il codice scritto in BISON:
    %{
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #define HASHSIZE 101


    /*Definizione della Struttura della Symbol Table*/
    struct nlist {
    struct nlist *next;
    char *name;
    int value;
    };


    struct nlist *hashtab[HASHSIZE];


    unsigned int hash(char *s);


    struct nlist *lookup(char *s);
    struct nlist *ins_attr(char *s,int i);
    struct nlist *ins_token(char *name) ;


    void yyerror (char const *);
    int curr_attr;
    %}




    %token DATANUMERICA DATA FALSE TRUE ID AND OR NOT '(' ')' ',' ';' ':'
    %token <string> VAR
    %type <boolean> truth_value
    %type <boolean> expr




    %union {
    int boolean;
    char *string;
    }


    %left OR
    %left AND
    %right NOT


    %start program


    %%


    program: date_declaration list_expr;


    date_declaration:
    DATA ':' DATANUMERICA ';'
    ;


    list_expr:
    /* empty expression */
    |
    expr maybe_vars ';' list_expr

    ;


    maybe_vars:
    /* empty list of vars */
    |
    ':' nonempty_list_vars
    ;


    nonempty_list_vars:
    VAR ID {
    ins_token($1);
    ins_attr($1, curr_attr);
    if (curr_attr)
    printf("%s=TRUE;",$1);
    else
    printf("%s=FALSE;",$1);
    }
    |
    VAR ID ',' nonempty_list_vars {
    ins_token($1);
    ins_attr($1, curr_attr);
    if (curr_attr)
    printf("%s=TRUE;",$1);
    else
    printf("%s=FALSE;",$1);
    }
    ;


    truth_value:
    TRUE { $$ = 1; }
    |
    FALSE { $$ = 0; }
    ;


    expr:
    ID { $$=1; curr_attr = $$; }
    |
    truth_value { $$=$1; curr_attr = $$; }
    |
    '(' expr ')' { $$=$2; curr_attr = $$; }
    |
    expr OR expr { $$=($1 || $3); curr_attr = $$; }
    |
    expr AND expr { $$=($1 && $3); curr_attr = $$; }
    |
    NOT expr { $$=!$2; curr_attr = $$; }
    ;

    %%


    void yyerror (char const *s) {
    fprintf(stderr, "%s\n", s);
    }


    int main() {
    yyparse();
    return 0;
    }


    unsigned int hash(char *s) {
    /* calcolare HASH di s;*/
    int h=0;
    for(;*s!='\0';s++)
    h=(127*h+*s)%HASHSIZE;
    return h;

    }


    struct nlist *lookup(char *s) {
    /* cercare s nella tabella HASH */
    struct nlist *np;
    for (np = hashtab[hash(s)]; np != NULL;np = np->next)
    if (strcmp(s,np->name) == 0)
    return np;
    return NULL;
    }


    struct nlist *ins_attr(char *s,int i) {
    /* cercare s nella tabella HASH e inserisce l'attributo i*/
    struct nlist *np;
    np=lookup(s);
    if(np!=NULL){
    np->value=i;
    return np;
    }
    return NULL;
    }


    struct nlist *ins_token(char *name) {
    /* inserire l'elemento di token */
    struct nlist *np;
    unsigned hashval;
    if ((np = lookup(name)) == NULL) {/*se non è già presente*/
    if ((np = (struct nlist *)malloc(sizeof(*np)))==NULL)
    return NULL;
    np->name = strdup(name);
    np->value=0;
    hashval = hash(name);
    np->next = hashtab[hashval];
    hashtab[hashval] = np;
    }
    return np;

    }
    Come output mi dovrebbe spuntare (preso in input un file txt contenente le espressioni booleane) un esempio del tipo:
    a1=FALSE; B=FALSE; c=FALSE;
    d_2=TRUE;
    FALSE;
    F=TRUE; h=TRUE;
    m=FALSE;
    l=TRUE; i=TRUE;
    R=TRUE;


    e invece ottengo:
    VAR=FALSE; VAR=FALSE; VAR=FALSE;
    VAR=TRUE;
    FALSE;
    VAR=TRUE; VAR=TRUE;
    VAR=FALSE;
    VAR=TRUE; VAR=TRUE;
    VAR=TRUE;

    cioè tutte le variabili nominate con la dicitura "VAR" quando invece dovrebbero essere nominate come "a1, B, c, d_2" ecc...
    Dove sbaglio?

  4. #4
    Utente di HTML.it
    Registrato dal
    Aug 2017
    Messaggi
    7
    Esempio di input:
    Data: 30/aprile/2016;
    FALSE : VAR a1, VAR B, VAR c;
    TRUE : VAR d_2;
    NOT a1 AND B;
    NOT ( (a1 AND c) OR (B AND d_2) ) : VAR F, VAR h ;
    FALSE : VAR m;
    TRUE :VAR l, VAR i;
    F OR FALSE OR m OR TRUE OR TRUE : VAR R ;

    Esempio di output:
    a1=FALSE; B=FALSE; c=FALSE;
    d_2=TRUE;
    FALSE;
    F=TRUE; h=TRUE;
    m=FALSE;
    l=TRUE; i=TRUE;
    R=TRUE;


    e invece ottengo:
    VAR=FALSE; VAR=FALSE; VAR=FALSE;
    VAR=TRUE;
    FALSE;
    VAR=TRUE; VAR=TRUE;
    VAR=FALSE;
    VAR=TRUE; VAR=TRUE;
    VAR=TRUE;

    cioè tutte le variabili nominate con la dicitura "VAR" quando invece dovrebbero essere nominate come "a1, B, c, d_2" ecc...
    Dove sbaglio?

  5. #5
    Utente di HTML.it L'avatar di Scara95
    Registrato dal
    Jul 2009
    residenza
    Zimella (VR)
    Messaggi
    2,590
    Non ho letto tutto ma 100/100 questa riga è sbagliata, prova a pensare al perché:
    codice:
    VAR    { yylval.string = strdup(yytext); return VAR; }
    "Quid enim est, quod contra vim sine vi fieri possit?" - Cicerone, Ad Familiares

  6. #6
    Utente di HTML.it
    Registrato dal
    Aug 2017
    Messaggi
    7
    Quote Originariamente inviata da Scara95 Visualizza il messaggio
    Non ho letto tutto ma 100/100 questa riga è sbagliata, prova a pensare al perché:
    codice:
    VAR    { yylval.string = strdup(yytext); return VAR; }
    Stampa la parola VAR e non l'identificatore?

  7. #7
    Utente di HTML.it
    Registrato dal
    Aug 2017
    Messaggi
    7
    [0-9]+"/"[0-9]+"/"[0-9]+ return DATANUMERICA;Data return DATA;
    VAR return VAR;
    AND return AND;
    OR return OR;
    NOT return NOT;
    FALSE return FALSE;
    TRUE return TRUE;
    [_a-zA-Z0-9]+ { yylval.string = strdup(yytext); return ID; }
    [():;,/] return *yytext;
    [ \n\t\r] ;
    ho modificato così, mi stampa :
    B=FALSE; a1=FALSE;
    e poi mi va in crash

  8. #8
    Utente di HTML.it
    Registrato dal
    Aug 2017
    Messaggi
    7
    Alla fine ci sono riuscita però altro errore , cioè mi stampa così:
    c=FALSE; B=FALSE; a1=FALSE; d_2=TRUE; h=TRUE; F=TRUE;m=FALSE;i=TRUE; l=TRUE;R=TRUE;


    messi al contrario come se partisse alla fine per poi tornare in testa

    Ho provato a modificare l'ultima parte del file di Bison , nella parte in C, così:
    struct nlist *ins_token(char *name) { /* inserire l'elemento di token */
    struct nlist *np, *p_1;
    unsigned hashval;
    if ((np = lookup(name)) == NULL) { /*se non è già presente*/
    if ((np = (struct nlist *)malloc(sizeof(*np)))==NULL)
    return NULL;
    np->name = strdup(name);
    np->value=0;
    np->next= NULL;
    hashval = hash(name);
    p_1= hashtab[hashval];
    while (p_1 != NULL && p_1->next != NULL)
    p_1 = p_1->next;
    if(p_1 == NULL)
    hashtab[hashval] = np;
    else
    p_1->next = np;
    }
    return hashtab[hashval];

    }


    ma niente, mi stampa i primi due (al contrario) e poi va in crash!
    Non capisco dove sia l'errore!
    HELP!!!

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.