Help with Lexical Analyzer

Asked

Viewed 147 times

0

Good evening everyone. I’m implementing a c/c++ lexical analysis, but I’m having a problem with the output. I was asked to leave the program with only 3 variables and 2 operands, plus 1 delimiter and 1 equality.

But I’ve tried everything and I can’t do it. If I put to print only 3 variables, the program "eats" one of them. Below follows the code of what I have already done. This analyzer will have to deliver tomorrow, so if anyone can help me I will be very grateful.

#include <stdlib.h> 
#include <string.h> 
#include <iostream> 
#include <iomanip> 
using namespace std; // contexto onde estão definidos macros e variáveis.

int main()
{

char frase[50], support[50];
int u=0,ct11=0,ct10=0,ct9=0,ct8=0,ct7=0,ct6=0,ct5=0,ct4=0 ,ct3=0 ,ct2=0,ct1_1=0,ct1=0,ct13=0,ct12=0,ct14=0,ct15=0,ct16=0,ct11_1=0,ct17=0; 
cout << "Digite a expressao: ";
cin.getline (frase,50) ; 
cout << setw(15) <<"Token" << setw(30) << "Lexema";
 for (int x = 0; x < strlen(frase); x ++){
    if (isalpha(frase[x])) {
            if (islower(frase[x])){
                cout<<"\n Identificador minusculo " << ct1;
                cout<<": \t\t"<< frase[x]; 
                ct1++;
            }
            if (isupper(frase[x])){
                cout<<"\n Identificador maisculo " << ct1_1;
                cout<<": \t\t"<< frase[x]; 
                ct1_1++;
            }

    }else if (isdigit(frase[x])){
        cout<<"\n Numero " << ct2;
        cout<<":\t\t\t\t"<< frase[x]; 
        ct2++;
    }
    else if (isspace(frase[x])) {
         cout<<"\n Espaco " <<ct3;
         cout<<":\t\t\t\t"<< frase[x];
         ct3++;
    }
    else if (frase[x] =='*' && frase[x+1] == '*') {
            frase[x] = '^';
            frase[u] = frase[x];
            frase[x+1] = '.';
            support[ct11] = frase[u];
                 cout<<"\n Potenciacao " <<ct11;
                cout<<":\t\t\t\t"<< support[ct11];
                ct11++;
            }
    else if (frase[x] =='!' && frase[x+1] == '=') {
            frase[x] = '\\';
            frase[u] = frase[x];
            frase[x+1] = '.';
            support[ct11_1] = frase[u];
                 cout<<"\n Diferenca " <<ct11_1;
                cout<<":\t\t\t\t"<< support[ct11_1];
                ct11_1++;

            }


    else if (ispunct(frase[x])){
        if (frase[x] == '(' || frase[x] == ')'){
            cout<<"\n Ordem de prioridade 1 " <<ct12;
            cout<<":\t\t"<< frase[x];
            ct12++;
        }
        if (frase[x] == '[' || frase[x] == ']'){
            cout<<"\n Ordem de prioridade 2 " <<ct13;
            cout<<":\t\t"<< frase[x];
            ct13++;
        }
        if (frase[x] == '{' || frase[x] == '}'){
            cout<<"\n Ordem de prioridade 3 " <<ct14;
            cout<<":\t\t"<< frase[x];
            ct14++;
        }
        if (frase[x] == '<'){
            cout<<"\n Menor que " <<ct16;
            cout<<":\t\t\t\t"<< frase[x];
            ct16++;
        }
        if (frase[x] == '>'){
            cout<<"\n Maior que " <<ct17;
            cout<<":\t\t\t\t"<< frase[x];
            ct17++;
        }
        if (frase[x] == '='){
            cout<<"\n Igualdade " <<ct4;
            cout<<":\t\t\t\t"<< frase[x];
            ct4++;
        }
        if (frase[x] == '^'){
            cout<<"\n Potenciacao " <<ct5;
            cout<<":\t\t\t\t"<< frase[x];
            ct5++;
        }
        if (frase[x] == '/'){
            cout<<"\n Operador de divisao " <<ct6;
            cout<<":\t\t\t"<< frase[x];
            ct6++;
        }
        if (frase[x] == '*'){
            cout<<"\n Operador de multiplicaco " <<ct7;
            cout<<":\t\t"<< frase[x];
            ct7++;
        }


        if (frase[x] == '+'){
            cout<<"\n Operador de adicao " <<ct8;
            cout<<":\t\t\t"<< frase[x];
            ct8++;  
        }
        if (frase[x] == '-'){
            cout<<"\n Operador de subtracao " <<ct9;
            cout<<":\t\t"<< frase[x];
            ct9++;  
        }
        if (frase[x] == ';') {
            cout<<"\n Delimitador "<<ct10;
            cout<<":\t\t\t\t"<< frase[x];
            ct10++;
        }
    }   



  }
    cout << '\n';
system("pause");
return 0;
}
  • I’m not going to read all your code because it’s big messed up and weird and mixed up. But is that not what’s causing this stop: if (x > 3){. Are you sure this is what you want? Even if it is, it’s weird.

  • I want to limit his output to 3 variables (letters) and 2 operands. I tried to put this if to see if the counter would stop when it reached more than 3. However it simply stops for the count. I’m not very good with programming, so I don’t know what to put right.

  • The code has a lot of weird stuff, at least, really big, so it’s hard to keep track. But run some tests on it to see if it changes anything.

  • I’ll post an earlier version that I did. See if it gets easier to understand the code.

  • 3

    "c/c++" Hmmm ... choose only one of these languages. Make one source that correctly Compile with compilers of two (or more) languages is difficult! Also the result is not good style either in one language or the other.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.