I'm trying to get PackCC to parse contents from a file. I open the file, read its contents into a buffer, then pass that inward as an auxiliary value to the .peg
file.
static void parser_read(const char *contents, int mode) {
FILE *fp = fopen(contents, "r");
if (fp == NULL) {
perror("Error opening file");
exit(EXIT_FAILURE);
}
char buffer[2048];
memset(buffer, 0, sizeof buffer);
fgets(buffer, 2048, fp);
fclose(fp);
scheme_context_t *ctx = scheme_create(buffer); // Not sure about this line.
ast *my_ast = NULL;
scheme_parse(ctx, &my_ast);
ast_print(my_ast);
eval_ast(my_ast);
}
Unfortunately, PackCC continues to only read from standard input and seems to completely ignore the auxiliary buffer I supply it. Am I doing this wrong? The PackCC documentation is very vague and almost non-existant with this situation.
PackCC reads input one character at a time using the macro
PCC_GETCHAR(auxil)
. By default, this macro is defined asgetchar()
, and PackCC expects thatPCC_GETCHAR(auxil)
will return values in the same way thatgetchar()
does; that is, an integer between 0 and 255 representing the input character, orEOF
to represent end of file (or an error condition, which I believe is treated as though it were an end of file).The simplest way of reading from a different file is to put a
FILE*
member in your auxiliary data structure and fill it in (by callingfopen
). You do not require a buffer. (In fact, since PackCC itself creates a buffer, creating another one would be redundant.)The code you present does not include the PackCC directives; I'm just assuming that you used something like
%auxil "char*"
. What you want, however, is something like this (untested, I'm afraid):