Add parser

This commit is contained in:
Augusto Gunsch 2020-10-29 21:22:03 -03:00
parent 92f003aa7b
commit 94f12e1006
No known key found for this signature in database
GPG Key ID: F7EEFE29825C72DC
5 changed files with 164 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
vmtranslator

7
Makefile Normal file
View File

@ -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}

33
main.c Normal file
View File

@ -0,0 +1,33 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#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;
}

112
parser.c Normal file
View File

@ -0,0 +1,112 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#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;
}

11
parser.h Normal file
View File

@ -0,0 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
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);