diff --git a/labs/03/COMMNDSLEXYACC.txt b/labs/03/COMMNDSLEXYACC.txt new file mode 100644 index 0000000..de4c54d --- /dev/null +++ b/labs/03/COMMNDSLEXYACC.txt @@ -0,0 +1,3 @@ +yacc -d analizer.y +lex analizer.l +gcc y.tab.c lex.yy.c -ly -ll -o analizer diff --git a/labs/03/analizer.l b/labs/03/analizer.l new file mode 100644 index 0000000..9bafab1 --- /dev/null +++ b/labs/03/analizer.l @@ -0,0 +1,52 @@ +%{ +#include +#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 +int yylex(); +void yyerror(const char *s); +%} + +%union { + char *sval; + } + +%token ARTICLE NOUN VERB PREP +%type 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 + ; + +%% + + diff --git a/labs/03/analyzer b/labs/03/analyzer new file mode 100644 index 0000000..eda345f Binary files /dev/null and b/labs/03/analyzer differ diff --git a/labs/03/test.txt b/labs/03/test.txt new file mode 100644 index 0000000..204fb9f --- /dev/null +++ b/labs/03/test.txt @@ -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