Pycarl parse

Number independent types

class CarlParserTransformer(package)
arith_nary(items)
arith_unary(items)
bool_value(items)
bool_variable(items)
carl_expr(items)
constraint(items)
form_expr(items)
formula_nary(items)
formula_unary(items)
number(items)
real_variable(items)
start(items)
class Lark(grammar: Grammar | str | IO[str], **options)

Main interface for the library.

It’s mostly a thin wrapper for the many different parsers, and for the tree constructor.

Parameters:

grammar: a string or file-object containing the grammar spec (using Lark’s ebnf syntax) options: a dictionary controlling various aspects of Lark.

Example:
>>> Lark(r'''start: "foo" ''')
Lark(...)

=== General Options ===

start

The start symbol. Either a string, or a list of strings for multiple possible starts (Default: “start”)

debug

Display debug information and extra warnings. Use only when debugging (Default: False) When used with Earley, it generates a forest graph as “sppf.png”, if ‘dot’ is installed.

strict

Throw an exception on any potential ambiguity, including shift/reduce conflicts, and regex collisions.

transformer

Applies the transformer to every parse tree (equivalent to applying it after the parse, but faster)

propagate_positions

Propagates positional attributes into the ‘meta’ attribute of all tree branches. Sets attributes: (line, column, end_line, end_column, start_pos, end_pos,

container_line, container_column, container_end_line, container_end_column)

Accepts False, True, or a callable, which will filter which nodes to ignore when propagating.

maybe_placeholders

When True, the [] operator returns None when not matched. When False, [] behaves like the ? operator, and returns no value at all. (default= True)

cache

Cache the results of the Lark grammar analysis, for x2 to x3 faster loading. LALR only for now.

  • When False, does nothing (default)

  • When True, caches to a temporary file in the local directory

  • When given a string, caches to the path pointed by the string

cache_grammar

For use with cache option. When True, the unanalyzed grammar is also included in the cache. Useful for classes that require the Lark.grammar to be present (e.g. Reconstructor). (default= False)

regex

When True, uses the regex module instead of the stdlib re.

g_regex_flags

Flags that are applied to all terminals (both regex and strings)

keep_all_tokens

Prevent the tree builder from automagically removing “punctuation” tokens (Default: False)

tree_class

Lark will produce trees comprised of instances of this class instead of the default lark.Tree.

=== Algorithm Options ===

parser

Decides which parser engine to use. Accepts “earley” or “lalr”. (Default: “earley”). (there is also a “cyk” option for legacy)

lexer

Decides whether or not to use a lexer stage

  • “auto” (default): Choose for me based on the parser

  • “basic”: Use a basic lexer

  • “contextual”: Stronger lexer (only works with parser=”lalr”)

  • “dynamic”: Flexible and powerful (only with parser=”earley”)

  • “dynamic_complete”: Same as dynamic, but tries every variation of tokenizing possible.

ambiguity

Decides how to handle ambiguity in the parse. Only relevant if parser=”earley”

  • “resolve”: The parser will automatically choose the simplest derivation (it chooses consistently: greedy for tokens, non-greedy for rules)

  • “explicit”: The parser will return all derivations wrapped in “_ambig” tree nodes (i.e. a forest).

  • “forest”: The parser will return the root of the shared packed parse forest.

=== Misc. / Domain Specific Options ===

postlex

Lexer post-processing (Default: None) Only works with the basic and contextual lexers.

priority

How priorities should be evaluated - “auto”, None, “normal”, “invert” (Default: “auto”)

lexer_callbacks

Dictionary of callbacks for the lexer. May alter tokens during lexing. Use with caution.

use_bytes

Accept an input of type bytes instead of str.

ordered_sets

Should Earley use ordered-sets to achieve stable output (~10% slower than regular sets. Default: True)

edit_terminals

A callback for editing the terminals before parse.

import_paths

A List of either paths or loader functions to specify from where grammars are imported

source_path

Override the source of from where the grammar was loaded. Useful for relative imports and unconventional grammar loading

=== End of Options ===

get_terminal(name: str) TerminalDef

Get information about a terminal

grammar: Grammar
lex(text: AnyStr | TextSlice[AnyStr], dont_ignore: bool = False) Iterator[Token]

Only lex (and postlex) the text, without parsing it. Only relevant when lexer=’basic’

When dont_ignore=True, the lexer will return all tokens, even those marked for %ignore.

Raises:

UnexpectedCharacters – In case the lexer cannot find a suitable match.

lexer: Lexer
classmethod load(f) _T

Loads an instance from the given file object

Useful for caching and multiprocessing.

classmethod open(grammar_filename: str, rel_to: str | None = None, **options) _T

Create an instance of Lark with the grammar given by its filename

If rel_to is provided, the function will find the grammar filename in relation to it.

Example:

>>> Lark.open("grammar_file.lark", rel_to=__file__, parser="lalr")
Lark(...)
classmethod open_from_package(package: str, grammar_path: str, search_paths: Sequence[str] = [''], **options) _T

Create an instance of Lark with the grammar loaded from within the package package. This allows grammar loading from zipapps.

Imports in the grammar will use the package and search_paths provided, through FromPackageLoader

Example:

Lark.open_from_package(__name__, “example.lark”, (“grammars”,), parser=…)

options: LarkOptions
parse(text: AnyStr | TextSlice | Any, start: str | None = None, on_error: Callable[[UnexpectedInput], bool] | None = None) ParseTree

Parse the given text, according to the options provided.

Parameters:
text (LarkInput): Text to be parsed, as str or bytes.

TextSlice may also be used, but only when lexer=’basic’ or ‘contextual’. If Lark was created with a custom lexer, this may be an object of any type.

start (str, optional): Required if Lark was given multiple possible start symbols (using the start option). on_error (function, optional): if provided, will be called on UnexpectedInput error,

with the exception as its argument. Return true to resume parsing, or false to raise the exception. LALR only. See examples/advanced/error_handling.py for an example of how to use on_error.

Returns:

If a transformer is supplied to __init__, returns whatever is the result of the transformation. Otherwise, returns a Tree instance.

Raises:

UnexpectedInput – On a parse error, one of these sub-exceptions will rise: UnexpectedCharacters, UnexpectedToken, or UnexpectedEOF. For convenience, these sub-exceptions also inherit from ParserError and LexerError.

parse_interactive(text: AnyStr | TextSlice | Any | None = None, start: str | None = None) InteractiveParser

Start an interactive parsing session. Only works when parser=’lalr’.

Parameters:

text (LarkInput, optional): Text to be parsed. Required for resume_parse(). start (str, optional): Start symbol

Returns:

A new InteractiveParser instance.

See Also: Lark.parse()

parser: ParsingFrontend
save(f, exclude_options: Collection[str] = ()) None

Saves the instance into the given file object

Useful for caching and multiprocessing.

source_grammar: str
source_path: str
terminals: Collection[TerminalDef]
exception ParserError(message)
exception UnexpectedInput

UnexpectedInput Error.

Used as a base class for the following exceptions:

  • UnexpectedCharacters: The lexer encountered an unexpected string

  • UnexpectedToken: The parser received an unexpected token

  • UnexpectedEOF: The parser expected a token, but the input ended

After catching one of these exceptions, you may call the following helper methods to create a nicer error message.

column: int
get_context(text: str, span: int = 40) str

Returns a pretty string pinpointing the error in the text, with span amount of context characters around it.

Note:

The parser doesn’t hold a copy of the text it has to parse, so you have to provide it again

interactive_parser: InteractiveParser
line: int
match_examples(parse_fn: Callable[[str], Tree], examples: Mapping[T, Iterable[str]] | Iterable[Tuple[T, Iterable[str]]], token_type_match_fallback: bool = False, use_accepts: bool = True) T | None

Allows you to detect what’s wrong in the input text by matching against example errors.

Given a parser instance and a dictionary mapping some label with some malformed syntax examples, it’ll return the label for the example that bests matches the current error. The function will iterate the dictionary until it finds a matching error, and return the corresponding value.

For an example usage, see examples/error_reporting_lalr.py

Parameters:

parse_fn: parse function (usually lark_instance.parse) examples: dictionary of {'example_string': value}. use_accepts: Recommended to keep this as use_accepts=True.

pos_in_stream = None
state: Any
deserialize(text, package)

Number dependent types (gmp)

Number dependent types (cln)