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
25 changes: 25 additions & 0 deletions labs/03/Practica3.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
%{
#include "y.tab.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
%}

%{
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'; }


%%
63 changes: 63 additions & 0 deletions labs/03/Practica3.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
extern int line_number;
extern FILE *yyin;
extern int yylex();
void yyerror(const char *s);
%}

%union {
char *sval;
}

%token <sval> 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;
}