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
3 changes: 3 additions & 0 deletions labs/03/COMMNDSLEXYACC.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
yacc -d analizer.y
lex analizer.l
gcc y.tab.c lex.yy.c -ly -ll -o analizer
52 changes: 52 additions & 0 deletions labs/03/analizer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
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 { return PREP; }
[ \t] ;
\n { return 0; };
%%

int main(int argc, char **argv) {
FILE *fd;

if (argc == 2)
{
if (!(fd = fopen(argv[1], "r")))
{
perror("Error: ");
return (-1);
}

yyin = fd;


char ch;
int lines = 0;
// Counting the total number of lines in the file
while ((ch = fgetc(fd)) != EOF) {
if (ch == '\n') {
lines++;
}
}

fseek(fd, 0, SEEK_SET);
for(int i=0;i<lines;i++)
yyparse();


fclose(fd);


}
else
printf("Usage: a.out filename\n");
return (0);
}



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

%union {
char *sval;
}

%token <sval> ARTICLE NOUN VERB PREP
%type <sval> nounphrase verbphrase prepphrase complexnoun complexverb sentence


%%

input: sentence {printf("PASS\n");}
;
sentence: nounphrase verbphrase
;
nounphrase: complexnoun
| complexnoun prepphrase
;
verbphrase: complexverb
;
prepphrase: PREP complexnoun
;
complexnoun: ARTICLE NOUN
;
complexverb: VERB
| VERB nounphrase
;

%%


Binary file added labs/03/analyzer
Binary file not shown.
6 changes: 6 additions & 0 deletions labs/03/test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
a boy sees
the boy sees a flower
a girl with a flower likes the boy
a flower sees a flower
dada
the boy