Advertisement
Guest User

ANALISADOR_LEXICO

a guest
Sep 23rd, 2014
1,407
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.07 KB | None | 0 0
  1. %{
  2. #include<stdio.h>
  3. #include<stdlib.h>
  4. #include<malloc.h>
  5. #include<string.h>
  6.  
  7. #define   QTD_SLOT 100
  8. #define   TAM_LEXEMA 100
  9. #define     IDENT       11
  10. #define     NUM         12
  11. #define     STRING      13
  12.  
  13. #define     OP_SOMA     21
  14. #define     OP_SUB      22 
  15. #define     OP_MULT     23 
  16. #define     OP_DIV      24
  17. #define     OP_EXP      24
  18. #define     OP_INCRE    25
  19. #define     OP_DECRE    26
  20.  
  21. #define     OP_AND      31
  22. #define     OP_OR       32
  23. #define     OP_NOT      33
  24.  
  25. #define     OP_MENOR        41
  26. #define     OP_MENOR_IGUAL  42
  27. #define     OP_MAIOR        43
  28. #define     OP_MAIOR_IGUAL  44
  29. #define     OP_IGUAL        45
  30. #define     OP_DIFERENTE    46
  31.  
  32. #define     OP_CONCAT   51
  33. #define     OP_ATRIB    52
  34.  
  35. #define     FUNC_SIZE   61
  36. #define     FUNC_NEW    62
  37. #define     FUNC_TO_I   63
  38. #define     FUNC_TO_F   64
  39. #define     FUNC_TO_S   65
  40. #define     COMENTARIO  66
  41. #define   COMENTARIO_LINHA  67
  42.  
  43. #define     SIMB_PARENT_E   71
  44. #define     SIMB_PARENT_D   72
  45. #define   SIMB_CHAVE_E 73
  46. #define   SIMB_CHAVE_D 74
  47. #define     SIMB_FIM_SETEN  75
  48. #define     SIMB_SEPARADOR  76
  49.  
  50. #define     ER_IF       81
  51. #define     ER_WHILE    82
  52. #define     ER_FOR      83
  53. #define     ER_DO       84
  54.  
  55. #define   PALAVRA_BOOL 90
  56.  
  57. /* ---------------------------------------------------------------------------*/
  58. /* ----- DEFINICAO DA ESTRUTURA NO DA LISTA ----------------------------------*/
  59. /* ---------------------------------------------------------------------------*/
  60. struct lista_no {  
  61.    char lexema[TAM_LEXEMA];
  62.    struct lista_no *prox;
  63. };
  64. typedef struct lista_no LISTA;
  65.  
  66. LISTA *tab[QTD_SLOT];
  67.  
  68. /* ---------------------------------------------------------------------------*/
  69. /* ----- PROTÓTIPO DAS FUNCOES -----------------------------------------------*/
  70. /* ---------------------------------------------------------------------------*/
  71.  
  72. LISTA *criar(void);
  73. LISTA *insere(LISTA * a, char* lexema);
  74. int hash(int v,int n);
  75. void imprimir(LISTA *a);
  76. void insertID(int slot,char* lexema);
  77.  
  78. %}
  79.  
  80. /*============ DEFINICAO DAS REGRAS DOS TOKENS A SEREM RECONHECIDOS ============*/
  81.  
  82. delim       [ \t\n]
  83. ws          {delim}+
  84. fim_setenca \;
  85. separador   \,
  86.  
  87. parent_e    \(
  88. parent_d    \)
  89. chave_e  \{
  90. chave_d  \}
  91. tudo        (.|\n)*
  92.  
  93. sinal     ([-+])?
  94. digito      [0-9]
  95. digitos     ({digito})*
  96. fracao      (\.{digitos})?
  97. expoente    ((E([+|-])?)?{digitos}+)?
  98. numeral     {sinal}{digitos}{fracao}{expoente}
  99.  
  100. carac_espec [^\n]
  101. letra       [a-zA-Z]
  102. letras      {letra}+
  103. ident       (_|{letra})({letras}|{digitos}|_)*
  104. comentario  \/\*{tudo}*\*\/
  105. comentario_linha \/\/([^\n])*
  106. string    ((\"){carac_espec}*(\"))
  107.  
  108. if          ([iI][fF])
  109. while       ([wW][hH][iI][lL][eE])
  110. for         ([fF][oO][rR])
  111. do          ([dD][oO])
  112.  
  113. and         ([aA][nN][dD])
  114. or          ([oO][rR])
  115. not         ([nN][oO][tT])
  116.  
  117. size        ([sS][iI][zZ][eE])
  118. to_i        ([tT][oO]_[iI])
  119. to_f        ([tT][oO]_[fF])
  120. to_s        ([tT][oO]_[sS])
  121. new         ([nN][eE][wW])
  122.  
  123.  
  124. /*============ DEFINICAO DAS ACOES A SEREM EXECUTADAS DEPOIS DE ENCONTRAR O PADRAO ============*/
  125. %%
  126.  
  127. "+"         {printf("\n<OP_SOMA,%s,%d>",yytext,OP_SOMA);return OP_SOMA;}   
  128. "-"         {printf("\n<OP_SUB,%s,%d>",yytext,OP_SUB);return OP_SUB;}  
  129. "*"         {printf("\n<OP_MULT,%s,%d>",yytext,OP_MULT);return OP_MULT;}   
  130. "/"         {printf("\n<OP_DIV,%s,%d>",yytext,OP_DIV);return OP_DIV;}  
  131. "^"         {printf("\n<OP_EXP,%s,%d>",yytext,OP_EXP);return OP_EXP;}
  132. "="         {printf("\n<OP_ATRIB,%s,%d>",yytext,OP_ATRIB);return OP_ATRIB;}
  133.  
  134. {and}       {printf("\n<OP_AND,%s,%d>",yytext,OP_AND);return OP_AND;}
  135. {or}        {printf("\n<OP_OR,%s,%d>",yytext,OP_OR);return OP_OR;}
  136. {not}       {printf("\n<OP_NOT,%s,%d>",yytext,OP_NOT);return OP_NOT;}
  137.  
  138. "<"         {printf("\n<OP_MENOR,%s,%d>",yytext,OP_MENOR);return OP_MENOR;}
  139. "<="        {printf("\n<OP_MENOR_IGUAL,%s,%d>",yytext,OP_MENOR_IGUAL);return OP_MENOR_IGUAL;}
  140. ">"         {printf("\n<OP_MAIOR,%s,%d>",yytext,OP_MAIOR);return OP_MAIOR;}
  141. ">="        {printf("\n<OP_MAIOR_IGUAL,%s,%d>",yytext,OP_MAIOR_IGUAL);return OP_MAIOR_IGUAL;}
  142. "=="        {printf("\n<OP_IGUAL,%s,%d>",yytext,OP_IGUAL);return OP_IGUAL;}
  143. "!="        {printf("\n<OP_DIFERENTE,%s,%d>",yytext,OP_DIFERENTE);return OP_DIFERENTE;}
  144.  
  145. "true"    {printf("\n<PALAVRA_BOOL,%s,%d>",yytext,PALAVRA_BOOL);return PALAVRA_BOOL;}
  146. "false"    {printf("\n<PALAVRA_BOOL,%s,%d>",yytext,PALAVRA_BOOL);return PALAVRA_BOOL;}
  147.  
  148. {size}      {printf("\n<FUNC_SIZE,%s,%d>",yytext,FUNC_SIZE);return FUNC_SIZE;}
  149. {to_i}      {printf("\n<FUNC_TO_I,%s,%d>",yytext,FUNC_TO_I);return FUNC_TO_I;}
  150. {to_f}      {printf("\n<FUNC_TO_F,%s,%d>",yytext,FUNC_TO_F);return FUNC_TO_F;}
  151. {to_s}      {printf("\n<FUNC_TO_S,%s,%d>",yytext,FUNC_TO_S);return FUNC_TO_S;}
  152. {new}         {printf("\n<FUNC_NEW,%s,%d>",yytext,FUNC_NEW);return FUNC_NEW;}
  153.  
  154. {if}          {printf("\n<EF_IF,%s,%d>",yytext,ER_IF);return ER_IF;}
  155. {while}     {printf("\n<EF_WHILE,%s,%d>",yytext,ER_WHILE);return ER_WHILE;}
  156. {for}       {printf("\n<EF_FOR,%s,%d>",yytext,ER_FOR);return ER_FOR;}
  157. {do}          {printf("\n<EF_DO,%s,%d>",yytext,ER_DO);return ER_DO;}
  158.  
  159. {parent_e}  {printf("\n<PARENTESE_E,%s,%d>",yytext,SIMB_PARENT_E);return SIMB_PARENT_E;}
  160. {parent_d}  {printf("\n<PARENTESE_D,%s,%d>",yytext,SIMB_PARENT_D);return SIMB_PARENT_D;}
  161. {chave_d}  {printf("\n<CHAVE_D,%s,%d>",yytext,SIMB_CHAVE_D);return SIMB_CHAVE_D;}
  162. {chave_e}  {printf("\n<CHAVE_E,%s,%d>",yytext,SIMB_CHAVE_E);return SIMB_CHAVE_E;}
  163. {fim_setenca}   {printf("\n<FIM_SETENCA,%s,%d>",yytext,SIMB_FIM_SETEN);return SIMB_FIM_SETEN;}
  164. {separador} {printf("\n<SIMB_SEPARADOR,%s,%d>",yytext,SIMB_SEPARADOR);return SIMB_SEPARADOR;}
  165.  
  166. {ws}        {/* nao executa nada*/}
  167. {comentario}    {printf("\n<COMENT_MULT_LINHA,%s,%d>",yytext,COMENTARIO);return COMENTARIO;}
  168. {comentario_linha}  {printf("\n<COMENT_LINHA,%s,%d>",yytext,COMENTARIO_LINHA);return COMENTARIO_LINHA;}
  169. {ident}         {printf("\n<IDENTIFICADOR,%s,%d>",yytext,IDENT);return IDENT;}
  170. {numeral}       {printf("\n<NUMERAL,%s,%d>",yytext,NUM);return NUM;}
  171. {string}    {printf("\n<STRING,%s,%d>",yytext,STRING);return STRING;}
  172. <<EOF>>     {printf("\n<EOF,%s,%d>",yytext,-1);return -1;}
  173. .  {printf("\n<SIMBOLO_INVALIDO,%s,%d>",yytext,-2);return -2;}
  174. %%
  175.  
  176. /* ---------------------------------------------------------------------------*/
  177. /* --------------------------------- MAIN ------------------------------------*/
  178. /* ---------------------------------------------------------------------------*/
  179.  
  180. main(void)
  181. {
  182.    int nextToken = -1;
  183.    int index = 0;    
  184.    LISTA *p;        
  185.  
  186.        /* ---------------------------------------------------------------------------*/    
  187.        /* --------------------------- CRIANDO A TABELA ------------------------------*/        
  188.        /* ---------------------------------------------------------------------------*/
  189.  
  190.        printf("\nCriando tabela...");  
  191.        // inicializando a tabela
  192.        for (index = 0; index < QTD_SLOT; index++){  
  193.            tab[index] = criar();
  194.        }  
  195.  
  196.        /* ---------------------------------------------------------------------------*/
  197.        /* --- ANALISA OS LEXEMAS RETORNADOS PELO LEX --------------------------------*/        
  198.        /* ---------------------------------------------------------------------------*/
  199.        while(nextToken = yylex()){                  
  200.          if(nextToken == -1){
  201.            break;
  202.          }else if(nextToken == -2){
  203.            printf("\n[ERRO] Lexema invalido.");
  204.          }
  205.          else if(yyleng > TAM_LEXEMA){
  206.            printf("\n[ERRO] Lexema com tamanho superior a 100.");
  207.          }
  208.          else if(yyleng == 0){
  209.              printf("\n[ERRO] Lexema não pertence a linguagem.");
  210.  
  211.          }
  212.          else if(
  213.            nextToken == IDENT
  214.            || nextToken == FUNC_SIZE
  215.            || nextToken == FUNC_NEW
  216.            || nextToken == FUNC_TO_I
  217.            || nextToken == FUNC_TO_S
  218.            || nextToken == FUNC_TO_F          
  219.            || nextToken == PALAVRA_BOOL
  220.            ){            
  221.                insertID(nextToken,yytext);        
  222.            }
  223.        }
  224.        
  225.        /* ---------------------------------------------------------------------------*/
  226.        /* ---------------------- IMPRIME A TABELA -----------------------------------*/
  227.        /* ---------------------------------------------------------------------------*/                            
  228.        printf("\n* --------------------------------------------------*");  
  229.        printf("\n* ---------------- IMPRIME A TABELA ----------------*");  
  230.        printf("\n* --------------------------------------------------*");  
  231.  
  232.        for(index=0;index<QTD_SLOT;index++){  
  233.          printf("\nSLOT [%d]:",index);  
  234.          imprimir(tab[index]);
  235.        }    
  236.            
  237.        system("pause");
  238.        return (0);
  239. }
  240.  
  241. /* ---------------------------------------------------------------------------*/
  242. /* ----FUNCAO: insertID-------------------------------------------------------*/  
  243. /* ---------------------------------------------------------------------------*/
  244. /* --- INSERE OS LEXEMAS QUE CORRESPONDEM AOS TOKEN DE IDENTIFICADORES ----*/        
  245. /* ---------------------------------------------------------------------------*/
  246.  
  247. void insertID(int slot,char* lexema){
  248.      tab[slot] = insere(tab[slot],yytext);
  249. }
  250.  
  251. /* ---------------------------------------------------------------------------*/
  252. /* ----FUNCAO: insere---------------------------------------------------------*/  
  253. /* ---------------------------------------------------------------------------*/
  254. /* --- INSERE OS LEXEMAS NO SLOT DA TABELA, SE O SLOT JA ESTIVER PREECHIDO ---*/
  255. /* --- ELE ALOCA NO PROXIMO NO DA LISTA --------------------------------------*/        
  256. /* ---------------------------------------------------------------------------*/
  257.  
  258. LISTA* insere(LISTA* a, char* lexema)
  259. {
  260.  LISTA *ant = NULL;   /*  aponta para lista anterior  */
  261.  LISTA *p = a;  
  262.  LISTA *colisao = NULL;
  263.              
  264.  if(a!=NULL){
  265.        
  266.        while (p != NULL){   /*   faz uma busca na tabela     */
  267.  
  268.              if(strcmp(p->lexema, lexema) == 0){
  269.  
  270.                  printf("\n[AVISO] Lexema [%s] ja existe na tabela de simbolos.",lexema);  
  271.                  return a;
  272.  
  273.              }
  274.  
  275.              if(p->prox == NULL){ /*cheguei no final da lista*/
  276.                  
  277.                  printf("\n[AVISO] Inserindo o Lexema [%s] no SLOT nao vazio da tabela de simbolos",lexema);  
  278.                  colisao = ( LISTA* )malloc(sizeof(LISTA));                              
  279.                  colisao->prox = NULL;
  280.                  strcpy(colisao->lexema, lexema);  
  281.                  p->prox = colisao;          
  282.                  return a;              
  283.              }
  284.      
  285.               ant = p;
  286.               p = p->prox;      
  287.        }
  288.  }
  289.  
  290.  if (a == NULL){   /* chave não encontrada ---> CRIANDO o NÓ -----------------*/
  291.  
  292.        printf("\n[AVISO] Inserindo o Lexema [%s] no SLOT vazio da tabela de simbolos",lexema);  
  293.        colisao = ( LISTA* )malloc(sizeof(LISTA));                    
  294.        colisao->prox = NULL;
  295.        strcpy(colisao->lexema, lexema);  
  296.  
  297.        return colisao;                  
  298.     }    
  299. }  
  300.  
  301. /* ----------------------------------------------------------------------------------------------------------*/
  302. /* ----FUNCAO: imprimir--------------------------------------------------------------------------------------*/
  303. /* ----------------------------------------------------------------------------------------------------------*/
  304. /* --- DESCRICAO: FUNCAO QUE IMPRIME O CONTEUDO DA TABELA E OPERA DE MANEIRA RECURSIVA NA LISTA ENCADEADA ---*/
  305. /* ----------------------------------------------------------------------------------------------------------*/
  306.  
  307. void imprimir( LISTA *a ){
  308.      LISTA *p = a;      
  309.       if (p != NULL){            
  310.           printf("[ %s ] -> ",a->lexema);
  311.           imprimir(p = p->prox);
  312.       }    
  313. }
  314.  
  315. /* ----------------------------------------------------------------------------------------------------------*/
  316. /* ----FUNCAO: criar-----------------------------------------------------------------------------------------*/
  317. /* ----------------------------------------------------------------------------------------------------------*/
  318. /* --- DESCRICAO: FUNCAO QUE INICIALIZA COM O VALOR NULL AS POSICOES DA TABELA ------------------------------*/
  319. /* ----------------------------------------------------------------------------------------------------------*/
  320.  
  321. LISTA* criar(){
  322.         return NULL;  
  323. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement