From 94f12e10063b84f4d0d54eb4217eadeaf1198ad4 Mon Sep 17 00:00:00 2001 From: Augusto Gunsch Date: Thu, 29 Oct 2020 21:22:03 -0300 Subject: [PATCH] Add parser --- .gitignore | 1 + Makefile | 7 ++++ main.c | 33 ++++++++++++++++ parser.c | 112 +++++++++++++++++++++++++++++++++++++++++++++++++++++ parser.h | 11 ++++++ 5 files changed, 164 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c create mode 100644 parser.c create mode 100644 parser.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4c61528 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +vmtranslator diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..39b7d39 --- /dev/null +++ b/Makefile @@ -0,0 +1,7 @@ +FILES = parser.c main.c +INCLUDES = -I. +CFLAGS = -std=c99 -g +OUTFILE = vmtranslator + +main: ${FILES} + ${CC} ${CFLAGS} ${INCLUDES} -o ${OUTFILE} ${FILES} diff --git a/main.c b/main.c new file mode 100644 index 0000000..9ad23e6 --- /dev/null +++ b/main.c @@ -0,0 +1,33 @@ +#include +#include +#include +#include "parser.h" + +int main(int argc, char* argv[]) { + if(argc < 2) { + fprintf(stderr, "Usage: %s {file}\n", argv[0]); + return 1; + } + + FILE* input = fopen(argv[1], "r"); + + if(input == NULL) { + fprintf(stderr, "%s\n", strerror(errno)); + return errno; + } + + int lncount, widestln, maxtokens; + getinfo(input, &lncount, &widestln, &maxtokens); + struct line** lns = parse(input, lncount, widestln, maxtokens); + + printf("lns: %i\n", lncount); + for(int i = 0; i < lncount; i++) { + int tkcount = lns[i]->tokenscount; + for(int j = 0; j < tkcount; j++) { + printf("%s ", lns[i]->tokens[j]); + } + printf("\n"); + } + + return 0; +} diff --git a/parser.c b/parser.c new file mode 100644 index 0000000..8924163 --- /dev/null +++ b/parser.c @@ -0,0 +1,112 @@ +#include +#include +#include +#include +#include "parser.h" + +void gountilbrk (FILE* input) { + char c; + while(c = fgetc(input), c != -1) { + if(c == '\n') { + ungetc(c, input); + return; + } + } +} + +void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens) { + char c, nc; + int lns = 0; + int widest = 0; + int currsz = 0; + int tokens = 0; + int maxtoks = 0; + short readsmt = 0; + while(c = fgetc(input), c != -1) { + if(isspace(c) && readsmt) { + tokens++; + if(c == '\n') { + lns++; + if(currsz > widest) + widest = currsz; + if(tokens > maxtoks) + maxtoks = tokens; + currsz = 0; + tokens = 0; + } + readsmt = 0; + continue; + } + + if(c == '/') { + nc = fgetc(input); + if(nc == '/') { + gountilbrk(input); + continue; + } + ungetc(nc, input); + } + + currsz++; + readsmt = 1; + } + rewind(input); + (*lncount) = lns; + (*widestln) = widest; + (*maxtokens) = maxtoks; +} + +struct line** parse(FILE* input, int lncount, int widestln, int maxtokens) { + struct line** lns = (struct line**)malloc(sizeof(struct line*)*lncount); + char c, nc; + short readsmt = 0; + char tmp[widestln]; + char* tokens[maxtokens]; + int tmpind = 0; + int lnsind = 0; + int tokensind = 0; + int truelncount = 0; + while(c = fgetc(input), c != -1) { + if(isspace(c)) { + if(readsmt) { + tmp[tmpind] = '\0'; + char* newtoken = (char*)malloc(sizeof(char)*tmpind+1); + strcpy(newtoken, tmp); + tokens[tokensind] = newtoken; + tmpind = 0; + readsmt = 0; + tokensind++; + } + if(c == '\n') { + truelncount++; + if(tokensind > 0) { + struct line* newln = (struct line*)malloc(sizeof(struct line)); + newln->tokens = (char**)malloc(sizeof(char*)*tokensind); + for(int i = 0; i < tokensind; i++) { + newln->tokens[i] = tokens[i]; + } + newln->tokenscount = tokensind; + newln->truen = truelncount; + lns[lnsind] = newln; + lnsind++; + tokensind = 0; + } + } + continue; + } + + if(c == '/') { + nc = fgetc(input); + if(nc == '/') { + gountilbrk(input); + continue; + } + ungetc(nc, input); + } + + tmp[tmpind] = c; + tmpind++; + readsmt = 1; + } + return lns; +} diff --git a/parser.h b/parser.h new file mode 100644 index 0000000..a72d102 --- /dev/null +++ b/parser.h @@ -0,0 +1,11 @@ +#include +#include + +struct line { + char** tokens; + int tokenscount; + int truen; +}; + +void getinfo(FILE* input, int* lncount, int* widestln, int* maxtokens); +struct line** parse(FILE* input, int lncount, int widestln, int maxtokens);