2021-04-28 23:01:48 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "y.tab.h"
|
|
|
|
#include "ast.h"
|
|
|
|
#include "stack.h"
|
|
|
|
|
|
|
|
extern FILE *yyin;
|
|
|
|
extern int yydebug;
|
|
|
|
|
|
|
|
int Parse(char *inputFilename, Node **pRootNode, uint8_t parseVerbose)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
Stack *stack = CreateStack();
|
|
|
|
yydebug = parseVerbose;
|
|
|
|
|
|
|
|
FILE *fp = fopen(inputFilename, "r");
|
2021-04-28 23:10:17 +00:00
|
|
|
if (fp != NULL)
|
|
|
|
{
|
|
|
|
yyin = fp;
|
|
|
|
result = yyparse(fp, stack, pRootNode);
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf(stderr, "File not found.\n");
|
|
|
|
return 3;
|
|
|
|
}
|
2021-04-28 23:01:48 +00:00
|
|
|
|
|
|
|
/* TODO: free stack */
|
|
|
|
/* TODO: free AST on error */
|
|
|
|
|
|
|
|
if (result == 0)
|
|
|
|
{
|
|
|
|
if (parseVerbose)
|
|
|
|
{
|
|
|
|
PrintTree(*pRootNode, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (result == 1)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Syntax error.\n");
|
|
|
|
}
|
|
|
|
else if (result == 2)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Out of memory.\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|