Even though Pysec (and functional programming in python in general) turned out to be 2-3x slower, monadic parsing is still great for certain tasks. After I wrote about Pysec the first time, I received comments regarding pyparsing, which is a great parsing library that looks very similar. Today, I'd like to show you a very simple example where monadic parsing really shines above all other parsing techniques, including pyparsing. This is a real-world need that I had, not just an academic thought-experiment.
The situation is that I'm writing a peer-to-peer file synchronizer and I need to serialize binary data in an efficient way. I've choosen to use what I call "sized binary", which is basically netstrings without the comma or bencode's byte string. The byte-string "ABC", for example, would be encoded as "3:ABC", or "Hello World!" would be "12:Hello World!". This is very efficient because "ABC" or "Hello World!" can be any bytes \x00-\xFF, without any sort of encoding or decoding like uuencode.
Stop and think for a minute how you would define a grammer to parse "3:ABC". It's so simple, right? Well, try and write a grammar for it. Try using BNF, EBNF, yacc/bison, pyparsing, SimpleParse, or any parsing libarary you know of, in any programming language. If you manage, please write me an email and let me know how you did it, because I'm not so sure it's even possible. Maybe it is in the Perl6 grammars; they're throwing the kitchen sink into that thing :). The best I've seen is that the creator of pyparsing hacked a grammar object to change how it parses the "ABC" part after a different object parsed the "3" part. This basically uses the grammar object as a global variable. It works in a single-core world, but it won't work in the many-core world we're headed for.
But now you're getting bored with me because it's silly ringing my hands over such a simple format. I bet you could parse this format in just 4 lines of code:
def parse_sized_binary(stream): size_bytes, stream = stream.readUntil(":") size = int(size_bytes) bytes, stream = stream.read(size) return bytes, streamYou're right, it's really simple, at least until you have to embed this little "sized binary language" inside of a bigger language. Perhaps, like me, you need to embed binary data inside of a bigger data structure, and so you need to define a serialization for things like ints, floats, lists, and hash tables. For that stuff, you can define a grammer for a language like JSON or YAML and hand it over to a grammar-based parser library. But now you need a way to tell the library, "OK, when you see my binary data, hand control over to my four lines of code, and then I'll hand control back". You might even say "You know, why don't you just let me hand you any function of the form stream -> (value, stream) and let me control the parsing for a while".
Congratulations, you just invented monadic parsing. The function you wrote, parse_sized_binary, of the form stream -> (bytes, stream), is a Parser Monad. Monadic parsing is just combining lots of these little parsers together. And so, it's incredibly easy to insert arbitrary parsing code, like parse_sized_binary, into any parser.
As a complete example, using Pysec, here's a full parser for a language just like bencode but with support for floats and unicode. Notice that our parse_size_binary is renamed to bencode_sized and is given a more functional definition.
from Pysec import Stub, until, lift, read, many_until, branch bencode_any = Stub() bencode_sized = until(":") >> lift(int) >> read bencode_unsized = until("e") bencode_list = many_until(bencode_any, "e") bencode_any.set(branch({"s" : bencode_sized, "u" : bencode_sized >> lift(lambda bytes : bytes.decode("utf8")), "i" : bencode_unsized >> lift(int), "f" : bencode_unsized >> lift(float), "l" : bencode_list, "d" : bencode_list >> lift(dict)}))I think this is sort of like Greenspun's Tenth Rule. Any implementation of netstrings/sized-binary-data probably has an ad-hoc implementation of monadic parsing in it. Even my own non-monadic version of this parser (which I use for performance reasons) has an ad-hoc implementation of monadic parsing. I'm convinced that if you write a parser for a similar format, you'll implement/invent monadic parsing as well, even if you don't know it.
Using this bencode example and the javascript example, combined with the rest of the source you posted earlier (the Pysec/Parsec in Python post), I tried to create something that can parse something that could otherwise be decoded as follows:
ReplyDeletedef tlv(data):
# data is a string for this example
import struct
# two shorts in network byte order
type, len = struct.unpack('!HH', data[:4])
value, data = data[4:len+4], data[len+4:]
return (type,value), data
I succeeded in using your monad classes and parser decorator to create something to read a short, but passing the length into the next parser (I tried to use read, like in the bencode example) didn't work. It seems likely that the source provided for the javascript example isn't exactly what it is inside the Pysec module you import from in the bencode example.
Any tips?
Pysec has changed a bit since I posted about it the first time, mostly for performance reasons. If you'd like, I can post somewhere a more complete example that you can actually run. I'm never sure how popular a post will be, so I don't always go to the trouble to make a full, runnable example.
ReplyDeleteThat's understandable. Posting all the code over and over again when the changes are minor could clutter up the posts pretty quickly. I don't know what your plans are for releasing Pysec are but I was wondering, is the code publically accessible anywhere? Those of us keeping up with your blog can keep up with the code too =)
ReplyDeleteSorry about not getting to you sooner.
ReplyDeleteMy code isn't anywhere publicly, other than this blog. Do you know of a good place to host code informally? I guess Google Code looks pretty good, but what do I call the project? "Peter's Stuff"? I might want to release thing other than Pysec.
Also, I find it hard enough making regular blog entries. I'm not sure how much time I'd have to dedicate to any kind of "project" right now. But perhaps if there's sufficient interest for it, I could find a little time.
Well, a formal "project" isn't really necessary, I was thinking just a pub directory somewhere ;-)
ReplyDeleteOr, if you have a revision control system and a webhost, you could have a source browser (such as trac, or viewcvs).
Just a suggestion, though. Thanks for the full source on the most recent post, by the way =)
Now that you have discovered github, please post post your work on this parser there. I love the simplicity and ease for defining a grammar.
ReplyDeleteBencode isn’t context-free, so of course this isn’t possible. One of the reasons I think it’s a terrible format. If you restrict the length of Bytestrings you could probably describe it with a gigantic grammar, but I didn’t feel like figuring it out.
ReplyDeleteThere's also an issue with the parser you posted; viz. it allows you to have things like this 00000000000000000005:hello which would be bad. :-) I found a lot of buffer overflows this way... most people aren't subtle enough in writing these parsers. I ended up writing one by hand for Bencode, but it took a long time. — What a total crap format.
ReplyDelete
ReplyDeleteGood post it's amazing the knowledge you have in your Niche
xiaomi so popular
In my opinion, on https://stjosephpublicschoolsfoundation.org/essay-writing-rubrics-for-teachers/ you can get useful essay writing rubrics for teachers. I had such experience and it was great idea
ReplyDeleteThis is my first time i visit here. I found so many entertaining stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the good work. I have been meaning to write something like this on my website and you have given me an idea.
ReplyDeletedata science course in India
I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!
ReplyDeleteArtificial Intelligence Course
Thanks for sharing this informative content.,
ReplyDeleteLeanpitch provides online training in Scrum Master Certification during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.
Scrum master certification online
CSM certification online
CSM online
CSM online certification
Thanks for sharing this informative content.,
ReplyDeleteLeanpitch provides online training in Scrum Master Certification during this lockdown period everyone can use it wisely.
Join Leanpitch 2 Days CSM Certification Workshop in different cities.
Scrum master certification online
CSM online training
CSM training online
Scrum master training online
I truly like your style of blogging. I added it to my preferred's blog webpage list and will return soon…
ReplyDeleteClick Here
Jackets on Aspire Leather
If your looking for Online Illinois license plate sticker renewals then you have need to come to the right place.We offer the fastest Illinois license plate sticker renewals in the state.Please Visit:
ReplyDeleteJackets on Genuine Shopping Store
This is how we do it! I know that sometime we all necessity assistance with essay nad you can be the best and first who learn! Check this help writing dissertation proposal and go for it! Have a kind day my friend!
ReplyDeleteIt's very simple to find out any topic on net as compared to books, as I found this post at this site.
ReplyDelete온라인카지노
온라인카지노사이트
카지노
Hi there, I enjoy reading all of your post. I wanted to write a little comment to support you.
ReplyDelete토토사이트
사설토토
토토사이트추천
I have read so many articles or reviews regarding the blogger lovers except this paragraph is truly a good post, keep it up.
ReplyDelete스포츠토토
먹튀검증
스포츠토토티비
토토
ReplyDelete배트맨토토
스포츠토토
This web site truly has all the info I wanted about this subject and didn't know who to ask.
The first thing you should do is to find a reputable homework assignment help provider. Make sure that the company you are considering is a legitimate company and not a scam. A legitimate company will provide you with all the necessary information and a free consultation to make sure that you are satisfied with their service.
ReplyDeleteA very good article. I am very happy to read this article. This article is too informative and helpful. I will share with my friends. Thanks for sharing this article and good knowledge. Now it's time to avail Locksmith in Leeds for more information.
ReplyDeleteAmr Helmy Company provides the best kitchen for sale with high quality and reasonable price using the finest materials and the best wardrobes design. Amr Helmy’s kitchen is covered by a ten-year warranty and immediate maintenance service,
ReplyDeleteقد يُلحق النمل في بعض الأحيان ضرراً في المنزل، أو أنّه ينقل البكتيريا ولكي تتخلص منه لابد أن تفهم طريقة عمل مستعمرة النمل وملكتها؛ لأنّ الملكة تستمر بوضع البيض، وترسل النمل العامل لجلب الطعام دائماً. مراقبة المسارات التي يمشي فيها النمل. تعامل مع المركز الالماني لانه يعتبر من افضل شركات مبيدات حشرية.
ReplyDeleteStudents will find this site especially interesting because they can learn a lot without ever getting in touch with the healthcare assignment help. Come here to quickly and conveniently complete your task. Why do you need more now? hence, hurry.
ReplyDeleteWhat a great invention. Thanks for sharing this one! https://www.collegeparkgm.ca/new/GMC-Sierra%203500HD.html
ReplyDeleteYour blogs are really good and interesting. It is very great and informative. This basically uses the grammar object as a global variable. It works in a single-core world, but it won't work in the many-core world we're headed for bankruptcy lawyers in virginia beach. I got a lots of useful information in your blog. Keeps sharing more useful blogs..
ReplyDeleteThe article is informative and helpful, this article is important for programming language. I hope you share more blog on this type. Now it's time to avail same day computer repair in Essex, MD for more information.
ReplyDeleteWe are grateful that you have such high-quality information on your blog.divorce in new jersey laws.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteYou’re doing a great job Man, Interesting stuff to read here. Keep it up.
ReplyDeleteI’m hoping to check out the same high-grade blog posts. Keep on Writing.
ReplyDeleteImpressive! Thanks for giving me an idea here. Hoping for your success.
ReplyDeleteThis type of clever blog work and coverage! Keep up the great works
ReplyDeleteThe text was neat and readable. Great blog. Thanks for blogging.
ReplyDeleteI am happy to find this way of writing the post. Thank you forthis useful information
ReplyDeleteI enjoy this kinds of own post. It Like this knowledgeable blog here.
ReplyDeleteYou put very helpful information on this website. Keep it up. Keep blogging.
ReplyDeleteLooking forward to read your next update, Thanks Nice article you have!
ReplyDeleteHi there, You have done an excellent job. Lot will be benefited in this web site.
ReplyDeleteI love this blog.. very nice colors & theme. Keep it up! thanks a lot.
ReplyDeleteIts enjoyable for me to come here and visit more here. It is terrific post dude
ReplyDeleteThis is a great inspiring article. Good work. Keep it up, Thanks for keeping it free
ReplyDeleteI hope you continue to do this awesome blog work, I support you for your writing
ReplyDeleteI am very thankfull to you for sharing this fantastic article, Useful to us
ReplyDeleteSuch a excellent post. Continue for sharing like this, Its helpful information
ReplyDeleteYou are terrific. I like this remarkable things, Keep it up and keep on writing
ReplyDeleteI like to see some other posts on the same subject! Very useful informative info!!
ReplyDeleteI must thank you for the efforts you’ve put in writing this blog. Awesome!
ReplyDeleteUncover the mysteries of Monadic Parsing and learn how you could have been the mastermind behind its creation. Dive into your website to explore this intriguing concept further.
ReplyDeleteSariska Resorts and Hotels offers a unique blend of luxury and comfort, set against the backdrop of the stunning Sariska Tiger Reserve in Rajasthan, India. This destination is not only a haven for wildlife enthusiasts but also provides an exquisite experience for travelers seeking relaxation and adventure.
ReplyDeleteThe accommodations are designed to cater to a variety of preferences, featuring well-appointed rooms and suites that reflect the rich cultural heritage of the region. Guests can indulge in a range of amenities, including fine dining options that showcase local and international cuisine, as well as recreational activities that allow for exploration of the surrounding natural beauty.
Monadic parsing is a flexible, state-dependent approach that excels in parsing sized binary formats like "3" or "12" World!" It allows dynamic determination of bytes based on prefix numbers, overcoming the limitations of traditional parsing techniques like BNF, yacc, and pyparsing. Monadic parsing is ideal for variable-length inputs where length is unknown ahead of time. Immigration Attorney Colombia Lawyers play a vital role in upholding the justice system, representing and advising clients, interpreting laws, and ensuring that legal procedures are correctly followed.
ReplyDeleteThank you for such a great blog post! I learned so much, especially from the part where you talked about [specific topic]. Your writing is so clear and easy to follow—it really gave me a fresh perspective. Can't wait to read more from you! brand marketing agency
ReplyDeleteI just had to say thank you for sharing such a thoughtful and well-written blog post. best interior designers in Chennai It was not only super informative but also gave me so much to think about. I felt totally connected to your perspective, and the way you explained everything was so clear and engaging. You can really tell how passionate you are about this topic, and it made reading it such a great experience!
ReplyDeletetanjore India Thank you so much for this amazing blog post! Your insights really hit home, and the way you wrote it was so clear and easy to follow. I didn’t just learn something new—I actually found myself thinking about what you shared long after I finished reading. It really left a lasting impression on me!
ReplyDeleteYour post was not just informative, but really inspiring too. Chennai biggest hospital I caught myself thinking about it long after I finished reading. Honestly, it left a lasting impression on me!
ReplyDeleteI really appreciated how informative it was, especially the part where you discussed. Your writing style is so clear and engaging, and I walked away with so many valuable insights. I can tell you put a lot of thought into it, and it truly made a difference in my understanding. Keep up the great work! Indoor play place London
ReplyDeleteI genuinely found it super helpful, especially the part where you discussed. Endurance Training benefits Your writing is so easy to follow, and it really kept me engaged throughout. I learned a lot, and it's definitely given me some fresh insights. Keep up the great work!
ReplyDeleteHey, just wanted to say a huge thank you for the amazing blog post! I really enjoyed reading it, especially the part where you talked about. It was super informative, and I loved how clear and engaging your writing was. I walked away with so many helpful insights—seriously appreciate it! palmera
ReplyDeleteThank you for sharing this fascinating perspective on monadic parsing! The explanation really breaks down what initially seems like a complex concept into something accessible and inspiring. It’s amazing to think how such an elegant approach could have been within reach with the right mindset. Appreciate the thought-provoking insights!
ReplyDeleteAbogado de Inmigración en Falls Church VA