uni

University stuff
git clone git://git.margiolis.net/uni.git
Log | Files | Refs | README | LICENSE

commit 1d7fb22b9ff8207d7303b430311e7ba8d3963d57
parent 80254b05d7584404abf39f39e74cb0eefc86944b
Author: Christos Margiolis <christos@margiolis.net>
Date:   Sat, 21 May 2022 17:36:02 +0300

lex part3 almost done

Diffstat:
Rlex_bison_compilers/part1/02_B1_PART_A2.pdf -> lex_yacc_compilers/part1/02_B1_PART_A2.pdf | 0
Rlex_bison_compilers/part1/cmt.fsm -> lex_yacc_compilers/part1/cmt.fsm | 0
Rlex_bison_compilers/part1/def.fsm -> lex_yacc_compilers/part1/def.fsm | 0
Rlex_bison_compilers/part1/float.fsm -> lex_yacc_compilers/part1/float.fsm | 0
Rlex_bison_compilers/part1/fsm -> lex_yacc_compilers/part1/fsm | 0
Rlex_bison_compilers/part1/fsm.c -> lex_yacc_compilers/part1/fsm.c | 0
Rlex_bison_compilers/part1/int.fsm -> lex_yacc_compilers/part1/int.fsm | 0
Rlex_bison_compilers/part1/lex.fsm -> lex_yacc_compilers/part1/lex.fsm | 0
Rlex_bison_compilers/part1/sep.fsm -> lex_yacc_compilers/part1/sep.fsm | 0
Rlex_bison_compilers/part1/str.fsm -> lex_yacc_compilers/part1/str.fsm | 0
Rlex_bison_compilers/part1/var.fsm -> lex_yacc_compilers/part1/var.fsm | 0
Rlex_bison_compilers/part2/02_B1_PART_A3.pdf -> lex_yacc_compilers/part2/02_B1_PART_A3.pdf | 0
Rlex_bison_compilers/part2/Makefile -> lex_yacc_compilers/part2/Makefile | 0
Rlex_bison_compilers/part2/input.txt -> lex_yacc_compilers/part2/input.txt | 0
Rlex_bison_compilers/part2/lex.l -> lex_yacc_compilers/part2/lex.l | 0
Rlex_bison_compilers/part2/output.txt -> lex_yacc_compilers/part2/output.txt | 0
Rlex_bison_compilers/part2/token.h -> lex_yacc_compilers/part2/token.h | 0
Alex_yacc_compilers/part3/Makefile | 8++++++++
Rlex_bison_compilers/part2/input.txt -> lex_yacc_compilers/part3/input.txt | 0
Alex_yacc_compilers/part3/lex.l | 38++++++++++++++++++++++++++++++++++++++
Alex_yacc_compilers/part3/syntax.y | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
21 files changed, 163 insertions(+), 0 deletions(-)

diff --git a/lex_bison_compilers/part1/02_B1_PART_A2.pdf b/lex_yacc_compilers/part1/02_B1_PART_A2.pdf Binary files differ. diff --git a/lex_bison_compilers/part1/cmt.fsm b/lex_yacc_compilers/part1/cmt.fsm diff --git a/lex_bison_compilers/part1/def.fsm b/lex_yacc_compilers/part1/def.fsm diff --git a/lex_bison_compilers/part1/float.fsm b/lex_yacc_compilers/part1/float.fsm diff --git a/lex_bison_compilers/part1/fsm b/lex_yacc_compilers/part1/fsm Binary files differ. diff --git a/lex_bison_compilers/part1/fsm.c b/lex_yacc_compilers/part1/fsm.c diff --git a/lex_bison_compilers/part1/int.fsm b/lex_yacc_compilers/part1/int.fsm diff --git a/lex_bison_compilers/part1/lex.fsm b/lex_yacc_compilers/part1/lex.fsm diff --git a/lex_bison_compilers/part1/sep.fsm b/lex_yacc_compilers/part1/sep.fsm diff --git a/lex_bison_compilers/part1/str.fsm b/lex_yacc_compilers/part1/str.fsm diff --git a/lex_bison_compilers/part1/var.fsm b/lex_yacc_compilers/part1/var.fsm diff --git a/lex_bison_compilers/part2/02_B1_PART_A3.pdf b/lex_yacc_compilers/part2/02_B1_PART_A3.pdf Binary files differ. diff --git a/lex_bison_compilers/part2/Makefile b/lex_yacc_compilers/part2/Makefile diff --git a/lex_bison_compilers/part2/input.txt b/lex_yacc_compilers/part2/input.txt diff --git a/lex_bison_compilers/part2/lex.l b/lex_yacc_compilers/part2/lex.l diff --git a/lex_bison_compilers/part2/output.txt b/lex_yacc_compilers/part2/output.txt diff --git a/lex_bison_compilers/part2/token.h b/lex_yacc_compilers/part2/token.h diff --git a/lex_yacc_compilers/part3/Makefile b/lex_yacc_compilers/part3/Makefile @@ -0,0 +1,8 @@ +all: + bison -d syntax.y + flex lex.l + cc syntax.tab.c lex.yy.c -lm -o uniclips + ./uniclips input.txt + +clean: + rm -f *.yy.c *.output *.tab.c *.tab.h uniclips *.core diff --git a/lex_bison_compilers/part2/input.txt b/lex_yacc_compilers/part3/input.txt diff --git a/lex_yacc_compilers/part3/lex.l b/lex_yacc_compilers/part3/lex.l @@ -0,0 +1,38 @@ +%option noyywrap +%{ +#include <stdlib.h> +#include "syntax.tab.h" +%} + +/* + * Με βάση το μέρος Α2, υλοποιούμε τις κανονικές εκφράσεις και τις + * αντιστοιχούμε στα κατάλληλα tokens. + */ +FUNC deffacts|defrule|test|bind|read|printout|quit|"+"|"-"|"*"|"/"|"="|"e" +DELIM [ \t]+ +INT 0|[+-]?[1-9]+[0-9]* +FLOAT [+-]?[0-9]+((\.[0-9]+)([eE][+-]?[0-9]*)?|([eE][+-]?[0-9]*)?) +STR \"[^\"\\]*(?:\\.[^\"\\]*)*\" +DEFIN [A-Za-z]+[A-Za-z0-9_-]* +VAR \?[A-Za-z0-9]+ +COMMENT ;.* + +/* + * Όταν βρίσκει οποιοδήποτε token, επιστρέφει την αντίστοιχη κατηγορία στο + * οποίο ανήκει + */ +%% +{FUNC} { yylval.sval = strdup(yytext); return FUNC; } +{INT} { yylval.dval = strtod(yytext, NULL); return INT; } +{FLOAT} { yylval.dval = strtod(yytext, NULL); return FLOAT; } +{STR} { yylval.sval = strdup(yytext); return STR; } +{DEFIN} { yylval.sval = strdup(yytext); return DEFIN; } +{VAR} { yylval.sval = strdup(yytext); return VAR; } +"(" { return LPAR; } +")" { return RPAR; } +"->" { return ARROW; } +\n { return NEWLINE; } +{DELIM} { /* ignore whitespace */ } +{COMMENT} { /* skip comments */ } +. { return UNKNOWN; } +%% diff --git a/lex_yacc_compilers/part3/syntax.y b/lex_yacc_compilers/part3/syntax.y @@ -0,0 +1,117 @@ +%{ +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <math.h> + +extern int yylex(void); +extern int yyparse(void); +extern FILE *yyin, *yyout; +extern char *yytext; + +double func(char *); +double calc(char *, double, double); +void yyerror(const char *); +%} + +%union { + double dval; + char *sval; +} + +%token <sval> FUNC STR DEFIN VAR +%token <dval> INT FLOAT +%token NEWLINE UNKNOWN LPAR RPAR ARROW + +%type <dval> expr + +%start prog + +%% +prog: + { printf("> "); } + | prog expr NEWLINE { printf("%f\n> ", $2); } + ; + +expr: + INT { $$ = $1; } + | FLOAT { $$ = $1; } + | LPAR FUNC RPAR { $$ = func($2); } + | LPAR FUNC expr expr RPAR { $$ = calc($2, $3, $4); } + | error { yyerror("error"); } + ; +%% + +double +func(char *op) +{ + double n; + int rc; + + if (strcmp(op, "quit") == 0) { + exit(0); + } else if (strcmp(op, "read") == 0) { + do { + printf("read> "); + rc = scanf("%lf", &n); + (void)getchar(); + } while (rc != 1); + return (n); + } + + yyerror("invalid expression"); + /* NOT REACHED */ + return (0); +} + +double +calc(char *op, double arg1, double arg2) +{ + if (strcmp(op, "+") == 0) + return (arg1 + arg2); + else if (strcmp(op, "-") == 0) + return (arg1 - arg2); + else if (strcmp(op, "*") == 0) + return (arg1 * arg2); + else if (strcmp(op, "/") == 0) { + if (arg2 == 0) + yyerror("cannot divide by 0"); + return (arg1 / arg2); + } + else if (strcmp(op, "e") == 0) + return (pow(arg1, arg2)); + else if (strcmp(op, "=") == 0) + return (arg1 == arg2); + + yyerror("invalid expression"); + /* NOT REACHED */ + return (0); +} + +void +yyerror(const char *s) +{ + fprintf(stderr, "%s\n", s); + exit(1); +} + +int +main(int argc, char *argv[]) +{ + /*if (argc < 2) {*/ + /*fprintf(stderr, "usage: %s input [output]\n", *argv);*/ + /*return (-1);*/ + /*}*/ + /*if ((yyin = fopen(argv[1], "r")) == NULL)*/ + /*err(1, "fopen(%s)", argv[1]);*/ + /*if (argc == 3 && (yyout = fopen(argv[2], "w")) == NULL)*/ + /*err(1, "fopen(%s)", argv[2]);*/ + + if (yyparse() == 0) + fprintf(stderr, "success\n"); + else + fprintf(stderr, "failure\n"); + + return (0); +}