From bb76c2fe054168057e709e274a6e5d0a6aa16b80 Mon Sep 17 00:00:00 2001 From: Xiaofan Sun Date: Thu, 15 Oct 2026 16:38:14 +0000 Subject: [PATCH 1/3] Add modern C++ Flex/Bison calculator example --- CMakeLists.txt | 1 + README.md | 14 ++++++++++++ modern-cpp/CMakeLists.txt | 12 +++++++++++ modern-cpp/Makefile | 26 ++++++++++++++++++++++ modern-cpp/README.md | 42 ++++++++++++++++++++++++++++++++++++ modern-cpp/lexer.l | 21 ++++++++++++++++++ modern-cpp/main.cpp | 19 +++++++++++++++++ modern-cpp/parser.y | 45 +++++++++++++++++++++++++++++++++++++++ 8 files changed, 180 insertions(+) create mode 100644 modern-cpp/CMakeLists.txt create mode 100644 modern-cpp/Makefile create mode 100644 modern-cpp/README.md create mode 100644 modern-cpp/lexer.l create mode 100644 modern-cpp/main.cpp create mode 100644 modern-cpp/parser.y diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c75a12..de30f7e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,3 +11,4 @@ add_subdirectory(error-recovery) add_subdirectory(optional-semicolon) add_subdirectory(python-like-indentation) +add_subdirectory(modern-cpp) diff --git a/README.md b/README.md index 2b8d591..2aa08de 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,20 @@ # Flex/Bison Examples +## Tool versions and modern example + +Upstream NEWS checked on 2026-10-16 still lists **Flex 2.6.4** (2017-05-06) +and **Bison 3.8.2** (2021-09-25) as the latest stable releases; development +changes above these entries are unreleased: +[Flex NEWS](https://github.com/westes/flex/blob/master/NEWS), +[Bison NEWS](https://github.com/akimd/bison/blob/master/NEWS). +The older examples are not all written for the latest Bison API. + +新增 [`modern-cpp`](modern-cpp/):面向 Flex 2.6.4、Bison 3.8+、C++17 的计算器, +包含可重入扫描器、variant/token constructor、RAII、位置跟踪、detailed 错误提示与 LAC。 +提供独立 Make/CMake 构建;其他示例保留原有写法,便于对照。 +截至 2026-10-16,上游最新稳定版仍为上述版本,不能仅凭发布时间较早判断已经过时。 + Here is a list of flex/bison examples to show some advanced features in the newest versions of flex/bison. There are several topics in this list: 1. Reentrancy - this is important for parsing in multithread programs diff --git a/modern-cpp/CMakeLists.txt b/modern-cpp/CMakeLists.txt new file mode 100644 index 0000000..bfc17e1 --- /dev/null +++ b/modern-cpp/CMakeLists.txt @@ -0,0 +1,12 @@ +cmake_minimum_required(VERSION 3.16) +project(modern-cpp LANGUAGES CXX) +find_package(BISON 3.8 REQUIRED) +find_package(FLEX 2.6.4 REQUIRED) +BISON_TARGET(ModernParser parser.y ${CMAKE_CURRENT_BINARY_DIR}/parser.cpp + DEFINES_FILE ${CMAKE_CURRENT_BINARY_DIR}/parser.hpp + COMPILE_FLAGS "-Wall -Werror") +FLEX_TARGET(ModernLexer lexer.l ${CMAKE_CURRENT_BINARY_DIR}/lexer.cpp) +ADD_FLEX_BISON_DEPENDENCY(ModernLexer ModernParser) +add_executable(modern-cpp main.cpp ${BISON_ModernParser_OUTPUTS} ${FLEX_ModernLexer_OUTPUTS}) +target_compile_features(modern-cpp PRIVATE cxx_std_17) +target_include_directories(modern-cpp PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) diff --git a/modern-cpp/Makefile b/modern-cpp/Makefile new file mode 100644 index 0000000..71eda53 --- /dev/null +++ b/modern-cpp/Makefile @@ -0,0 +1,26 @@ +CXX = g++ +CXXFLAGS ?= -O2 -Wall -Wextra -std=c++17 +BISON = bison +FLEX = flex +BUILD = build + +.PHONY: all clean +all: $(BUILD)/modern-cpp + +$(BUILD): + mkdir -p $@ + +$(BUILD)/parser.cpp: parser.y | $(BUILD) + $(BISON) -Wall -Werror --defines=$(BUILD)/parser.hpp -o $@ $< + +$(BUILD)/parser.hpp: $(BUILD)/parser.cpp + @test -f $@ || $(BISON) -Wall -Werror --defines=$@ -o $(BUILD)/parser.cpp parser.y + +$(BUILD)/lexer.cpp: lexer.l $(BUILD)/parser.hpp + $(FLEX) -o $@ $< + +$(BUILD)/modern-cpp: main.cpp $(BUILD)/parser.cpp $(BUILD)/lexer.cpp $(BUILD)/parser.hpp + $(CXX) $(CXXFLAGS) -I$(BUILD) main.cpp $(BUILD)/parser.cpp $(BUILD)/lexer.cpp -o $@ + +clean: + rm -rf $(BUILD) diff --git a/modern-cpp/README.md b/modern-cpp/README.md new file mode 100644 index 0000000..fe68ee1 --- /dev/null +++ b/modern-cpp/README.md @@ -0,0 +1,42 @@ +# Modern C++ calculator / 现代 C++ 示例 + +Requires Flex >= 2.6.4, Bison >= 3.8 (tested with 3.8.2), a C++17 compiler, +and Make or CMake >= 3.16. These version requirements apply to this example; +older examples retain their existing requirements. + +需要 Flex 2.6.4、Bison 3.8+ 和 C++17。此示例演示可重入扫描器、RAII 资源释放、 +variant 语义值、token constructor、位置跟踪,以及 detailed + LAC 精确语法错误提示。 +`api.namespace` 替代旧的 `name-prefix`,不依赖全局解析状态。 +Bison 的 variant 是其生成器提供的类型容器,并非 `std::variant`。 + +```sh +cd modern-cpp +make -j +printf '1 + 2 * 3\n-(4 + 2) / 2\n' | ./build/modern-cpp +# 7 +# -3 +``` + +Or build independently with CMake / 也可以独立使用 CMake: + +```sh +cmake -S modern-cpp -B build/modern-cpp +cmake --build build/modern-cpp --parallel +printf '1 + 2 * 3\n' | ./build/modern-cpp/modern-cpp +``` + +Each expression must end with a newline (LF or CRLF); blank lines are allowed. +Supports decimal numbers, parentheses, unary minus, and `+ - * /`. +Invalid characters, syntax errors, and division by zero print a location to stderr +and return a nonzero exit code. This example stops at the first error; +see `error-recovery` for recovery across multiple lines. + +每个表达式以换行结束,支持空行、小数、括号和一元负号。错误时输出行列位置并返回失败; +此例在首个错误处停止,多行错误恢复请参照 `error-recovery`。 + +```sh +printf '1 + )\n' | ./build/modern-cpp +# 1.5: syntax error, unexpected ) ... +printf '1 / 0\n' | ./build/modern-cpp +# 1.5: division by zero +``` diff --git a/modern-cpp/lexer.l b/modern-cpp/lexer.l new file mode 100644 index 0000000..2efedd9 --- /dev/null +++ b/modern-cpp/lexer.l @@ -0,0 +1,21 @@ +%option reentrant noyywrap nounput noinput nodefault +%{ +#include "parser.hpp" +#include +#include +#define YY_DECL calc::Parser::symbol_type yylex(yyscan_t yyscanner, calc::location& loc) +#define YY_USER_ACTION loc.step(); loc.columns(yyleng); +%} +%% +[ \t]+ {} +\r\n|\n|\r { loc.lines(1); return calc::Parser::make_EOL(loc); } +([0-9]+(\.[0-9]*)?|\.[0-9]+) { return calc::Parser::make_NUMBER(std::strtod(yytext, nullptr), loc); } +"+" { return calc::Parser::make_PLUS(loc); } +"-" { return calc::Parser::make_MINUS(loc); } +"*" { return calc::Parser::make_STAR(loc); } +"/" { return calc::Parser::make_SLASH(loc); } +"(" { return calc::Parser::make_LPAREN(loc); } +")" { return calc::Parser::make_RPAREN(loc); } +<> { loc.step(); return calc::Parser::make_YYEOF(loc); } +. { throw calc::Parser::syntax_error(loc, "invalid character: " + std::string(yytext, yyleng)); } +%% diff --git a/modern-cpp/main.cpp b/modern-cpp/main.cpp new file mode 100644 index 0000000..d2a3e41 --- /dev/null +++ b/modern-cpp/main.cpp @@ -0,0 +1,19 @@ +#include "parser.hpp" +#include +#include + +// Flex's reentrant scanner handle is opaque; no global scanner state is used. +int yylex_init(void** scanner); +int yylex_destroy(void* scanner); + +int main() { + void* raw = nullptr; + if (yylex_init(&raw) != 0) { + std::cerr << "cannot initialize scanner\n"; + return 1; + } + std::unique_ptr scanner(raw, &yylex_destroy); + calc::location loc; + calc::Parser parser(scanner.get(), loc); + return parser.parse(); +} diff --git a/modern-cpp/parser.y b/modern-cpp/parser.y new file mode 100644 index 0000000..ad2f68f --- /dev/null +++ b/modern-cpp/parser.y @@ -0,0 +1,45 @@ +%require "3.8" +%skeleton "lalr1.cc" +%defines +%define api.namespace {calc} +%define api.parser.class {Parser} +%define api.value.type variant +%define api.token.constructor +%define parse.error detailed +%define parse.lac full +%locations +%parse-param {void* scanner} {calc::location& loc} +%lex-param {void* scanner} {calc::location& loc} +%code requires { +#include +} +%code { +#include +calc::Parser::symbol_type yylex(void* scanner, calc::location& loc); +} +%token NUMBER "number" +%token PLUS "+" MINUS "-" STAR "*" SLASH "/" +%token LPAREN "(" RPAREN ")" EOL "end of line" +%nterm expr +%left PLUS MINUS +%left STAR SLASH +%precedence NEGATE +%% +input: %empty | input line; +line: EOL | expr EOL { std::cout << $1 << '\n'; }; +expr: + NUMBER +| expr PLUS expr { $$ = $1 + $3; } +| expr MINUS expr { $$ = $1 - $3; } +| expr STAR expr { $$ = $1 * $3; } +| expr SLASH expr { + if ($3 == 0) throw syntax_error(@3, "division by zero"); + $$ = $1 / $3; + } +| MINUS expr %prec NEGATE { $$ = -$2; } +| LPAREN expr RPAREN { $$ = $2; } +; +%% +void calc::Parser::error(const location_type& where, const std::string& message) { + std::cerr << where << ": " << message << '\n'; +} From 43e1ec8041e44d8878b3e24b2d50184093f1cc3b Mon Sep 17 00:00:00 2001 From: Xiaofan Sun Date: Thu, 15 Oct 2026 16:39:18 +0000 Subject: [PATCH 2/3] Add CI matrix and calculator regression tests --- .github/workflows/ci.yml | 38 ++++++++++++++++++++++++++++ modern-cpp/README.md | 16 ++++++++++++ modern-cpp/test.py | 54 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 .github/workflows/ci.yml create mode 100644 modern-cpp/test.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..1f453d0 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,38 @@ +name: Modern C++ example + +on: + push: + pull_request: + workflow_dispatch: + +permissions: + contents: read + +jobs: + build-and-test: + name: ${{ matrix.os }} / ${{ matrix.build }} + runs-on: ${{ matrix.os }} + timeout-minutes: 10 + strategy: + fail-fast: false + matrix: + os: [ubuntu-22.04, ubuntu-24.04] + build: [make, cmake] + steps: + - uses: actions/checkout@v4 + - name: Install dependencies + run: | + sudo apt-get update + sudo apt-get install -y flex bison g++ make cmake python3 + flex --version + bison --version + - name: Build with Make + if: matrix.build == 'make' + run: make -C modern-cpp -j2 + - name: Build with CMake + if: matrix.build == 'cmake' + run: | + cmake -S modern-cpp -B modern-cpp/build -DCMAKE_BUILD_TYPE=Release + cmake --build modern-cpp/build --parallel 2 + - name: Test calculator + run: python3 modern-cpp/test.py modern-cpp/build/modern-cpp diff --git a/modern-cpp/README.md b/modern-cpp/README.md index fe68ee1..55c6336 100644 --- a/modern-cpp/README.md +++ b/modern-cpp/README.md @@ -40,3 +40,19 @@ printf '1 + )\n' | ./build/modern-cpp printf '1 / 0\n' | ./build/modern-cpp # 1.5: division by zero ``` + +## Automated tests / 自动测试 + +```sh +python3 modern-cpp/test.py modern-cpp/build/modern-cpp +``` + +Run this command from the repository root after either build above (for the +standalone CMake path, pass `build/modern-cpp/modern-cpp` instead). +Tests check results, exit codes, and diagnostics for valid and invalid input. +GitHub Actions runs both Make and CMake on Ubuntu 22.04 and 24.04 for pushes and +pull requests; it can also be triggered manually. + +在仓库根目录执行上述命令;若按前面的 CMake 路径构建,则传入 +`build/modern-cpp/modern-cpp`。CI 在两个 Ubuntu 版本上分别构建并测试两种构建方式, +覆盖运算优先级、小数、换行格式、错误位置、除零及不完整输入。 diff --git a/modern-cpp/test.py b/modern-cpp/test.py new file mode 100644 index 0000000..9bd6c05 --- /dev/null +++ b/modern-cpp/test.py @@ -0,0 +1,54 @@ +"""Black-box regression tests; run with a path to the calculator executable.""" + +import pathlib +import subprocess +import sys +import unittest + + +BINARY = pathlib.Path(sys.argv.pop(1)).resolve() if len(sys.argv) > 1 else ( + pathlib.Path(__file__).resolve().parent / "build" / "modern-cpp" +) + + +class CalculatorTest(unittest.TestCase): + def run_calculator(self, source): + return subprocess.run( + [str(BINARY)], input=source, text=True, capture_output=True, timeout=5 + ) + + def test_valid_expressions(self): + cases = [ + ("", ""), + ("\n \t\n", ""), + ("1 + 2 * 3\n", "7\n"), + ("-(4 + 2) / 2\n", "-3\n"), + ("8 / 2 / 2\n10 - 3 - 2\n", "2\n5\n"), + (".5 + 1.25\n2. * 3\n", "1.75\n6\n"), + ("\r\n1 + 2\r\n4\r", "3\n4\n"), + ] + for source, expected in cases: + with self.subTest(source=source): + result = self.run_calculator(source) + self.assertEqual(result.returncode, 0, result.stderr) + self.assertEqual(result.stdout, expected) + self.assertEqual(result.stderr, "") + + def test_errors(self): + cases = [ + ("1 + )\n", "1.5: syntax error, unexpected )"), + ("1 / 0\n", "1.5: division by zero"), + ("\n @\n", "2.3: invalid character: @"), + ("1 + 2", "unexpected end of file"), + ("(1 + 2\n", "syntax error"), + ] + for source, diagnostic in cases: + with self.subTest(source=source): + result = self.run_calculator(source) + self.assertNotEqual(result.returncode, 0) + self.assertEqual(result.stdout, "") + self.assertIn(diagnostic, result.stderr) + + +if __name__ == "__main__": + unittest.main() From 9e5e81a96bc56bf7e40044096c81021b8f3f0f61 Mon Sep 17 00:00:00 2001 From: Xiaofan Sun Date: Thu, 15 Oct 2026 16:43:16 +0000 Subject: [PATCH 3/3] Simplify CI to two Ubuntu 24.04 build jobs --- .github/workflows/ci.yml | 5 ++--- modern-cpp/README.md | 4 ++-- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f453d0..621d191 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,13 +10,12 @@ permissions: jobs: build-and-test: - name: ${{ matrix.os }} / ${{ matrix.build }} - runs-on: ${{ matrix.os }} + name: Ubuntu 24.04 / ${{ matrix.build }} + runs-on: ubuntu-24.04 timeout-minutes: 10 strategy: fail-fast: false matrix: - os: [ubuntu-22.04, ubuntu-24.04] build: [make, cmake] steps: - uses: actions/checkout@v4 diff --git a/modern-cpp/README.md b/modern-cpp/README.md index 55c6336..155e511 100644 --- a/modern-cpp/README.md +++ b/modern-cpp/README.md @@ -50,9 +50,9 @@ python3 modern-cpp/test.py modern-cpp/build/modern-cpp Run this command from the repository root after either build above (for the standalone CMake path, pass `build/modern-cpp/modern-cpp` instead). Tests check results, exit codes, and diagnostics for valid and invalid input. -GitHub Actions runs both Make and CMake on Ubuntu 22.04 and 24.04 for pushes and +GitHub Actions runs both Make and CMake on Ubuntu 24.04 for pushes and pull requests; it can also be triggered manually. 在仓库根目录执行上述命令;若按前面的 CMake 路径构建,则传入 -`build/modern-cpp/modern-cpp`。CI 在两个 Ubuntu 版本上分别构建并测试两种构建方式, +`build/modern-cpp/modern-cpp`。CI 在 Ubuntu 24.04 上分别构建并测试两种构建方式, 覆盖运算优先级、小数、换行格式、错误位置、除零及不完整输入。