| 1 | # Makefile for re2c/lemon parser generator |
| 2 | |
| 3 | # Check if we have re2c and lemon in the system |
| 4 | HAVE_RE2C := $(shell command -v re2c 2>/dev/null) |
| 5 | HAVE_LEMON := $(shell command -v lemon 2>/dev/null) |
| 6 | |
| 7 | # Variables for lemon if we need to download it |
| 8 | LEMON_C = lemon.c |
| 9 | LEMPAR_C = lempar.c |
| 10 | LEMON_URL = https://raw.githubusercontent.com/sqlite/sqlite/master/tool/lemon.c |
| 11 | LEMPAR_URL = https://raw.githubusercontent.com/sqlite/sqlite/master/tool/lempar.c |
| 12 | |
| 13 | all: lexer.c parser.c parser.h |
| 14 | |
| 15 | # Generate lexer from re2c file |
| 16 | lexer.c: lexer.re |
| 17 | ifndef HAVE_RE2C |
| 18 | $(error re2c is not installed. Please install re2c to build this project) |
| 19 | else |
| 20 | re2c -o $@ $< |
| 21 | endif |
| 22 | |
| 23 | # Generate lemon parser from grammar |
| 24 | parser.c parser.h: parser.y $(LEMPAR_C) |
| 25 | ifdef HAVE_LEMON |
| 26 | lemon -s -T$(LEMPAR_C) $< |
| 27 | else |
| 28 | $(CC) -o lemon $(LEMON_C) |
| 29 | ./lemon -s -T$(LEMPAR_C) $< |
| 30 | endif |
| 31 | @if [ ! -f parser.c ]; then \ |
| 32 | echo "Error: parser.c was not generated"; \ |
| 33 | exit 1; \ |
| 34 | fi |
| 35 | |
| 36 | # Download lemon sources if needed |
| 37 | $(LEMON_C): |
| 38 | ifndef HAVE_LEMON |
| 39 | @echo "Downloading lemon source..." |
| 40 | curl -s -o $(LEMON_C) $(LEMON_URL) |
| 41 | endif |
| 42 | |
| 43 | # Download lempar.c template |
| 44 | $(LEMPAR_C): |
| 45 | @echo "Downloading lempar.c template..." |
| 46 | curl -s -o $(LEMPAR_C) $(LEMPAR_URL) |
| 47 | |
| 48 | # Clean rule |
| 49 | clean: |
| 50 | rm -f lexer.c parser.c parser.h parser.out lemon $(LEMON_C) $(LEMPAR_C) |
| 51 | |
| 52 | .PHONY: all clean |