aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2019-10-16 22:43:18 +0200
committerJulian T <julian@jtle.dk>2019-10-16 22:46:25 +0200
commit0a599caf862f459e179f3d3841f9764a608381bc (patch)
tree5c38eec875bc7145da1caed2276b7ceba0e474ec
parent05c1fcd85ab196df585fa2b680ff1aed4f6d2996 (diff)
Added simple hashing function like the one in lecture
-rw-r--r--sem1/osc/mm10/Makefile12
-rw-r--r--sem1/osc/mm10/opg4.l58
2 files changed, 67 insertions, 3 deletions
diff --git a/sem1/osc/mm10/Makefile b/sem1/osc/mm10/Makefile
index 3d4089e..99bd635 100644
--- a/sem1/osc/mm10/Makefile
+++ b/sem1/osc/mm10/Makefile
@@ -1,4 +1,4 @@
-opgaver=opg1 opg3
+opgaver=opg1 opg3 opg4
LEX=flex
CC=gcc
@@ -8,12 +8,18 @@ DEFAULT_TARGET=all
%: %.yy.c
$(CC) -o $@ $^ $(LINKFLAG)
+%.o: %.c
+ $(CC) -c -o $@ $^
+
%.yy.c: %.l
$(LEX) -o $@ $^
all: $(opgaver)
-phony: all clean
+phony: all clean run
+
+run: $(BIN)
+ ./$(BIN)
clean:
- rm $(opgaver)
+ rm -f $(opgaver)
diff --git a/sem1/osc/mm10/opg4.l b/sem1/osc/mm10/opg4.l
new file mode 100644
index 0000000..9d18634
--- /dev/null
+++ b/sem1/osc/mm10/opg4.l
@@ -0,0 +1,58 @@
+%{
+
+#include <stdio.h>
+#include <stdint.h>
+
+#define BEGIN_T 1
+#define END_T 2
+#define NUM_T 3
+#define VAR_T 4
+#define HASHSIZE 100
+
+%}
+
+VAR [a-zA-Z][a-zA-Z\_\-0-9]*
+TAL [0-9]+
+
+%%
+
+begin {printf("Found a BEGIN\n"); return BEGIN_T;}
+end {printf("Found a END\n"); return END_T;}
+{VAR} {printf("Found a variable %s\n", yytext); return VAR_T;}
+{TAL} {printf("Found a number %d\n", strtol(yytext, NULL, 10)); return NUM_T;}
+
+%%
+
+unsigned int hash(char *s) {
+ uint32_t hv = 0;
+ for( int i = 0; s[i] != '\0'; i++ ) {
+ // take MSB 6 bits of hv and xors with LSB of s[i]
+ uint32_t v = ( hv >> 26 ) ^ (s[i] & 0x3f);
+
+ // Push those back on hv
+ hv = (hv << 4) | v;
+ }
+ // Return appropriate size
+ return hv % HASHSIZE;
+}
+
+int main(void) {
+
+ char str[100];
+ while( fgets(str, 100, stdin) ) {
+ printf("%s: %d\n", str, hash(str));
+ }
+
+ return 0;
+
+ for(;;) {
+ int t = yylex();
+ printf("yylex: %d\n", t);
+ if( t == END_T ) {
+ break;
+ }
+ }
+
+ return 0;
+
+}