Skip to content

Commit 4e4e8cf

Browse files
committed
Auto merge of #21452 - bleibig:bison-grammar, r=nikomatsakis
This adds a new lexer/parser combo for the entire Rust language can be generated with with flex and bison, taken from my project at https://github.com/bleibig/rust-grammar. There is also a testing script that runs the generated parser with all *.rs files in the repository (except for tests in compile-fail or ones that marked as "ignore-test" or "ignore-lexer-test"). If you have flex and bison installed, you can run these tests using the new "check-grammar" make target. This does not depend on or interact with the existing testing code in the grammar, which only provides and tests a lexer specification. OS X users should take note that the version of bison that comes with the Xcode toolchain (2.3) is too old to work with this grammar, they need to download and install version 3.0 or later. The parser builds up an S-expression-based AST, which can be displayed by giving the "-v" argument to parser-lalr (normally it only gives output on error). It is only a rough approximation of what is parsed and doesn't capture every detail and nuance of the program. Hopefully this should be sufficient for issue #2234, or at least a good starting point.
2 parents bb7cc4e + f39297f commit 4e4e8cf

File tree

7 files changed

+2663
-0
lines changed

7 files changed

+2663
-0
lines changed

configure

+2
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,8 @@ probe CFG_ISCC iscc
645645
probe CFG_JAVAC javac
646646
probe CFG_ANTLR4 antlr4
647647
probe CFG_GRUN grun
648+
probe CFG_FLEX flex
649+
probe CFG_BISON bison
648650
probe CFG_PANDOC pandoc
649651
probe CFG_PDFLATEX pdflatex
650652
probe CFG_XELATEX xelatex

mk/grammar.mk

+48
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ B = $(CFG_BUILD_DIR)/$(CFG_BUILD)/stage2/
1414
L = $(B)lib/rustlib/$(CFG_BUILD)/lib
1515
LD = $(CFG_BUILD)/stage2/lib/rustlib/$(CFG_BUILD)/lib/
1616
RUSTC = $(STAGE2_T_$(CFG_BUILD)_H_$(CFG_BUILD))
17+
ifeq ($(CFG_OSTYPE),apple-darwin)
18+
FLEX_LDFLAGS=-ll
19+
else
20+
FLEX_LDFLAGS=-lfl
21+
endif
1722

1823
# Run the reference lexer against libsyntax and compare the tokens and spans.
1924
# If "// ignore-lexer-test" is present in the file, it will be ignored.
@@ -67,3 +72,46 @@ $(info cfg: javac not available, skipping lexer test...)
6772
check-lexer:
6873

6974
endif
75+
76+
$(BG)lex.yy.c: $(SG)lexer.l $(BG)
77+
@$(call E, flex: $@)
78+
$(Q)$(CFG_FLEX) -o $@ $<
79+
80+
$(BG)lexer-lalr.o: $(BG)lex.yy.c $(BG)parser-lalr.tab.h
81+
@$(call E, cc: $@)
82+
$(Q)$(CFG_CC) -include $(BG)parser-lalr.tab.h -c -o $@ $<
83+
84+
$(BG)parser-lalr.tab.c $(BG)parser-lalr.tab.h: $(SG)parser-lalr.y
85+
@$(call E, bison: $@)
86+
$(Q)$(CFG_BISON) $< --output=$(BG)parser-lalr.tab.c --defines=$(BG)parser-lalr.tab.h \
87+
--name-prefix=rs --warnings=error=all
88+
89+
$(BG)parser-lalr.o: $(BG)parser-lalr.tab.c
90+
@$(call E, cc: $@)
91+
$(Q)$(CFG_CC) -c -o $@ $<
92+
93+
$(BG)parser-lalr-main.o: $(SG)parser-lalr-main.c
94+
@$(call E, cc: $@)
95+
$(Q)$(CFG_CC) -std=c99 -c -o $@ $<
96+
97+
$(BG)parser-lalr: $(BG)parser-lalr.o $(BG)parser-lalr-main.o $(BG)lexer-lalr.o
98+
@$(call E, cc: $@)
99+
$(Q)$(CFG_CC) -o $@ $^ $(FLEX_LDFLAGS)
100+
101+
102+
ifdef CFG_FLEX
103+
ifdef CFG_BISON
104+
check-grammar: $(BG) $(BG)parser-lalr
105+
$(info Verifying grammar ...)
106+
$(SG)testparser.py -p $(BG)parser-lalr -s $(S)src
107+
108+
else
109+
$(info cfg: bison not available, skipping parser test...)
110+
check-grammar:
111+
112+
endif
113+
else
114+
$(info cfg: flex not available, skipping parser test...)
115+
check-grammar:
116+
117+
endif

0 commit comments

Comments
 (0)