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
Binary file added labs/03/lab03
Binary file not shown.
20 changes: 20 additions & 0 deletions labs/03/lab03.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
%{
#include "y.tab.h"
%}

%%

"a"|"the" { return ARTICLE; }
"boy"|"girl"|"flower" { return NOUN; }
"touches"|"likes"|"sees" { return VERB; }
"with" { return PREPOSITION; }
[\t]+ ; /* ignore whitespace */
\n { return EOL; }
. ; /* ignore other characters */

%%

int yywrap(void) {
return 1;
}

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

%token ARTICLE NOUN VERB PREPOSITION EOL

%%

sentences
: /* empty */
| sentences sentence EOL { printf("PASS\n"); }
| sentences error EOL { printf("FAIL\n"); yyerrok; yyclearin; }
;

sentence
: noun_phrase verb_phrase
;

noun_phrase
: ARTICLE NOUN
| ARTICLE NOUN prep_phrase
;

verb_phrase
: VERB
| VERB noun_phrase
| VERB prep_phrase
;

prep_phrase
: PREPOSITION noun_phrase
;

%%

int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr, "Usage: %s filename\n", argv[0]);
exit(1);
}

yyin = fopen(argv[1], "r");
if (!yyin) {
perror(argv[1]);
exit(1);
}

do {
yyparse();
} while (!feof(yyin));

fclose(yyin);
return 0;
}

void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}

Loading