libjit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Libjit] How to generate the commands in libjit?


From: Alexey Galakhov
Subject: Re: [Libjit] How to generate the commands in libjit?
Date: Tue, 19 Feb 2019 03:03:29 +0100

On Mon, 18 Feb 2019 17:53:41 -0600
Peng Yu <address@hidden> wrote:

> Hi,
> 
> I am checking examples in libjit. I can understand the gcd example.
> 
> What I miss in the big picture is how to translate the source language
> into the libjit instructions?
> 
> Does anybody have any good examples that I can follow? Thanks.
> 

Hi,

the question you're asking is damn complex. You need to write a parser,
which understands your input language and translates it into something
else (i.e. in libjit instructions, but in fact almost any other
intermediate language will do, you may even invent your own one).
Writing parsers usually requires dedicated tools. The well-known ones
are FLEX + Bison (or Lex + Yacc), my personal favorites for C are Ragel
+ Lemon, there are also many others like ANTLR. An random blog post
with code example of it:
https://mashandmish.wordpress.com/2011/11/11/a-simple-calculator-with-ragel-and-lemon/
Basically you'll need the same, just instead of executing operations
directly (interpreting them) you pass them to libjit so that they're
compiled, not interpreted.

If your input language is simple, you may prefer to parse it "by hand"
by writing a recursive descent parser:
https://en.wikipedia.org/wiki/Recursive_descent_parser
For more complex languages or for better flexibility it is usually a
good idea to use a parser generator (so-called "compiler compiler"). In
this case you basically describe your desired language in the form of
BNF grammar (provided your language is context-free).
https://en.wikipedia.org/wiki/Comparison_of_parser_generators
Most likely you want LALR, GLR or others of LR-family, but in some cases
LL(k) or even LL(1) would be adequate.

If you need a textbook about it, I can recommend "Modern compiler
design" by Dick Grune or "Compilers: Principles, Techniques, and Tools"
(the "Dragon Book"). I personally prefer the first one over the second
one as I find it easier to understand, but I may be wrong.

Regards,
Alexey



reply via email to

[Prev in Thread] Current Thread [Next in Thread]