aengelberg / instagenerate Goto Github PK
View Code? Open in Web Editor NEWReversing instaparse with core.logic
Reversing instaparse with core.logic
Hi Alex,
I was wonder if it is somehow possible to drive instagenerate in a probabilistic way.
Let me explain.
Let's suppose I got a grammar similar to:
A = B ('c3' | 'd4')
B = 'a1' | 'b2'
This will generate these 4 strings:
a1c3
b2c3
b2d4
a1d4
Now, for my goal, I know that is more likely that the string I am looking for start with a1
, so I want the generator to return first a1c3
and a1d4
and later b2c3
and b2d4
.
Is that possible ? How ?
Do you suggest some resource I can explore further ?
Thanks for your time and wonderful work :)
I've taken a few passes at implementing negative lookahead and thought I should report back on some of the difficulties I've had. My main focus has been modifying the code for positive lookahead to see if it would work for negative lookahead somehow. I was trying to model negative lookahead as the negation of positive lookahead and so I quickly got dragged into the nonrelational parts of core.logic. I'm no expert on either logic programming/constaint logic programming nor how instagenerate has been implemented so I'll try to keep from theorizing about the following:
Interesting definition for negative lookahead
(defmethod combinator-parseo :neg
[{comb :parser :keys [hide] :as combinator} grammar strings parse-tree remaining-strings remaining-parse-tree]
(let [comb (assoc comb :hide true)]
(fresh [fake-remaining-strings]
(!= strings remaining-strings)
(!= parse-tree remaining-parse-tree)
(partial-parseo comb grammar strings parse-tree fake-remaining-strings parse-tree))))
Simple grammar with interesting results:
(def grammar
(insta/parser
"S = !('B') ('A' | 'B' | 'C')"))
(run* [input]
(fresh [abc]
(instaparseo grammar input
[:S abc])))
;; (((B . _0) :- (!= (("A") (_1)))) ((B . _0) :- (!= (("C") (_1)))))
So in some weird sense instagenerate can do negative lookahead but you would have to carefully rip apart all the cons to get the correct results. It seems like if I understand how this all tied together it would be straightforward to pull these results out in the right way but so far it has escaped me.
In addition to this, I tried to project strings
and check that the negative lookahead's parsers didn't successfully parse the value of strings. I don't believe I ever got strings
to be ground though and so I nevere got anything but exceptions trying this. Also, I tried using nafc
but it created similiar output as above that was far more verbose while being effectively the same.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.