Skip to content

leissa/fe

Repository files navigation

FE

Linux Windows macOS Doxygen License: MIT

[TOC]

FE is a header-only C++20 toolkit for writing hand-rolled compiler and interpreter frontends. It does not generate lexers or parsers for you; instead, it gives you the reusable pieces that make handwritten frontends pleasant to build and maintain.

💡 Why FE?

FE is built around a few small components that compose well:

  • fe::Arena for arena allocation and arena-backed ownership.
  • fe::Sym and fe::SymPool for string interning and cheap identifier comparison.
  • fe::Driver for diagnostics and shared frontend state.
  • fe::Pos and fe::Loc for source positions and spans.
  • fe::Lexer<K, S> for UTF-8-aware lexing with lookahead and token text accumulation.
  • fe::Parser<Tok, Tag, K, S> for recursive-descent style parsing with token lookahead and span tracking.
  • Optional FE_ABSL support for Abseil hash containers.

The best end-to-end example in this repository is tests/lexer.cpp.

🚀 Quick start

CMake

Add FE as a subdirectory and link the fe interface target:

add_subdirectory(external/fe)
target_link_libraries(my_compiler PRIVATE fe)

If you want Abseil-backed hash containers, enable FE_ABSL before adding the subdirectory:

set(FE_ABSL ON)
add_subdirectory(external/fe)
target_link_libraries(my_compiler PRIVATE fe)

Direct include

Since FE is header-only, you can also vendor include/fe/ directly into your project and add -DFE_ABSL if you want Abseil support.

🧭 Typical workflow

  1. Define a token type that exposes tag() and loc().
  2. Derive your lexer from fe::Lexer<K, S>.
  3. Derive your parser from fe::Parser<Tok, Tag, K, S>.
  4. Use fe::Driver for diagnostics and identifier interning.
  5. Thread fe::Loc through tokens and AST nodes for precise error reporting.

If you want a concrete model to copy from, start with tests/lexer.cpp.

🛠️ Building and testing FE itself

cmake -S . -B build -DBUILD_TESTING=ON
cmake --build build
ctest --test-dir build --output-on-failure

Run one discovered test:

ctest --test-dir build -R '^Lexer$' --output-on-failure

Or run a doctest case directly:

./build/bin/fe-test --test-case=Lexer

📚 Building the documentation

cmake -S . -B build -DFE_BUILD_DOCS=ON
cmake --build build --target docs

This requires Doxygen and Graphviz (dot).

🔨 Related projects

  • Let - a small demo language built on FE.
  • GraphTool - a DOT-language tool using FE-style frontend infrastructure.
  • MimIR - an intermediate representation project by the author.
  • SQL - a small SQL parser.

⚖️ License

FE is licensed under the MIT License.

About

A header-only C++ library for writing compiler/interpreter frontends.

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors