Created
May 8, 2010 03:10
-
-
Save thiagofm/394284 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string> | |
#include <iostream> | |
#include <cstdlib> | |
#include <cmath> | |
using namespace std; | |
class AnalisadorLexico | |
{ | |
public: | |
int atual; | |
int valorAtual; | |
string variavelAtual; | |
string s; | |
string sAtual; | |
int qtdVal; | |
int qtdVar; | |
int topoPilha; | |
int passado; | |
int pilha[100]; | |
typedef struct { | |
string var; | |
double val; | |
} tabela[100]; | |
tabela tab; | |
AnalisadorLexico::AnalisadorLexico(char x[]) | |
{ | |
qtdVal=0; | |
passado=0; //se passado = 1, a seq passada era val, serve pra saber se o valor deve ser atribuido ou nao | |
qtdVar=0; | |
atual=0; | |
valorAtual=0; | |
variavelAtual=""; | |
sAtual=""; | |
s=x; | |
topoPilha=-1; | |
e0(); | |
} | |
private: | |
//push(v),pop(),newstack() <-pilha | |
void AnalisadorLexico::push(int v){ | |
topoPilha++; | |
pilha[topoPilha]=v; | |
} | |
int AnalisadorLexico::pop(){ | |
int valortopo=pilha[topoPilha]; | |
topoPilha--; | |
return valortopo; | |
} | |
void AnalisadorLexico::newStack(){ | |
topoPilha=-1; | |
} | |
int AnalisadorLexico::eNumero(char s) | |
{ | |
if ( (((char)s)>=48) && (((char)s)<=57) ){return 1;} | |
return 0; | |
} | |
int AnalisadorLexico::eLetra(char s) | |
{ | |
if ( (((char)s)>=65) && (((char)s)<=90) || (((char)s)>=97) && (((char)s)<=122) ){return 1;} | |
return 0; | |
} | |
int AnalisadorLexico::eEspaco(char s) | |
{ | |
if ( (((char)s)==32) ){return 1;} | |
return 0; | |
} | |
int AnalisadorLexico::eIgual(char s) | |
{ | |
if ( (((char)s)==61) ){return 1;} | |
return 0; | |
} | |
void AnalisadorLexico::sucesso() | |
{ | |
for(int i=0;i<qtdVar;i++){ | |
cout << "v(" << tab[i].var << ") ... n(" << tab[i].val << ")" << endl; | |
} | |
system("PAUSE"); | |
} | |
bool AnalisadorLexico::varExisteQ() | |
{ | |
for(int i=0;i<=qtdVar;i++){ | |
if( tab[i].var == variavelAtual ){return true;} | |
} | |
return false; | |
} | |
//push(v),pop(),newstack() <-pilha | |
void AnalisadorLexico::adicionarVal() | |
{ | |
int velhoTopo=topoPilha-1; | |
tab[qtdVar-1].val=0; | |
while(topoPilha!=-1){ | |
//printf("A%dB",pop()*((int)pow(10,((double)(velhoTopo-topoPilha))))); | |
tab[qtdVar-1].val+=pop()*(pow(10,((double)(velhoTopo-topoPilha)))); | |
} | |
passado=0; | |
} | |
void AnalisadorLexico::adicionarVar() | |
{ | |
string vIF ("if"); | |
string vELSE ("else"); | |
if(!varExisteQ() && (vIF.compare(variavelAtual)!=0) && (vELSE.compare(variavelAtual)!=0) ){ | |
tab[qtdVar].var=variavelAtual; | |
tab[qtdVar].val=0; | |
qtdVar++; | |
} | |
variavelAtual=""; | |
passado=1; | |
} | |
void AnalisadorLexico::e0() | |
{ | |
if (atual == s.size()) | |
{ | |
sucesso(); | |
} | |
if ( eIgual(s[atual]) && passado==1 ) | |
{ | |
atual++; | |
e1 (); | |
} | |
else if ( eLetra(s[atual]) ) | |
{ | |
e2(); | |
} | |
else if ( !eLetra(s[atual]) || eNumero(s[atual]) ) | |
{ | |
passado=0; | |
atual++; | |
e0(); | |
} | |
} | |
int AnalisadorLexico::e1() | |
{ | |
if (atual == s.size()) | |
{ | |
adicionarVal(); | |
sucesso(); | |
} | |
if ( eNumero(s[atual]) ) | |
{ | |
push(((int)s[atual])-48); | |
atual++; | |
e1(); | |
} | |
//push(v),pop(),newstack() <-pilha | |
else if ( !eNumero(s[atual]) ) | |
{ | |
//salva o conteudo | |
adicionarVal(); | |
e0(); | |
} | |
} | |
int AnalisadorLexico::e2() | |
{ | |
if (atual == s.size()) | |
{ | |
adicionarVar(); //caso seja o fim, adiciona var | |
sucesso(); | |
} | |
if ( eLetra(s[atual]) || eNumero(s[atual]) ) | |
{ | |
variavelAtual+=s[atual]; | |
atual++; | |
e2(); | |
} | |
else if ( !eLetra(s[atual]) && !eNumero(s[atual]) ) | |
{ | |
adicionarVar(); | |
e0(); | |
} | |
} | |
}; | |
int main(int argc, char** argv) | |
{ | |
char s[100]; | |
printf("Digite o codigo: "); | |
gets (s); | |
AnalisadorLexico AL(s); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment