Questions tagged [instaparse]

Parser generator for context-free grammars in [tag:Clojure]. Supports EBNF and ABNF notation.

Instaparse aims to be the simplest way to build parsers in . (Source)

  • Turns standard EBNF or ABNF notation for context-free grammars into an executable parser that takes a string as an input and produces a parse tree for that string.
  • No Grammar Left Behind: Works for any context-free grammar, including left-recursive, right-recursive, and ambiguous grammars.
  • Extends the power of context-free grammars with PEG-like syntax for lookahead and negative lookahead.
  • Supports both of Clojure's most popular tree formats (hiccup and enlive) as output targets.
  • Detailed reporting of parse errors.
  • Optionally produces lazy sequence of all parses (especially useful for diagnosing and debugging ambiguous grammars).
  • "Total parsing" mode where leftover string is embedded in the parse tree.
  • Optional combinator library for building grammars programmatically.
  • Performant.
19 questions
9
votes
1 answer

How do we define a grammar for clojure code using instaparse?

I'm a newbie to parsing and wish to analyse some clojure code. I am hoping that someone can provide an example of how clojure code can be parsed using instaparse. I just need to do numbers, symbols, keywords, sexps, vectors and whitespace. Some…
zcaudate
  • 13,998
  • 7
  • 64
  • 124
6
votes
1 answer

How to test for texts not fitting an Instaparse-grammar (Clojure)?

I wrote a project for parsing strings using context-free grammar in Instaparse (Clojure). Now I'd like to test several input-Strings for their parsing results. Some input strings might not fit into the grammar. So far I only tested for "parsed…
Edward
  • 4,453
  • 8
  • 44
  • 82
5
votes
2 answers

Any way to speed up instaparse?

I'm trying to use instaparse on a dimacs file less than 700k in size, with the following grammar = clause+ comment=#'c.*' problem=#'p\s+cnf\s+\d+\s+\d+\s*' clause=literal* <'0'> =#'[1-9]\d*'|#'-\d+' calling like…
rwallace
  • 31,405
  • 40
  • 123
  • 242
5
votes
1 answer

How do you get instaparse to skip whitespace?

How do you get instaparse to skip whitespace between tokens? (I could of course define whitespace as a token in its own right and insert it between all the elements on the right hand side of every rule, but I'm dealing with a grammar that has over…
rwallace
  • 31,405
  • 40
  • 123
  • 242
3
votes
1 answer

Instaparse series of numbers or letters as one leaf?

So I've been messing around with instaparse and it's been great, however I've been trying to avoid using Regexes as a crutch and it has resulted in a bit more verbose. For the sake of keeping this readable let's just say #'[A-z]' is actually in the…
BWStearns
  • 2,567
  • 2
  • 19
  • 33
3
votes
1 answer

How to traverse parse tree from instaparse

I'm experimenting with Clojure and Instaparse. I have created a small toy language, and I'm getting stuck at how to properly treat the resulting tree. This is what i get: [:ClassDescription [:ClassName "Test"] [:Properties [:Property …
NiklasJ
  • 580
  • 2
  • 12
1
vote
0 answers

Trouble building clara rules dynamically using instaparse

I followed this example Insta Declarative DSL where we use Clara with instaparse to use a DSL and generate rules. everything works for me as expected but one issue. I am not able to access the variable binding from the condition expressing in lhs…
1
vote
3 answers

postwalk to evaluate arithmetic expression

I am trying to use Instaparse to make a simple arithmetic expression evaluator. The parser seems to work fine but I cannot figure out how to evaluate the returned nested vector. Currently I am using postwalk, like this (ns test5.core (:require…
Anonimista
  • 185
  • 1
  • 11
1
vote
2 answers

Easiest way to get an error message as a string in Instaparse?

Instaparse can pprint nice error messages to the REPL => (negative-lookahead-example "abaaaab") Parse error at line 1, column 1: abaaaab ^ Expected: NOT "ab" but I can not find a built-in function to get the message as a String. How to do that?
Aleksei Sotnikov
  • 633
  • 4
  • 15
1
vote
1 answer

How to remove ambiguity in EBNF Instaparse grammar

How can i prevent that the "," literal in the structure rule is parsed as a operator in the following EBNF grammar for Instaparse? Grammar: structure = atom <"("> term ("," term)* <")"> term = atom | number | structure | variable | "(" term ")" |…
Aruscher
  • 153
  • 1
  • 10
1
vote
2 answers

Instaparse ambiguity in Clojure

I've got a problem with an ambiguous parse in insta. Here's the grammar: (def yip-shape (insta/parser (str/join "\n" ["S = ( list-item | heading | text-block )*" ;; lists and that "list-item =…
Phil Jackson
  • 456
  • 3
  • 10
1
vote
1 answer

How to parse > character in Clojure Instaparse?

I am trying to parse the > character in Clojure Instaparse. I have tried |> and |\> but the parser doesn't seem to recognize any of these. Does anyone know the correct syntax?
yazz.com
  • 57,320
  • 66
  • 234
  • 385
1
vote
1 answer

Resolving ambiguity in simple Instaparse grammar

[Also posted on the Instaparse mailing list, but posted here as well since I'm guessing this is a fairly general problem] Consider the grammar D = (B|S)* S = 'S' B* B = 'B' (This is Instaparse's version of BNF...) B can occur by itself, or after…
JohnJ
  • 4,753
  • 2
  • 28
  • 40
0
votes
2 answers

Unbelievably bad parse time

I need to analyze Elisp (Emacs Lisp) code so I wrote a parser for it using Instaparse. I expected it to be slow but doing 1k lines per second is way too slow to be right even on a calculator (or my pretty old i7). Can it be that bad or do I do…
JAre
  • 4,666
  • 3
  • 27
  • 45
0
votes
1 answer

Grammar that allows arbitrary rules order

I'm (trying) to design a domain-specific language (I called it "Fahrenheit") for designing citation styles. A program written in Fahrenheit: MUST have exactly one citation block MAY have zero or more macro blocks. Here's a simplified yet valid…
customcommander
  • 17,580
  • 5
  • 58
  • 84
1
2