Skip to content
This repository was archived by the owner on Nov 23, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions grammar/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ build:
build/parser.c: grammar.y | build
peg $^ > $@

build/full.c: main.c build/parser.c
cat $^ > $@

build/parser: build/full.c
build/parser: main.c | build/parser.c
$(CC) -O3 -o $@ $^

build/parser_debug: build/full.c
build/parser_debug: main.c
$(CC) -O3 -o $@ $^ -DYY_DEBUG
46 changes: 43 additions & 3 deletions grammar/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,50 @@
#include <stdio.h>

int yyparse();
FILE* input = NULL;
static int lineno = 0;
static char* filename= NULL;

#define YY_CTX_LOCAL

#define YY_INPUT(ctx, buf, result, max) { \
int c = getc(input); \
if (c == '\n') ++lineno; \
result = (EOF == c) ? 0 : (*(buf) = c, 1); \
}

#include "build/parser.c"

void yyerror(yycontext* ctx, char* message) {
fprintf(stderr, "%s:%d: %s", filename, lineno, message);

if (ctx->__pos < ctx->__limit || !feof(input)) {
// Find the offending line.
int pos = ctx->__limit;
while (ctx->__pos < pos) {
if (ctx->__buf[pos] == '\n') {
++pos;
break;
}

--pos;
}

ctx->__buf[ctx->__limit] = '\0';
fprintf(stderr, "%s", ctx->__buf + pos);
}

fprintf(stderr, "\n");
}

int main() {
if (yyparse() == 0) {
fprintf(stderr, "Failed!\n");
input = stdin;
lineno = 1;
filename = "<stdin>";

yycontext ctx;
memset(&ctx, 0, sizeof(yycontext));
if (yyparse(&ctx) == 0) {
yyerror(&ctx, "syntax error\n");
return 1;
}

Expand Down