aboutsummaryrefslogtreecommitdiff
path: root/sem3/osc/mm10
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2020-02-11 12:24:56 +0100
committerJulian T <julian@jtle.dk>2020-02-11 12:24:56 +0100
commit6db1a2cdd3b96731f2e092d55d8c2136eabc52d0 (patch)
tree2be8fae8ce82d708ed9f00f376dda14420850e80 /sem3/osc/mm10
parent57305119e05559c1c37e903aef89cd43f44c42c9 (diff)
Rename and cleanup
Diffstat (limited to 'sem3/osc/mm10')
-rw-r--r--sem3/osc/mm10/Makefile25
-rw-r--r--sem3/osc/mm10/opg1.l22
-rw-r--r--sem3/osc/mm10/opg3.l36
-rw-r--r--sem3/osc/mm10/opg4.l101
-rw-r--r--sem3/osc/mm10/opgaver.md55
-rw-r--r--sem3/osc/mm10/stateMachine.pngbin0 -> 21881 bytes
6 files changed, 239 insertions, 0 deletions
diff --git a/sem3/osc/mm10/Makefile b/sem3/osc/mm10/Makefile
new file mode 100644
index 0000000..99bd635
--- /dev/null
+++ b/sem3/osc/mm10/Makefile
@@ -0,0 +1,25 @@
+opgaver=opg1 opg3 opg4
+
+LEX=flex
+CC=gcc
+LINKFLAG=-lfl
+DEFAULT_TARGET=all
+
+%: %.yy.c
+ $(CC) -o $@ $^ $(LINKFLAG)
+
+%.o: %.c
+ $(CC) -c -o $@ $^
+
+%.yy.c: %.l
+ $(LEX) -o $@ $^
+
+all: $(opgaver)
+
+phony: all clean run
+
+run: $(BIN)
+ ./$(BIN)
+
+clean:
+ rm -f $(opgaver)
diff --git a/sem3/osc/mm10/opg1.l b/sem3/osc/mm10/opg1.l
new file mode 100644
index 0000000..b5cb478
--- /dev/null
+++ b/sem3/osc/mm10/opg1.l
@@ -0,0 +1,22 @@
+%{
+
+#include <stdio.h>
+
+%}
+
+ting (a|b)abcd
+
+%%
+
+{ting} { printf("Fandt ting %s\n", yytext); return 1; }
+. { printf("Meh"); }
+
+%%
+
+int main(void) {
+
+ printf("yylex: %d\n", yylex());
+
+ return 0;
+
+}
diff --git a/sem3/osc/mm10/opg3.l b/sem3/osc/mm10/opg3.l
new file mode 100644
index 0000000..1c4b9c7
--- /dev/null
+++ b/sem3/osc/mm10/opg3.l
@@ -0,0 +1,36 @@
+%{
+
+#include <stdio.h>
+
+#define BEGIN_T 1
+#define END_T 2
+#define NUM_T 3
+#define VAR_T 4
+
+%}
+
+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;}
+
+%%
+
+int main(void) {
+
+ for(;;) {
+ int t = yylex();
+ printf("yylex: %d\n", t);
+ if( t == END_T ) {
+ break;
+ }
+ }
+
+ return 0;
+
+}
diff --git a/sem3/osc/mm10/opg4.l b/sem3/osc/mm10/opg4.l
new file mode 100644
index 0000000..aeb73b1
--- /dev/null
+++ b/sem3/osc/mm10/opg4.l
@@ -0,0 +1,101 @@
+%{
+
+#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
+
+typedef struct symnode_struct {
+ struct symnode_struct *next;
+ char *name;
+} symnode_t;
+
+symnode_t *sym_insert(char *var);
+symnode_t *sym_lookup(char *var);
+
+%}
+
+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);
+ if( !sym_lookup(yytext) ) {
+ printf("Not found inserting\n");
+ sym_insert(yytext);
+ }
+ return VAR_T;}
+{TAL} {printf("Found a number %d\n", strtol(yytext, NULL, 10)); return NUM_T;}
+
+%%
+
+symnode_t *symlist[HASHSIZE];
+
+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;
+}
+
+
+symnode_t *sym_insert(char *var) {
+ unsigned int index = hash(var);
+
+ // Save old value
+ symnode_t *oldSym = symlist[index];
+
+ // Make new
+ symlist[index] = malloc(sizeof(symnode_t));
+ if( symlist[index] == NULL ) {
+ // If malloc failed
+ symlist[index] = oldSym;
+ return NULL;
+ }
+
+ // Link old one
+ symlist[index]->next = oldSym;
+ symlist[index]->name = var;
+
+ return symlist[index];
+}
+
+symnode_t *sym_lookup(char *var) {
+ unsigned int index = hash(var);
+ symnode_t *n = symlist[index];
+
+ // Look trough list
+ while(n != NULL && strcmp(n->name, var) != 0) {
+ n = n->next;
+ }
+
+ return n;
+}
+
+
+int main(void) {
+
+ for(;;) {
+ int t = yylex();
+ printf("yylex: %d\n", t);
+ if( t == END_T ) {
+ break;
+ }
+ }
+
+ return 0;
+
+}
diff --git a/sem3/osc/mm10/opgaver.md b/sem3/osc/mm10/opgaver.md
new file mode 100644
index 0000000..b8c8186
--- /dev/null
+++ b/sem3/osc/mm10/opgaver.md
@@ -0,0 +1,55 @@
+# MM10
+
+## Opgave 1
+
+A)
+
+<img src="./stateMachine.png"></img>
+
+B)
+
+Kan ses i den tilhørende opg1.l fil.
+For at compile kør:
+
+```
+make run BIN=opg1
+```
+
+C)
+
+```
+aabcd
+
+Fandt ting aabcd
+yylex: 1
+```
+
+```
+afggfd
+
+MehMehMehMehMehMeh
+```
+
+## Opgave 2
+
+Regulære udtryk for:
+
+```
+c kommentarer: \/\/.*\n
+real konstant: [-+]?[0-9]*(\.[0-9]+)?(e\+[0-9]+)?
+starter med store: [A-Z][a-zA-z]+
+```
+
+## Opgave 3
+
+Dette laver jeg i opg3.l filen.
+
+## Opgave 4
+
+Se opg4.l fil og kør med.
+
+```
+make run BIN=opg4
+```
+
+
diff --git a/sem3/osc/mm10/stateMachine.png b/sem3/osc/mm10/stateMachine.png
new file mode 100644
index 0000000..08c5bf2
--- /dev/null
+++ b/sem3/osc/mm10/stateMachine.png
Binary files differ