Code Monkey home page Code Monkey logo

flub's Introduction

flub

A simple demo of integrating Flex and Bison C++ modes in 2022

Organization

scanner.ll: The scanner (aka lexer) is responsible for turning text into tokens (.l for lex files, .ll for flex files)

lexer.y: The lex-style bnf definition of the grammar with parsing actions (.y for 'yacc' on which 'bison' is based)

driver.cpp: A class which drives the parser by passing it an input source, and which is made available as a context from the parser for ast building etc.

'.cc' and '.hh' files: I use this suffix to distinguish between written and generated files.

Building

Requires a fairly new flex/bison pair, so you may want to use a docker image:

Lin/Mac/WSL:

Assuming you have flex and bison installed (apt/yum/brew/port install) along with cmake, then from the top the repository:

# Configure the project for building, creating a 'build-dir/' folder for output
cmake -B build-dir -S .   # -DCMAKE=FLUB_ADDRESS_SANITIZE
cmake --build build-dir
./build-dir/example_parser

Windows:

I found installing flex and bison on Windows a royal pain. If you can install them, then the instructions above will work fine. But most likely you will want to take the Docker approach below.

Docker

Note Bien The provided Dockerfile.example is for running the flex and bison tools alone. To keep it small, I decided not to include compiler tools. This makes it possible to build on Windows without having to install those tools.

I've provided a Dockerfile.example which hosts flex and bison sufficient to perform the code-gen required to generate the .cc and .hh files needed to build the example.

By default, this will use kfsone/flexbison, which was built using the provided Dockerfile.example.

Use the kfsone/flexbison image:

cmake -S . -B build-dir -DFLUB_DOCKERIZED=ON
cmake --build build-dir
./build-dir/example_parser  # or ./build-dir/Debug/example_parser

Build your own image:

docker-build --tag flexbison -f Dockerfile.example .  # add --build-arg APT_PROXY=http://../ if you have an apt cache
docker run --rm -it flexbison
cmake -S . -B build-dir -DFLUB_DOCKERIZED=ON -DFLUB_DOCKER_IMAGE=flexbison
cmake --build build-dir
./build-dir/example_parser

Purpose

This was part of an attempt to piece together a working, pure-C++, no-globals Flex/Bison based parser from a number of different sources:

Bison's documentation This assumes a pure-C scanner.

Learn Modern CPPs tutorial A C++ flex scanner but no locations.

Jonathan Bear's tutorial Couldn't get this to work - although it may have been a combination of a typo and the lack of an explicit yyFlexLexer::yylex() { ... } override.

Grammar

It's a very simple grammar, # for comments, and the rest is either include 'filename-in-single-quotes' or ignore 'filename', from the 'example.txt' file.

flub's People

Contributors

kfsone avatar

Stargazers

josema avatar Albert avatar

Watchers

 avatar

flub's Issues

`setBuffer` can cause memory corruption, use Flex's `switch_streams` instead

I was building on this project and for some very specific input files, the scanner would randomly crash (see my StackOverflow post).

I figured out that the problem stems from setBuffer, which can corrupt memory in very rare cases. Whenever I used address sanitizer (-fsanitize=address), there were issues with buffer overreads as well.

The flex docs mention the method switch_stream, which can be used as a drop-in replacement for setBuffer:

int Driver::parse(const char* filename)
{

    std::ifstream file(filename);
    if (!file)
    {
        std::cerr << "Failed to open file: " << filename << '\n';
        return -1;
    }

    Lexer scanner{};
    scanner.switch_streams(&file, nullptr);

    yy::Parser parse(scanner, *this);
    int res = parse();

    return res;
}

If needed, I can create a pull request to update the repo.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.