Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Modern C++ example

on:
push:
pull_request:
workflow_dispatch:

permissions:
contents: read

jobs:
build-and-test:
name: Ubuntu 24.04 / ${{ matrix.build }}
runs-on: ubuntu-24.04
timeout-minutes: 10
strategy:
fail-fast: false
matrix:
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
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ add_subdirectory(error-recovery)
add_subdirectory(optional-semicolon)
add_subdirectory(python-like-indentation)

add_subdirectory(modern-cpp)
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 12 additions & 0 deletions modern-cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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})
26 changes: 26 additions & 0 deletions modern-cpp/Makefile
Original file line number Diff line number Diff line change
@@ -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)
58 changes: 58 additions & 0 deletions modern-cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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
```

## 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 24.04 for pushes and
pull requests; it can also be triggered manually.

在仓库根目录执行上述命令;若按前面的 CMake 路径构建,则传入
`build/modern-cpp/modern-cpp`。CI 在 Ubuntu 24.04 上分别构建并测试两种构建方式,
覆盖运算优先级、小数、换行格式、错误位置、除零及不完整输入。
21 changes: 21 additions & 0 deletions modern-cpp/lexer.l
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
%option reentrant noyywrap nounput noinput nodefault
%{
#include "parser.hpp"
#include <cstdlib>
#include <string>
#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); }
<<EOF>> { loc.step(); return calc::Parser::make_YYEOF(loc); }
. { throw calc::Parser::syntax_error(loc, "invalid character: " + std::string(yytext, yyleng)); }
%%
19 changes: 19 additions & 0 deletions modern-cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "parser.hpp"
#include <iostream>
#include <memory>

// 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<void, decltype(&yylex_destroy)> scanner(raw, &yylex_destroy);
calc::location loc;
calc::Parser parser(scanner.get(), loc);
return parser.parse();
}
45 changes: 45 additions & 0 deletions modern-cpp/parser.y
Original file line number Diff line number Diff line change
@@ -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 <string>
}
%code {
#include <iostream>
calc::Parser::symbol_type yylex(void* scanner, calc::location& loc);
}
%token <double> NUMBER "number"
%token PLUS "+" MINUS "-" STAR "*" SLASH "/"
%token LPAREN "(" RPAREN ")" EOL "end of line"
%nterm <double> 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';
}
54 changes: 54 additions & 0 deletions modern-cpp/test.py
Original file line number Diff line number Diff line change
@@ -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()
Loading