LR-demo

LALR(1) parsetable generator and interpreter

Stackage Nightly 2025-12-15:0.0.20251105
Latest on Hackage:0.0.20251105

See all snapshots LR-demo appears in

BSD-3-Clause licensed and maintained by Andreas Abel
This version can be pinned in stack with:LR-demo-0.0.20251105@sha256:faabd406adde2cce347aa39cd7ab895fb602049d4a4e93a5dbbd5a9d78fcac96,2336

A parser for LALR(1) grammars in Haskell

Usage:

lr-demo MyGrammar.cf < MyInput.txt

Prints the generated LALR(1) parse table for context-free grammar MyGrammar.cf and a trace of shift and reduce actions of the parser when accepting MyInput.txt.

The input .cf file consists of labelled BNF rules (LBNF) of the form:

LABEL "." NONTERMINAL "::=" (NONTERMINAL | TERMINAL)* ";"

For example:

Par.  S ::= "(" S ")" S ;
Done. S ::= ;

This format is compatible with BNFC (but BNFC pragmas are not recognized).

Limitation: terminals can only be single characters.

Getting started

  1. Have Haskell Stack installed.
  2. Clone and enter this repository.
  3. Execute: stack run lr-demo -- test/BalancedParentheses.cf < test/BalPar.txt
Using the following grammar:

Done . S ::=;
Par . S ::= "(" S ")" S;

Generated parse table:

State 0

	eof	reduce with rule Done
	'('	shift to state 1

	S 	goto state 2

State 1

	'('	shift to state 1
	')'	reduce with rule Done

	S 	goto state 3

State 2

	eof	reduce with rule %start

State 3

	')'	shift to state 4

State 4

	eof	reduce with rule Done
	'('	shift to state 1
	')'	reduce with rule Done

	S 	goto state 5

State 5

	eof	reduce with rule Par
	')'	reduce with rule Par


Parsing stdin...
            . '(' ')'  -- shift
'('         . ')'      -- reduce with rule Done
'(' S       . ')'      -- shift
'(' S ')'   .          -- reduce with rule Done
'(' S ')' S .          -- reduce with rule Par
S           .          -- halt

Reference

stack run -- --help

License

BSD 3-clause.

Changes

0.0.20251105

  • Initial release.
  • Tested with GHC 8.4.4 - 9.12.2.