Initial commit for yacc shaving contest. - brcon2024-hackathons - Bitreichcon 2… | |
git clone git://bitreich.org/brcon2024-hackathons git://enlrupgkhuxnvlhsf6lc3fz… | |
Log | |
Files | |
Refs | |
Tags | |
--- | |
commit fe1953cc524f2263d68e4bd294c5c97364270469 | |
Author: Christoph Lohmann <[email protected]> | |
Date: Wed, 12 Jun 2024 16:39:10 +0200 | |
Initial commit for yacc shaving contest. | |
Diffstat: | |
A yacc-shaving/brainfuck/README.md | 11 +++++++++++ | |
A yacc-shaving/brainfuck/brainfuck-e… | 2 ++ | |
A yacc-shaving/brainfuck/brainfuck.l | 22 ++++++++++++++++++++++ | |
A yacc-shaving/brainfuck/brainfuck.y | 45 +++++++++++++++++++++++++++++… | |
A yacc-shaving/brainfuck/makefile | 34 +++++++++++++++++++++++++++++… | |
5 files changed, 114 insertions(+), 0 deletions(-) | |
--- | |
diff --git a/yacc-shaving/brainfuck/README.md b/yacc-shaving/brainfuck/README.md | |
@@ -0,0 +1,11 @@ | |
+# Brainfuck Example | |
+ | |
+This is a example to get the grasp on how yacc(1) and lex(1) interact. | |
+ | |
+## Build Instructions | |
+ | |
+ % make | |
+ % ./brainfuck < brainfuck-example.bf | |
+ | |
+Have fun! | |
+ | |
diff --git a/yacc-shaving/brainfuck/brainfuck-example.bf b/yacc-shaving/brainfu… | |
@@ -0,0 +1,2 @@ | |
+[->+<] | |
+ | |
diff --git a/yacc-shaving/brainfuck/brainfuck.l b/yacc-shaving/brainfuck/brainf… | |
@@ -0,0 +1,22 @@ | |
+%{ | |
+#include "y.tab.h" | |
+%} | |
+ | |
+%% | |
+ | |
+">" { return INCPTR; } | |
+"<" { return DECPTR; } | |
+"+" { return INCVAL; } | |
+"-" { return DECVAL; } | |
+"." { return OUTPUT; } | |
+"," { return INPUT; } | |
+"[" { return LOOPSTART; } | |
+"]" { return LOOPEND; } | |
+. { /* ignore any other character */ } | |
+ | |
+%% | |
+ | |
+int yywrap(void) { | |
+ return 1; | |
+} | |
+ | |
diff --git a/yacc-shaving/brainfuck/brainfuck.y b/yacc-shaving/brainfuck/brainf… | |
@@ -0,0 +1,45 @@ | |
+%{ | |
+#include <stdio.h> | |
+ | |
+void yyerror(const char *s); | |
+int yylex(void); | |
+ | |
+%} | |
+ | |
+%token INCPTR DECPTR INCVAL DECVAL OUTPUT INPUT LOOPSTART LOOPEND | |
+ | |
+%% | |
+ | |
+program: | |
+ /* empty */ | |
+ | program command | |
+ ; | |
+ | |
+command: | |
+ INCPTR { printf("Move pointer to the right\n"); } | |
+ | DECPTR { printf("Move pointer to the left\n"); } | |
+ | INCVAL { printf("Increment the value at the pointer\n"); } | |
+ | DECVAL { printf("Decrement the value at the pointer\n"); } | |
+ | OUTPUT { printf("Output the value at the pointer\n"); } | |
+ | INPUT { printf("Input a value and store it at the pointer\n"); } | |
+ | loop | |
+ ; | |
+ | |
+loop: | |
+ LOOPSTART program LOOPEND | |
+ { | |
+ printf("Start of loop\n"); | |
+ printf("End of loop\n"); | |
+ } | |
+ ; | |
+ | |
+%% | |
+ | |
+void yyerror(const char *s) { | |
+ fprintf(stderr, "Error: %s\n", s); | |
+} | |
+ | |
+int main(void) { | |
+ return yyparse(); | |
+} | |
+ | |
diff --git a/yacc-shaving/brainfuck/makefile b/yacc-shaving/brainfuck/makefile | |
@@ -0,0 +1,34 @@ | |
+CC = gcc | |
+LEX = lex | |
+YACC = yacc | |
+YACCFLAGS = -d | |
+ | |
+TARGET = brainfuck | |
+LEX_SRC = brainfuck.l | |
+YACC_SRC = brainfuck.y | |
+LEX_GEN = lex.yy.c | |
+YACC_GEN = y.tab.c y.tab.h | |
+OBJ = y.tab.o lex.yy.o | |
+ | |
+all: $(TARGET) | |
+ | |
+$(TARGET): $(OBJ) | |
+ $(CC) -o $@ $^ | |
+ | |
+lex.yy.o: $(LEX_GEN) | |
+ $(CC) -c $< -o $@ | |
+ | |
+y.tab.o: y.tab.c | |
+ $(CC) -c $< -o $@ | |
+ | |
+$(LEX_GEN): $(LEX_SRC) | |
+ $(LEX) $< | |
+ | |
+$(YACC_GEN): $(YACC_SRC) | |
+ $(YACC) $(YACCFLAGS) $< | |
+ | |
+clean: | |
+ rm -f $(TARGET) $(OBJ) $(LEX_GEN) $(YACC_GEN) | |
+ | |
+.PHONY: all clean | |
+ |