Often I use sed 's/match/replacement/'
in places where I would rather have a more formal tool, just because sed
is what I know.
As an example, lets say I want to translate markdown to html. In sed I would start doing something like:
s/# (.*)/<h1>\1</h1>/
s/## (.*)/<h2>\1</h2>/
s/### (.*)/<h3>\1</h3>/
s/#### (.*)/<h4>\1</h4>/
s/`(.*)`/<pre><code>\1</pre></code>/
# etc...
Now for arguments sake, lets say that I had the grammars of markdown and html (or whatever two languages are the best for this example) defined in BNF.
Then instead of creating an ad-hoc parser for both markdown and html, I would just have to find a tool to translate from grammar A to grammar B. Is there any such framework?
- For example, I know pandoc can translate from several markup languages to each-other.
- Also a lot of languages now-a-days translate to javascript (eg: typescript, elm, purescript, coffeescript).
- I took a look at string-rewriting/semi-Thue systems on wikipedia.
I'm guessing that most of these tools do the translation by hand, rather than taking as an input two grammars and some mapping from one to the other. Is this even possible?