Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add files via upload #152

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
69 changes: 69 additions & 0 deletions grammar.y
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//A01781041
%{
#include <stdio.h>
#include <stdlib.h>

// Declare yyin and yylineno if they're not already defined by Flex
extern FILE *yyin;
extern int yylineno;

void yyerror(const char *s) {
fprintf(stderr, "Error: %s at line %d\n", s, yylineno);
exit(EXIT_FAILURE);
}

int yylex(void);
%}

%token ARTICLE BOY GIRL FLOWER TOUCHES LIKES SEES WITH

%%

SENTENCE: NOUN_PHRASE VERB_PHRASE
;

NOUN_PHRASE: ARTICLE NOUN
| ARTICLE NOUN PREP_PHRASE
;

NOUN: BOY
| GIRL
| FLOWER
;

VERB_PHRASE: VERB
| VERB NOUN_PHRASE
;

VERB: TOUCHES
| LIKES
| SEES
;

PREP_PHRASE: WITH NOUN_PHRASE
;

%%

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

FILE *file = fopen(argv[1], "r");
if (!file) {
perror(argv[1]);
exit(EXIT_FAILURE);
}

yyin = file;

// Parse through the input until there is no more:
do {
yyparse();
} while (!feof(yyin));

fclose(file);
return 0;
}
24 changes: 24 additions & 0 deletions tokens.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%{
#include "grammar.tab.h"
%}

%option yylineno

%%

[aA] { return ARTICLE; }
boy { return BOY; }
girl { return GIRL; }
flower { return FLOWER; }
touches { return TOUCHES; }
likes { return LIKES; }
sees { return SEES; }
with { return WITH; }
[ \t\n]+ ; /* Ignore whitespaces and newlines */
. ; /* Ignore other characters */

%%

int yywrap(void) {
return 1;
}