lua-users home
lua-l archive

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


On 22/08/2011 11.08, Axel Kittenberger wrote:
On Mon, Aug 22, 2011 at 6:58 AM, Gopalakrishnan Subramani
<gopalakrishnan.subramani@gmail.com>  wrote:
I want to learn the Language parsing in Lua. The idea is to parse a language
called DDL (Device Description Language) and generate the Lua code out of
it.

Where I can start with if I don't know how the languages and semantics
works? Are any Lua library supports easy language parsing?

There is a lot of stuff you can find simply by googling :-) Just take
care that when I looked into this a lot of theory caters for problems
that aren't any more, like writing a compiler that only need 64Kb
memory. I'm personally also not too found of parser generators.
Everybody goes "ooh you need a parser generator", most of the times
they aren't worth the hassle, writing a parser is actually very simply
and not the witchcraft people make it. All you need to do is read in
the file as character array(1), then make it an array of tokens so
keywords become keywords multicharacter operators like<= etc. become
one token as well string literals and numbers. All you need to do now
is for the token array run your functions that understand it. The
"trick" is simply your stack is analog the compiled programs stack, if
you encounter an "if" call the "parse_if" function, if you encounter a
"function" call the "parse_function", if you encounter "end" just
return from the parse function you are currently in. While parsing you
build a "tree", basicly just a big load of objects that describe what
you've found. Finally just traverse the tree and output what you want
to output.

Thank you Axel!

I never had a formal education in compiler theory, but that's the simplest and clearer description of how a working compiler could be made!

I know that there may be languages harder to parse and more problems, but, man, a workable description in less than 30 lines of English! :-)


(1) search/replace "array" with "stream" if you want to

Also you might consider just your "DDL" to be simple valid Lua code.
Lua call syntax and so allows you to create an environment with custom
function that very much doesn't need to look like lua.



-- Lorenzo