Skip to main content

No project description provided

Project description

rbnf-rts

Runtime support for generated parsers of RBNF.hs

More Examples

Check the test directory:

  • test
    • multiply : parser/lexer implementation for multiplications
    • arith : parser/lexer implementation for arithmetics
    • relax : parser/lexer implementation for a full-featured programming language
    • llvmir: parser/lexer implementation for LLVM IR, nearly full-featured

In each sub-directory of test, you can run tests via directly invoking the test.sh., like cd test/llvmir && bash test.sh

Native Dependencies

  • The Haskell Stack Toolchain

  • The executable rbnf-pgen in PATH.

    If ~/.local/bin is already in PATH:

    git clone https://github.com/thautwarm/RBNF.hs
    cd RBNF.hs
    stack install .
    

Example: Multiplications

  1. write a multiply.rbnf file:
# 'mul' is a python global which should be marked as 'required' in .rlex
Mul    : !lhs=Mul "*" !rhs=Atom            -> mul(lhs, rhs);
Mul    : !a=Atom                           -> a;
Atom   : "(" !a=Mul ")"                    -> a;

# 'unwrap' should be marked as 'required', just as 'mul'
Atom   : !a=<number>                       -> unwrap(a);
START ::= <BOF> !a=Mul <EOF>               -> a;
  1. write a multiply.rlex file:
%require mul
%require unwrap
%ignore space
number [+-]?\d+
space \s+
  1. codegen
sh> rbnf-pygen multiply.rbnf multiply.rlex multiply.py --k 1 --traceback
  1. run statically-generated parsers and lexers and enjoy its efficiency
from rbnf_rts.rts import Tokens, State
from multiply import run_lexer, mk_parser
import operator

def unwrap(x: Token):
    return int(x.value)

scope = dict(mul=operator.mul, unwrap=unwrap)

parse = mk_parser(**scope)


tokens = list(run_lexer("<current file>", "-1 * 2 * (3 * 4)"))
got = parse(State(), Tokens(tokens))
print(got)

Got (True, -24), where True indicates the parsing succeeded.

If False, a list of errors will be given in the second element of the return tuple.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

rbnf_rts-0.4-py3-none-any.whl (82.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page