Begin bison-based parsing.

This commit is contained in:
Jacob Signorovitch 2024-11-02 10:31:55 -04:00
parent a1f210fee1
commit ecc12f6f3b
5 changed files with 37 additions and 1 deletions

1
.gitignore vendored
View File

@ -3,4 +3,5 @@
tags
*.out
.cache
build/*
compile_commands.json

View File

@ -3,8 +3,10 @@ NAME = scl
TARGET = $(NAME).out
SRC_DIR = src
INC_DIR = $(SRC_DIR)/include
BUILD_DIR = build
OBJ_DIR = $(BUILD_DIR)/obj
GRAM_DIR = $(BUILD_DIR)/grammars
TEST_DIR = test
TEST_BUILD_DIR = $(BUILD_DIR)/test
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
@ -36,13 +38,22 @@ run: $(TARGET)
@ echo -e "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)"
@ ./$(TARGET)
# Generate grammars with bison.
grammars: $(SRC_DIR)/grammar.y
@ mkdir -p $(GRAM_DIR)
@ echo -e "$(WHITE_BOLD)Generating grammars...$(RESETCOLOR) bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h"
@ bison $< -o$(GRAM_DIR)/grammar.tab.c -H$(GRAM_DIR)/grammar.tab.h
# Link to final binary.
$(TARGET): $(OBJ_FILES)
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)"
@ $(LINK) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)
# Lexer depends on grammars.
$(OBJ_DIR)/lexer.o: grammars
# Compile project sources.
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(SRC_DIR)/include/%.h
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(INC_DIR)/%.h
@ mkdir -p $(OBJ_DIR)
@ echo -e "$(WHITE_BOLD)Compiling $(WHITE)$<$(WHITE_BOLD)... $(RESETCOLOR)$(CC) $(CFLAGS) -c $< -o $@"
@ $(CC) $(CFLAGS) -c $< -o $@

17
src/grammar.y Normal file
View File

@ -0,0 +1,17 @@
%union {
int intval;
char* strval;
}
%token <intval> NUM
%token <strval> CALL
%token PLUS
%%
exp:
NUM {}
| exp PLUS exp {}
;
%%

View File

@ -68,4 +68,7 @@ void lexer_print_i(Lexer* lexer, int ilvl);
// Print a representation of a LexerState.
void lexerstate_print_raw(LexerState s);
// Interface with bison.
int yylex();
#endif

View File

@ -143,3 +143,7 @@ void lexerstate_print_raw(LexerState s) {
log_dbgf("%d is not a valid LexerState (max: %d)", s, TOKEN_TYPE_MAX);
} else printf("%s", lexerstate_names[s]);
}
int yylex() {
lexer_lex()
}