• Stars
    star
    188
  • Rank 205,563 (Top 5 %)
  • Language
    Shell
  • License
    Other
  • Created almost 12 years ago
  • Updated about 8 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Flex Bison C++ Template/Example
                       *** Flex Bison C++ Example ***

Author: Timo Bingmann (Mail: tb a-with-circle idlebox dot net)
Date: 2007-08-20

*** Summary ***

This example shows how to use both Flex and Bison in C++ mode. This way both
lexer and parser code and data is encapsulated into classes. Thus the lexer and
parser are fully re-entrant, because all state variables are contained in the
class objects. Furthermore multiple different lexer-parser pairs can easily be
linked into one binary, because they have different class names and/or are
located in a different namespace.

*** Website / License ***

The current example package can be downloaded from
http://idlebox.net/2007/flex-bison-cpp-example/

The following just mean you can copy the example code into your program or use
it for whatever purpose without crediting me (though I would really like it if
you did):

The parts of the example code written by myself are released into the public
domain or, at your option, under the Do What The Fuck You Want To Public
License (WTFPL), which can be found in the file COPYING. There are some special
GPL license exceptions for the included source files from the Bison and Flex
distributions.

The idea and method of this example is based on code from
http://ioctl.org/jan/bison/

*** Why Use These Old Tools? ***

Well, they are here to stay and they work well. These days there are much more
sophisticated C++ parser generation frameworks around:

  * Most well-known is the Boost.Spirit parser framework
  * and the ANTLR parser generator.
  * Less well known is the Common Text Transformation Library.

All these libraries do good jobs when you need to generate parsers for more
difficult grammars.

However if you write a program with one of the frameworks above, then your
users need that parser framework installed to compile your program. But Flex
and Bison require no compile-time dependencies, because they generate fully
autonomous source code. (And Flex and Bison are installed almost everywhere.) 
So far I have not found any modern parser generator which outputs independent
code.

It is even possible to compile the generated source with Visual C++ on Windows
(worked with 8.0 aka 2005). Flex and Bison need not be installed on the windows
machine. The source package includes a VC++ solution and two project files.

*** Source and Generated Files ***

The src directory contains the following source files. Note that some of them
are automatically generated from others.

  * scanner.ll contains the Flex source for the C++ lexical scanner.
  * scanner.cc is generated from scanner.ll by Flex.
  * scanner.h defines the lexer class example::Scanner.
  * FlexLexer.h copied from Flex distribution. Defines the abstract lexer
    class.

  * parser.yy is the example Bison parser grammar.
  * parser.cc generated from parser.yy by Bison.
  * parser.h generated from parser.yy by Bison.
  * y.tab.h contains nothing. Just forwards to parser.h

  * location.hh installed by Bison. Contains something required by the
    parser class.
  * position.hh same.
  * stack.hh same.

  * driver.h defines the example::Driver class, which puts together lexer
    and parser.
  * driver.cc implementation for driver.h

  * expression.h defines the example's calculator node classes.
  * exprtest.cc contains a main function to run the example calculator.

  * readme.dox doxygen explanation text, which you are reading right now.

So if you wish to create a program using a C++ Flex lexer and Bison parser,
you need to copy the following files:

  * scanner.ll, scanner.h, FlexLexer.h
  * parser.yy, y.tab.h
  * location.hh, position.hh, stack.hh (are created by Bison when run on
    the grammar)
  * driver.h, driver.cc

--- Namespace and Library ---

The scanner, parser and driver classes are located within the example
namespace. When coding a larger program, I believe it is most convenient to put
all scanner/parser source files into a separate directory and build a static
library. Then all parts of the parser are located in a separate namespace and
directory.

*** Code Overview ***

This is a brief overview of the code's structure. Further detailed information
is contained in the doxygen documentation, comments in the source and
ultimately in the code itself.

--- Scanner ---

The input stream is first converted by the lexical scanner into tokens. The
scanner is defined by the list of regular expressions in scanner.ll . From this
file Flex generates the file scanner.cc, which mainly contains a function
called yylex(). This function returns the next token for the parser. For a C++
scanner the yylex() function is contained in a class, which is named
yyFlexLexer by default. It is declared in the FlexLexer.h and is derived from
the abstract FlexLexer class.

By defining the macro yyFlexLexer => ExampleFlexLexer in scanner.h, the default
name of the scanner class is changed. Furthermore to extend yylex()'s parameter
list, the class example::Scanner is derived from the ExampleFlexLexer class. It
is mainly a forwarding class. By defining the macro YY_DECL, the yylex()
function generated by Flex is renamed to example::Scanner::lex().

Another change to the default Flex code is that the token type is changed from
int to the enum example::Parser::token defined by parser.

--- Parser ---

Bison's support for C++ is much more sophisticated. In C++ mode it generates a
class named example::Parser, which is located in parser.cc and declared in
parser.h . The header file also defines the scanner tokens, which must be
returned by the Flex scanner's regular expression rules. Bison's C++ skeleton
also installs the three .hh files, which contain utility classes required by
the parser.

In the example calculator the Bison code constructs a calculation node
tree. The tree's nodes are derived from CalcNode and are evaluated to output
the parsed expression's result.

--- Driver ---

The example::Driver class brings the two components scanner and parser classes
together. It is the context parameter of the parser class. The hook between
scanner object and parser object is done by defining the yylex() macro to be
"driver.lexer->lex". This way the Bison parser requests the next token from the
scanner object contained within the driver.

The example::Driver object can be accessed by the Bison actions. Therefore it
will contain a reference to the data classes filled by the parser's rules. In
the example it contains a reference to the CalcContext. Thus a refernce to a
CalcContext must be given to the constructor of example::Driver. This
CalcContext object will be filled with the parsed data.

To initiate parsing the example::Driver class contains the three functions
example::Driver::parse_stream(), example::Driver::parse_file() and
example::Driver::parse_string().

*** Example Calculator ***

The example lexer and grammar is a simple floating point arithmetic
calculator. It follows the usual operator precedence rules (sometimes called
BODMAS or PEMDAS): Parentheses, Exponentation, Multiplication/Division,
Addition/Subtraction.

Besides these simple arithmetic operators, the program also supports
variables. These can be assigned a value and used in subsequent expressions.

It can be started interactively and will process expressions entered on the
console. The expression's parse tree is printed and then evaluated. Here some
examples:

--- snip ---
$ ./exprtest
Reading expressions from stdin
input: 4 * 1.5 + 3 * (2 ^ 4 - 4)
tree:
+ add
  * multiply
    4
    1.5
  * multiply
    3
    - subtract
      ^ power
        2
        4
      4
evaluated: 42
input: v = (2 ^ 4 - 4)
Setting variable v = 12
input: 3.5 * a
input:1.6: Unknown variable "a"
input: 3.5 * v
tree:
* multiply
  3.5
  12
evaluated: 42
input: 5 + * 6
input:1.4: syntax error, unexpected '*'
input: 5 + (4 * 4
input:1.10-9: syntax error, unexpected end of file, expecting ')'
--- snap ---

The exprtest can also be used to process text files containing
expressions. Within the file each line is parsed as an expression. Multiple
expressions can be put into one line by terminating them with a semicolon
';'. The exprtest outputs a parse tree for each parsed non-assignment line.

--- snip ---

v = (2 ^ 4 - 4); e = 2.71828

4 * 1.5 + 3 * v

6 * (2 * 2) ^ 2 / 2

--- snap ---

The above example file (included as exprtest.txt) can be processed by calling
./exprtest exprtest.txt. The program outputs the following evaluation:

--- snip ---
Setting variable v = 12
Setting variable e = 2.71828
Expressions:
[0]:
tree:
+ add
  * multiply
    4
    1.5
  * multiply
    3
    12
evaluated: 42
[1]:
tree:
/ divide
  * multiply
    6
    ^ power
      * multiply
        2
        2
      2
  2
evaluated: 48
--- snap ---

More Repositories

1

sound-of-sorting

The Sound of Sorting - "Audibilization" and Visualization of Sorting Algorithms
C++
836
star
2

stx-btree

OBSOLETE, contained in https://github.com/tlx/tlx - STX B+ Tree C++ Template Classes -
HTML
212
star
3

pmbw

Parallel Memory Bandwidth Measurement / Benchmark Tool
C++
103
star
4

cobs

COBS - Compact Bit-Sliced Signature Index (for Genomic k-Mer Data or q-Grams)
C++
83
star
5

malloc_count

Method to measure the amount of allocated memory of a program at run-time.
C
65
star
6

cryptote

CryptoTE - Text Editor with Transparent Encryption
C++
44
star
7

parallel-string-sorting

Collection of Parallel String Sorting Algorithms including Parallel Super Scalar String Sample Sort and Parallel Multiway LCP-Mergesort
C++
32
star
8

disk-filltest

Simple program to detect bad disks by filling them with random data.
C
30
star
9

2018-cpp-spirit-parsing

Examples for a Meetup Talk on Parsing Structured Text with Boost Spirit in C++
C++
28
star
10

sqlplot-tools

Generate pgfplots or gnuplots from embedded SQL statements
C++
24
star
11

eSAIS

eSAIS and DC3 with LCP construction
C++
11
star
12

BlinkenAlgorithms

Sorting Algorithms and Other Animations for Neopixel / WS8212B / SK6812 / APA102 LEDs on ESP8266, Teensy, or Raspberry Pi
C++
8
star
13

yed2tikz

yed2tikz or yworks2pgf Translator using XSLT
XSLT
7
star
14

vncrec

Collection of vncrec versions, original, with patches and based on tightvnc
C
7
star
15

dot-emacs

My emacs configuration. It contains many things collected over the years, and is not very tidy. Copy at will.
Emacs Lisp
7
star
16

evdev-keylogger

Simple keylogger for Linux that uses evdev.
C
5
star
17

stx-execpipe

STX Execution Pipe C++ Library
C++
5
star
18

stx-cbtreedb

STX Constant B-Tree Database Template Classes
C++
5
star
19

stx-exparser

STX Expression Parser C++ Framework
Shell
4
star
20

digup

digup - A Digest Updating Tool
C
4
star
21

bingmann.github.io

Github Pages Version of My Homepage
HTML
4
star
22

bachelorarbeit-vorlage

LaTeX-Vorlage für Bachelor- und Masterarbeiten
4
star
23

pDCX

Parallel Suffix Sorting with DC3/skew3/DC7/DC13 using MPI
C++
2
star
24

microbenchmarks

Microbenchmarks of Various Basic Data Structures and Algorithms with perf Events
C++
2
star
25

cobs-experiments

Experiment Scripts for COBS, BIGSI, Mantis, and various SBTs
Shell
2
star
26

2017-cpp-goodies

Source code examples used in C++ Meetup Talk
C++
2
star
27

tilewm

Prototype of new C++ tiling window manager for X11/XCB
C++
1
star
28

dot-awesome

My AwesomeWM config
Lua
1
star