Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions labs/03/analyzer.l
Original file line number Diff line number Diff line change
@@ -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 */ }

%%
100 changes: 100 additions & 0 deletions labs/03/analyzer.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
STUDENT INFORMATION

Rodrigo López Guerra
A01737437

Laboratorio 3.

YACC Syntax Interpreter
*/

/* DEFINICIONES */

%{
#include <stdio.h>

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;
}
5 changes: 5 additions & 0 deletions labs/03/test.txt
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions labs/03/usage.txt
Original file line number Diff line number Diff line change
@@ -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".