diff --git a/labs/03/analyzer.l b/labs/03/analyzer.l new file mode 100644 index 0000000..49a739f --- /dev/null +++ b/labs/03/analyzer.l @@ -0,0 +1,44 @@ +/* + STUDENT INFORMATION + + Rodrigo López Guerra + A01737437 + + Laboratorio 3. + + Lexical Analyzer Program +*/ + +/* DEFINICIONES */ + +%{ +#include "y.tab.h" +%} + +/* OPCIONES */ + +%option noyywrap + +/* REGLAS Y TOKENS */ + +%% + +[aA] | +[tT]he { return ARTICLE; } + +[bB]oy | +[gG]irl { return NOUN; } + +[fF]lower { return GNOUN; } + +[tT]ouches | +[lL]ikes | +[sS]ees { return VERB; } + +[wW]ith { return PREP; } + +\n { return EOL; } + +[ \t]+ { /* Ignora espacios en blanco */ } + +%% \ No newline at end of file diff --git a/labs/03/analyzer.y b/labs/03/analyzer.y new file mode 100644 index 0000000..8bbf480 --- /dev/null +++ b/labs/03/analyzer.y @@ -0,0 +1,100 @@ +/* + STUDENT INFORMATION + + Rodrigo López Guerra + A01737437 + + Laboratorio 3. + + YACC Syntax Interpreter +*/ + +/* DEFINICIONES */ + +%{ +#include + +extern FILE *yyin; + +void yyerror(const char *s); +int yylex(void); + +int set = 0; +%} + +/* IDENTIFICADORES */ + +%token ARTICLE NOUN GNOUN VERB PREP EOL +%start INPUT + +/* REGLAS */ + +%% + +/* Reglas de la Gramática Adaptada */ + +INPUT: /* Espacio en blanco */ + | EOL {/* Ignora espacios en blanco */ ;} + | INPUT SENTENCE {set = 0;} + | INPUT error EOL { yyerrok; printf("FAIL\n"); set=0; } + ; + +SENTENCE: NOUN_PHRASE VERB_PHRASE EOL { printf("PASS\n"); set=1; } + ; + +NOUN_PHRASE : CMPLX_NOUN + | CMPLX_NOUN PREP_PHRASE + ; + +PREP_PHRASE: PREP CMPLX_NOUN + | PREP CMPLX_GNOUN + ; + +VERB_PHRASE : VERB + | VERB CMPLX_GNOUN + | VERB NOUN_PHRASE + ; + +CMPLX_NOUN : ARTICLE NOUN + ; + +CMPLX_GNOUN: ARTICLE GNOUN + ; + +%% + +/* ERRORES GENERADOS */ + +void yyerror(const char *s) { +} + +/* PROGRAMA PRINCIPAL */ + +int main(int argc, char **argv) { + + FILE *fd; // Declaración de la variable fd + + if (argc == 2) { + fd = fopen(argv[1], "r"); + if (!fd){ + perror("Error: "); + return (-1); + } + yyin = fd; // Establecer yyin para leer del archivo + } + else { + printf("Usage: ./analyzer FILENAME\n"); + return 1; // Retornar con error si no se proporciona el nombre del archivo + } + + int result = yyparse(); + if (set != 1){ + printf("FAIL\n"); // Se pone este comparador ya que al finalizar todas las reglas, YACC es incapaz de devolver la regla para un error. + } + + if (fd != NULL) { + fclose(fd); // Cerrar el archivo si está abierto + } + + return 0; +} diff --git a/labs/03/test.txt b/labs/03/test.txt new file mode 100644 index 0000000..80f3545 --- /dev/null +++ b/labs/03/test.txt @@ -0,0 +1,5 @@ + +a boy sees +the boy sees a flower +a girl with a flower likes the boy +a flower sees a flower \ No newline at end of file diff --git a/labs/03/usage.txt b/labs/03/usage.txt new file mode 100644 index 0000000..e8d318c --- /dev/null +++ b/labs/03/usage.txt @@ -0,0 +1,15 @@ +1.- AT TERMINAL, SEARCH FOR THE LOCATION OF THIS FILES WITH THE "cd" COMMAND. + +2. RUN + lex analyzer.l + +3. RUN + yacc -d analyzer.y + +4. RUN + gcc -o analyzer y.tab.c lex.yy.c -ll + +5. RUN + ./analyzer test.txt + +NOTE: If you want to try the program with personalized sentences, you need to change the content of the file named "test.txt". \ No newline at end of file