DBF.YAC



/* PERMITIR EL LENGUAJE DE SQL PARA ARCHIVOS DBF */
/* ---------------------------------------------------------
   --- Programa:    dbf.yac                              ---
   --- TEcnica:     Dirigida por la sintAxis             ---
   --- Objetivo:    Permitir el lenguaje de SQL para DBF ---
   --- Programador: J. Rafael R. ochoa                   ---
   ---------------------------------------------------------*/
%{
   #include "dbf.h"   
%}

%union
{
   char str[80];
}

%token <str> COMA LEFT RIGHT IGUAL LINEA SALIR GO INSERT INTO
%token <str> VALUES DELETE FROM WHERE SELECT UPDATE CREATE TABLE DROP
%token <str> APOSTROFE NOT_NULL SET AND OR CHAR INT ASTERISCO
%token <str> NUMERO BDD TEXTO
%type  <str> insert delete select update tables datos dato criterio
%type  <str> view list_view estructura not_null tipo cambios crit_list

%%
/* producciones de la gramatica */
entrada   : expr
          | expr entrada
          | error LINEA { printf("\n:-) "); } entrada
          ;
expr      : SALIR     LINEA   { return 1; }
          | insert GO LINEA   { printf("\n:-) "); } /* ok */
          | delete GO LINEA   { printf("\n:-) "); } /* ok */
          | select GO LINEA   { printf("\n:-) "); } /* ok */
          | update GO LINEA   { printf("\n:-) "); } /* ok */
          | tables GO LINEA   { printf("\n:-) "); } /* ok */
          ;
insert    : INSERT INTO BDD VALUES LEFT datos RIGHT
            {
               insertar($3);
               datos_insert=NULL;
            }
          ;
delete    : DELETE FROM BDD criterio
            {
               delete($3);
               filtros = NULL;
            }
          ;
select    : SELECT view FROM BDD criterio
            {
               consulta($4);
               filtros = NULL;
               field_req = NULL;
            }
          ;
update    : UPDATE BDD SET cambios criterio
            {
               update($2);
               filtros = NULL;
               datos_insert=NULL;
               cambios = NULL;
            }
          ;
tables    : CREATE TABLE BDD LEFT estructura RIGHT
            {
               sprintf($$,"create table %s (%s)\\g",$3,$5);
            }
          | DROP   TABLE BDD
            {
               sprintf($$,"drop table %s\\g",$3);
            }
          ;
datos     : dato
              { read_insert($1); }
          | dato COMA datos
              { read_insert($1); }
          ;
dato      : APOSTROFE TEXTO APOSTROFE
              { sprintf($$,"%s",$2); }
          | NUMERO
              { sprintf($$,"%s",$1); }
          ;
criterio  : {sprintf($$,"%s","rafa"); filtros = NULL;} /* todos */
          | WHERE crit_list
          ;
crit_list : TEXTO IGUAL dato
               { read_filtros($1,$3); }
          | TEXTO IGUAL dato AND crit_list
               { read_filtros($1,$3); }
          ;
cambios   :  TEXTO IGUAL dato
              { read_cambios($1,$3); }
          | TEXTO IGUAL dato COMA cambios
              { read_cambios($1,$3); }
view      : ASTERISCO { read_campos("*"); }
          | list_view
          ;
list_view : TEXTO {read_campos($1);}
          | TEXTO COMA list_view {read_campos($1);}
          ;
estructura: TEXTO tipo LEFT NUMERO RIGHT not_null
              { sprintf($$,"%s %s (%s)%s",$1,$2,$4,$6); }
          | TEXTO tipo LEFT NUMERO RIGHT not_null
            COMA estructura
              { sprintf($$,"%s %s (%s)%s, %s",$1,$2,$4,$6,$8); }
          ;
tipo      : CHAR    { strcpy($$,"char"); }
          | INT     { strcpy($$, "int"); }
          ;
not_null  :   { strcpy($$, " "); }
          | NOT_NULL
              { strcpy($$, " not null"); }
          ;
%%

/* funciones auxiliares */
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include </usr/include/sys/stat.h>
#include </usr/include/sys/types.h>

#include "utiles.c"
#include "select.c"
#include "insert.c"
#include "delete.c"
#include "update.c"

extern int yylex();  /* Liga con la funcion yylex generada por LEX */
extern int yynerrs;

int yyparse();


void main(int argc, char *argv[])
{
   system("clear"); printf("\n"); 
   filtros = NULL;
   field_req = NULL;  campos = NULL,

   read_struct(argv[1]);
   printf("-------------------------------------------\n");
   printf("   Monitor SQL - DBF\n");
   printf("-------------------------------------------\n");
   printf(":-) ");
   yyparse();
   printf("\n:-)~ok\n");
}

yyerror()           /* llamado por yyparse() cuando ocurra */
{                             /*   un error                          */
  printf("\n error de sintaxis \n");
  return 1;
}