A small Haskell toolchain that drives GNU Bison to produce LR(1) parse tables and a runnable pushdown automaton (PDA) parser for very small grammars. It generates a Bison grammar from a compact custom format, runs Bison with XML reports, parses the report, and exposes the automaton & tables in Haskell for interactive parsing.
- Convert a simple plaintext grammar description into a Bison
.yfile. - Run
bison --xmland parse the XML report to extract LR(1) states, transitions and reductions. - Build internal action/goto tables and run a stack-based LR(1) parser in a REPL.
- Normalizes token names between Bison and single-character terminals used in the input grammar.
- Generates intermediate code (three-address code) for parsed input strings.
- GHC (8.x or later)
- Cabal or Stack (optional; you can also
ghc --make) - GNU Bison (with
--xmlreport support) - Haskell packages: text, containers, xml-conduit (used by Text.XML)
- exe/Main.hs — main program: reads
grammar.txt, generatesgrammar.y, runs Bison, builds automaton and starts a REPL parser. - exe/GenBison.hs — generates Bison
.yfrom compact productions. - exe/BisonXML.hs — parses Bison XML report into Haskell data structures.
- grammar.txt — example grammar input format (see below).
- table.rep / grammar.xml — example Bison output produced in a run.
Plain text file with:
- Non-terminals line (space separated single characters)
- Terminals line (space separated single characters)
- Start symbol (single character)
- Blank line
- Productions, one per line as
LHS RHSwhere LHS and RHS are sequences of characters (LHS typically single character). Example:
E T F
a + - * / ( )
E
E E+T
E T
T T*F
T F
F (E)
F a
The tool maps terminals to Bison token names and emits a .y file.
- Place your grammar in
grammar.txt. - Build and run the executable:
- with GHC: ghc -o bison_parser exe/Main.hs exe/GenBison.hs exe/BisonXML.hs && ./bison_parser
- or with Stack/Cabal if you create a project file.
- The program will:
- create
grammar.y - run Bison to produce
grammar.xmlandtable.rep - parse the report and print states and tables
- start a REPL to parse input strings interactively and output intermediate code.
- create
- The tool assumes very small, single-character tokens and nonterminals. Token name mapping for common symbols is built-in (e.g.
+→PLUS). - Bison must be available in PATH. The program invokes bison to create the XML report.
- Main.hs — orchestration, I/O, parsing REPL
- GenBison.hs — grammar → Bison generator
- BisonXML.hs — XML → internal representation
- grammar.txt — example grammar used during development
Designed as a university project for learning about parser generators and LR parsing.
