aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2019-10-24 09:17:29 +0200
committerJulian T <julian@jtle.dk>2019-10-24 09:18:01 +0200
commit5bc38338c79ef313f639696ec85e424c5d9c0b0c (patch)
tree92e0fa72ed025ea2dc23aff399546a3eb19e5252
parentad2bc056ae684f8de57f2ff880e51440d07ee04a (diff)
Added regn2
-rw-r--r--sem1/osc/mm11/opgaver.md4
-rw-r--r--sem1/osc/mm11/regn2/Makefile30
-rw-r--r--sem1/osc/mm11/regn2/regn.l27
-rw-r--r--sem1/osc/mm11/regn2/regn.y46
4 files changed, 107 insertions, 0 deletions
diff --git a/sem1/osc/mm11/opgaver.md b/sem1/osc/mm11/opgaver.md
index 8fab5c4..276236a 100644
--- a/sem1/osc/mm11/opgaver.md
+++ b/sem1/osc/mm11/opgaver.md
@@ -26,3 +26,7 @@ Denne laver jeg måske senere.
Denne er løst i regn mappen.
Kør `make run` deri for at køre.
+
+# Opgave 4
+
+I regn2 mappen
diff --git a/sem1/osc/mm11/regn2/Makefile b/sem1/osc/mm11/regn2/Makefile
new file mode 100644
index 0000000..9640aaa
--- /dev/null
+++ b/sem1/osc/mm11/regn2/Makefile
@@ -0,0 +1,30 @@
+
+LEX=flex
+YACC=bison
+LIBS=-ly -lfl -lm
+CC=gcc
+
+PROG=regn
+TRASH=lex.yy.c $(PROG).tab.c $(PROG) $(PROG).tab.h $(PROG).output
+
+$(PROG): $(PROG).tab.o lex.yy.o
+ $(CC) -o $@ $^ $(LIBS)
+
+$(PROG).tab.c $(PROG).tab.h: $(PROG).y
+ $(YACC) -d -v $(PROG).y
+
+lex.yy.c: $(PROG).l
+ $(LEX) $(PROG).l
+
+%.o: %.c
+ $(CC) -c -o $@ $^
+
+PHONY: clean run
+
+run: $(PROG)
+ ./$(PROG)
+
+clean:
+ rm -f *.o
+ rm -f $(TRASH)
+
diff --git a/sem1/osc/mm11/regn2/regn.l b/sem1/osc/mm11/regn2/regn.l
new file mode 100644
index 0000000..9988ddd
--- /dev/null
+++ b/sem1/osc/mm11/regn2/regn.l
@@ -0,0 +1,27 @@
+%{
+#include <math.h>
+#include <string.h>
+#include "regn.tab.h"
+%}
+
+realtal ([0-9]+|([0-9]*\.[0-9]+))([eE][-+]?[0-9]+)?
+op_log log
+op_exp exp
+op_sqrt sqrt
+
+%%
+{realtal} {yylval.dval = atof(yytext);
+ return TAL;}
+{op_log} {return LOG;}
+{op_exp} {return EXP;}
+{op_sqrt} {return SQRT;}
+
+[ \t] ;
+
+
+'$' {return 0;}
+
+\n|. {return yytext[0];}
+
+%%
+
diff --git a/sem1/osc/mm11/regn2/regn.y b/sem1/osc/mm11/regn2/regn.y
new file mode 100644
index 0000000..a181cb3
--- /dev/null
+++ b/sem1/osc/mm11/regn2/regn.y
@@ -0,0 +1,46 @@
+%{
+#include <stdio.h>
+#include <math.h>
+#include <string.h>
+%}
+
+%union {
+ double dval;
+}
+
+%token <dval> TAL
+%token LOG EXP SQRT
+
+%left '-' '+'
+%left LOG EXP SQRT
+%left '*' '/'
+%right UMINUS
+
+%type <dval> expression
+
+%%
+
+statement_list: statement '\n'
+ | statement_list statement '\n' ;
+
+statement: expression {;};
+
+expression: expression '+' expression {printf("sum \n");}
+ | expression '-' expression {printf("sub \n");}
+ | expression '*' expression {printf("mul \n");}
+ | expression '/' expression {if ($3 == 0.0)
+ yyerror("divide dy zero");
+ else printf("div \n");}
+ | '-' expression %prec UMINUS {printf("neg \n");}
+ | '(' expression ')' {;}
+ | LOG expression {printf("log \n");}
+ | EXP expression {printf("exp \n");}
+ | SQRT expression {printf("sqrt \n");}
+ | TAL {printf("load %f \n", $$);};
+%%
+
+int main()
+{
+ yyparse();
+}
+