aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2019-10-17 08:09:46 +0200
committerJulian T <julian@jtle.dk>2019-10-17 08:09:46 +0200
commit0b42016ade418b1a78db27143d028f6f27741798 (patch)
treefcf8ea7e085e8e273cd6029510ce045b40ef0125
parent0a599caf862f459e179f3d3841f9764a608381bc (diff)
Færdiggjorde osc
-rw-r--r--sem1/osc/mm10/opg4.l55
-rw-r--r--sem1/osc/mm10/opgaver.md9
2 files changed, 56 insertions, 8 deletions
diff --git a/sem1/osc/mm10/opg4.l b/sem1/osc/mm10/opg4.l
index 9d18634..aeb73b1 100644
--- a/sem1/osc/mm10/opg4.l
+++ b/sem1/osc/mm10/opg4.l
@@ -9,6 +9,14 @@
#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]*
@@ -18,11 +26,18 @@ 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;}
+{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++ ) {
@@ -36,14 +51,42 @@ unsigned int hash(char *s) {
return hv % HASHSIZE;
}
-int main(void) {
- char str[100];
- while( fgets(str, 100, stdin) ) {
- printf("%s: %d\n", str, hash(str));
+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;
}
- return 0;
+ // 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();
diff --git a/sem1/osc/mm10/opgaver.md b/sem1/osc/mm10/opgaver.md
index a3889ef..b8c8186 100644
--- a/sem1/osc/mm10/opgaver.md
+++ b/sem1/osc/mm10/opgaver.md
@@ -12,8 +12,7 @@ Kan ses i den tilhørende opg1.l fil.
For at compile kør:
```
-make opg1
-./opg1
+make run BIN=opg1
```
C)
@@ -45,6 +44,12 @@ starter med store: [A-Z][a-zA-z]+
Dette laver jeg i opg3.l filen.
+## Opgave 4
+Se opg4.l fil og kør med.
+
+```
+make run BIN=opg4
+```