LR-demo: LALR(1) parsetable generator and interpreter

[ bsd3, library, parsing, program ] [ Propose Tags ] [ Report a vulnerability ]

An LALR(1) parsetable generator and interpreter.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.0.20251105
Change log CHANGELOG.md
Dependencies array, base (>=4.11 && <5), containers, LR-demo, microlens, microlens-th, mtl, string-qq, transformers [details]
Tested with ghc ==9.12.2, ghc ==9.10.3, ghc ==9.8.4, ghc ==9.6.7, ghc ==9.4.8, ghc ==9.2.8, ghc ==9.0.2, ghc ==8.10.7, ghc ==8.8.4, ghc ==8.6.5, ghc ==8.4.4
License BSD-3-Clause
Author Andreas Abel <andreas.abel@gu.se>
Maintainer Andreas Abel <andreas.abel@gu.se>
Category Parsing
Source repo head: git clone https://github.com/teach-plt/LR-demo.git
Uploaded by AndreasAbel at 2025-11-05T14:56:38Z
Distributions
Executables lr-demo
Downloads 2 total (2 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2025-11-05 [all 1 reports]

Readme for LR-demo-0.0.20251105

[back to package description]

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.