Begin bison-based parsing.
This commit is contained in:
parent
a1f210fee1
commit
ecc12f6f3b
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,4 +3,5 @@
|
|||||||
tags
|
tags
|
||||||
*.out
|
*.out
|
||||||
.cache
|
.cache
|
||||||
|
build/*
|
||||||
compile_commands.json
|
compile_commands.json
|
||||||
|
13
Makefile
13
Makefile
@ -3,8 +3,10 @@ NAME = scl
|
|||||||
TARGET = $(NAME).out
|
TARGET = $(NAME).out
|
||||||
|
|
||||||
SRC_DIR = src
|
SRC_DIR = src
|
||||||
|
INC_DIR = $(SRC_DIR)/include
|
||||||
BUILD_DIR = build
|
BUILD_DIR = build
|
||||||
OBJ_DIR = $(BUILD_DIR)/obj
|
OBJ_DIR = $(BUILD_DIR)/obj
|
||||||
|
GRAM_DIR = $(BUILD_DIR)/grammars
|
||||||
TEST_DIR = test
|
TEST_DIR = test
|
||||||
TEST_BUILD_DIR = $(BUILD_DIR)/test
|
TEST_BUILD_DIR = $(BUILD_DIR)/test
|
||||||
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
|
TEST_OBJ_DIR = $(TEST_BUILD_DIR)/obj
|
||||||
@ -36,13 +38,22 @@ run: $(TARGET)
|
|||||||
@ echo -e "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)"
|
@ echo -e "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)"
|
||||||
@ ./$(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.
|
# Link to final binary.
|
||||||
$(TARGET): $(OBJ_FILES)
|
$(TARGET): $(OBJ_FILES)
|
||||||
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)"
|
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)"
|
||||||
@ $(LINK) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)
|
@ $(LINK) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)
|
||||||
|
|
||||||
|
# Lexer depends on grammars.
|
||||||
|
$(OBJ_DIR)/lexer.o: grammars
|
||||||
|
|
||||||
# Compile project sources.
|
# 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)
|
@ mkdir -p $(OBJ_DIR)
|
||||||
@ echo -e "$(WHITE_BOLD)Compiling $(WHITE)$<$(WHITE_BOLD)... $(RESETCOLOR)$(CC) $(CFLAGS) -c $< -o $@"
|
@ echo -e "$(WHITE_BOLD)Compiling $(WHITE)$<$(WHITE_BOLD)... $(RESETCOLOR)$(CC) $(CFLAGS) -c $< -o $@"
|
||||||
@ $(CC) $(CFLAGS) -c $< -o $@
|
@ $(CC) $(CFLAGS) -c $< -o $@
|
||||||
|
17
src/grammar.y
Normal file
17
src/grammar.y
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
%union {
|
||||||
|
int intval;
|
||||||
|
char* strval;
|
||||||
|
}
|
||||||
|
|
||||||
|
%token <intval> NUM
|
||||||
|
%token <strval> CALL
|
||||||
|
%token PLUS
|
||||||
|
|
||||||
|
%%
|
||||||
|
|
||||||
|
exp:
|
||||||
|
NUM {}
|
||||||
|
| exp PLUS exp {}
|
||||||
|
;
|
||||||
|
|
||||||
|
%%
|
@ -68,4 +68,7 @@ void lexer_print_i(Lexer* lexer, int ilvl);
|
|||||||
// Print a representation of a LexerState.
|
// Print a representation of a LexerState.
|
||||||
void lexerstate_print_raw(LexerState s);
|
void lexerstate_print_raw(LexerState s);
|
||||||
|
|
||||||
|
// Interface with bison.
|
||||||
|
int yylex();
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -143,3 +143,7 @@ void lexerstate_print_raw(LexerState s) {
|
|||||||
log_dbgf("%d is not a valid LexerState (max: %d)", s, TOKEN_TYPE_MAX);
|
log_dbgf("%d is not a valid LexerState (max: %d)", s, TOKEN_TYPE_MAX);
|
||||||
} else printf("%s", lexerstate_names[s]);
|
} else printf("%s", lexerstate_names[s]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int yylex() {
|
||||||
|
lexer_lex()
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user