lrparser

A SLR parser written in Nim with compile-time and run-time grammar generation.

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version Unknown
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-07-21 05:23

Installation

nimble install lrparser
choosenim install lrparser
git clone https://github.com/vanyle/lrparser/

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
lrparser - - - - - - -

Source

Repository https://github.com/vanyle/lrparser/
Homepage https://github.com/vanyle/lrparser/
Registry Source nimble_official

README

LRparser

Documentation Generation Tests

This nim package implements an SLR parser, an algorithm that allows you to parse string to form trees. Such a parser allows you to parse programming languages for example. For such an example, see tests/vlang.nim

More information about SLR parsers here: https://en.wikipedia.org/wiki/LR_parser

Usage

let input_string = """
        4 * 41 + 3 * (7 + 2)
    """
# The ruleset
let grammar = @[
    ("S", @["E"]),
    ("E", @["E", "+", "T"]),
    ("E", @["T"]),
    ("T", @["T", "*", "F"]),
    ("T", @["F"]),
    ("F", @["(", "E", ")"]),
    ("F", @["LIT"]),
]

# We need to convert the strings to ids so that the parser understands it
let tokenIdTable = makeTokenIdTable(grammar) # returns a Table[string, int]
let int_grammar = convertGrammar(grammar, tokenIdTable)

proc labeler(s: string): int =
    # Function that will detect the types of the tokens
    if s[0] == '*': return tokenIdTable["*"]
    if s[0] == '+': return tokenIdTable["+"]
    if s[0] == '(': return tokenIdTable["("]
    if s[0] == ')': return tokenIdTable[")"]
    return tokenIdTable["LIT"]

# Build the grammar to use it. This might take time so consider doing this at compile time for maximum performance.
let full_grammar = constructGrammar(int_grammar)

# We convert the string into "tokens" (i.e a seq of strings with some metadata)
let tokens = tokenize(input_string, @["*", "+", "(", ")"], @[" ", "\t",
        "\n"], labeler)

# We parse the string
let ast = parse(tokens, full_grammar)

echo ast # We obtain a tree like data structure !

Even more examples in tests. Complete documentation available here

Contributing

Feel free to implement a LALR parser.