How to make a program to calculate constant mathematical expressions [duplicate]

6

I have a job to build an algorithm that calculates expressions coming from a TXT file in this format.

10 + 20 * (30) / 25.

I know that this is a recursive descending parser, but I have no idea where to start studying and how to do the program's analysis routine. The language used is C, materials, tips, are of good help. / p>

Program until then.

#include <stdio.h>
#include <stdlib.h>

void token(void) {




int main () {
char bse[100];
    FILE *file;
    file = fopen("conta.txt", "r");
    if(file == NULL) {
         printf("Não foi possivel abrir o arquivo");
         getchar();
         exit(0);
    }     
    while(fgets(bse, 100, file) != NULL) { // Leitura e impressão do conteudo do arquivo TXT
         printf("%s", bse);
    }
    fclose(file);     



 }

I have not done anything related to the downward recursive syntax itself. I just did the command to read the file and store it to a string. I am having second thoughts about how to do this parsing of expressions that would be the famous (recursive descending syntax).

    
asked by anonymous 27.10.2015 / 23:36

1 answer

0

Razor, I already implemented something similar in Compiler discipline, a bit more complex because the text was in written form (eg "three hundred and twenty-three thousand, two hundred and fifty-four plus fifteen times two"). For this implementation I used the Java language with the help of Jflex it is a lexical analyzer. Follow the main code, I hope it helps.

LineTerminator = \r|\n|\r\n
WhiteSpace     = {LineTerminator} | [ \t\f]

Number = [:digit:] [:digit:]*
pzero = "zero"
unidades = "um" | "dois" | "tres" | "quatro" | "cinco" | "seis" | "sete" | "oito" | "nove"
dezena = "onze" | "doze" | "treze" | "quatorze" | "quinze" | "dezesseis" | "dezessete" | "dezoito" | "dezenove"
dezenas = "vinte" | "trinta" | "quarenta" | "cinquenta" | "sessenta" | "setenta" | "oitenta" | "noventa"
pcem = "cem" | "duzentos" | "trezentos" | "catorze" | "quatrocentos" | "quinhentos" | "seiscentos" | "setecentos" | "oitocentos" | "novecentos"
centenas = "cento" | "duzentos" | "trezentos" | "quatrocentos" | "quinhentos" | "seiscentos" | "setecentos" | "oitocentos" | "novecentos"
dezuni = {dezenas}(" e "{unidades})
centdezuni = {centenas}( " e "{dezuni}) | {centenas}( " e "{dezenas}) | {centenas}( " e "{unidades}) | {centenas}( " e "{dezena})
juntos = {unidades} | {dezena} | {pcem} | {dezuni} | {centdezuni}
unidadedemilhar = {juntos}( " mil")( " , " {dezenas})*( " e " {unidades})* 
            | {juntos}( " mil")( " , " {pcem})
            | {juntos}( " mil")( " , " {centdezuni})


{WhiteSpace}+   { /* ignore */ }

<YYINITIAL> {
    {juntos}  { return newToken(Terminals.NUMBER, new String(yytext())); }
    {centdezuni}  { return newToken(Terminals.NUMBER, new String(yytext())); }
    {dezuni}    { return newToken(Terminals.NUMBER, new String(yytext())); }
    {pzero}    { return newToken(Terminals.NUMBER, new String(yytext())); }
    {pcem}    { return newToken(Terminals.NUMBER, new String(yytext())); }
{dezenas}    { return newToken(Terminals.NUMBER, new String(yytext())); }
    {dezena}    { return newToken(Terminals.NUMBER, new String(yytext())); }
    {centenas}    { return newToken(Terminals.NUMBER, new String(yytext())); }
    {unidadedemilhar}    { return newToken(Terminals.NUMBER, new String(yytext())); }

"("         { return newToken(Terminals.LPAREN); }
")"         { return newToken(Terminals.RPAREN); }
"vezes"         { return newToken(Terminals.MULT);   }
"dividido"         { return newToken(Terminals.DIV);    }
"mais"         { return newToken(Terminals.PLUS);   }
"menos"         { return newToken(Terminals.MINUS);  }
}

In short, to facilitate your work, use some lexical analyzer, I know there are some libraries for C. Any questions you can contact.

    
29.10.2015 / 00:09