diff --git a/labs/03/Practica3.l b/labs/03/Practica3.l new file mode 100644 index 0000000..5cefa61 --- /dev/null +++ b/labs/03/Practica3.l @@ -0,0 +1,25 @@ +%{ +#include "y.tab.h" +#include +#include +#include +%} + +%{ +int line_number = 1; +%} + +%% + +a|the {yylval.sval = strdup(yytext); return ARTICLE;} +boy|girl|flower {yylval.sval = strdup(yytext); return NOUN;} +touches|likes|sees {yylval.sval = strdup(yytext); return VERB;} +with {yylval.sval = strdup(yytext); return PREP;} + +[ \t]+ /* ignore whitespace */ +^[ \t]*\n /* ignore blank lines */ + +\n { line_number++; return '\n'; } + + +%% diff --git a/labs/03/Practica3.y b/labs/03/Practica3.y new file mode 100644 index 0000000..b95aef8 --- /dev/null +++ b/labs/03/Practica3.y @@ -0,0 +1,63 @@ +%{ +#include +#include +#include +extern int line_number; +extern FILE *yyin; +extern int yylex(); +void yyerror(const char *s); +%} + +%union { + char *sval; +} + +%token ARTICLE NOUN VERB PREP + +%% + +start: sentences '\n' { printf("PASS\n"); } + + +sentences: /* empty */ + | sentences sentence '\n' + +sentence: NOUN_PHRASE VERB_PHRASE { printf("PASS\n"); } + | NOUN_PHRASE { printf("PASS\n"); } + | VERB_PHRASE { printf("PASS\n"); } + +NOUN_PHRASE: ARTICLE NOUN + | ARTICLE NOUN PREP_PHRASE +; + +VERB_PHRASE: VERB + | VERB NOUN_PHRASE +; + +PREP_PHRASE: PREP NOUN_PHRASE +; + + +%% + +void yyerror(const char *s) { + fprintf(stderr, "FAIL... FIN DE ARCHIVO\n"); +} + +int main(int argc, char **argv) { + if (argc == 2) { + if (!(yyin = fopen(argv[1], "r"))) { + perror("Error al abrir el archivo: "); + return 1; + } + + yyparse(); + fclose(yyin); + + } else { + fprintf(stderr, "Usage: %s filename\n", argv[0]); + return 1; + } + + return 0; +}