Print parsed tree on run
This commit is contained in:
parent
4c1f95370c
commit
5392258a0d
@ -2,6 +2,7 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "printtree.h"
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
@ -18,5 +19,6 @@ int main(int argc, char *argv[]) {
|
|||||||
fprintf(stderr, "Error during parsing.\n");
|
fprintf(stderr, "Error during parsing.\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
printtree(code,0);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
|
#ifndef PARSER_H
|
||||||
|
#define PARSER_H
|
||||||
enum SyntaxElementType {
|
enum SyntaxElementType {
|
||||||
TOPTREE,
|
TOPTREE,
|
||||||
TREE,
|
TREE,
|
||||||
@ -17,3 +19,4 @@ struct SyntaxElement {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct SyntaxElement* parse(FILE* file);
|
struct SyntaxElement* parse(FILE* file);
|
||||||
|
#endif
|
||||||
|
37
src/printtree.c
Normal file
37
src/printtree.c
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "parser.h"
|
||||||
|
|
||||||
|
void printtree(struct SyntaxElement* syntaxtree, int depth) {
|
||||||
|
if (syntaxtree == NULL)
|
||||||
|
return;
|
||||||
|
for (int i = 1; i <= depth; i++)
|
||||||
|
putc(' ',stdout);
|
||||||
|
switch (syntaxtree->type) {
|
||||||
|
case TOPTREE:
|
||||||
|
printf("TOPTREE\n");
|
||||||
|
printtree((struct SyntaxElement*)syntaxtree->content, depth + 1);
|
||||||
|
break;
|
||||||
|
case TREE:
|
||||||
|
printf("TREE\n");
|
||||||
|
printtree((struct SyntaxElement*)syntaxtree->content, depth + 1);
|
||||||
|
break;
|
||||||
|
case TOKEN:
|
||||||
|
printf("TOKEN: %s\n", (char*)syntaxtree->content);
|
||||||
|
break;
|
||||||
|
case STRING:
|
||||||
|
printf("STRING: %s\n", (char*)syntaxtree->content);
|
||||||
|
break;
|
||||||
|
case NUMBER:
|
||||||
|
// TODO: printtree NUMBER
|
||||||
|
fprintf(stderr,"TODO: printtree NUMBER\n");
|
||||||
|
exit(1);
|
||||||
|
case NONE:
|
||||||
|
printf("NONE\n");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (syntaxtree->next != NULL)
|
||||||
|
printtree(syntaxtree->next, depth);
|
||||||
|
return;
|
||||||
|
}
|
2
src/printtree.h
Normal file
2
src/printtree.h
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
#include "parser.h"
|
||||||
|
void printtree(struct SyntaxElement* syntaxtree, int depth);
|
Loading…
Reference in New Issue
Block a user