[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua AST and Parsing a "C" Like language
- From: Axel Kittenberger <axkibe@...>
- Date: Mon, 22 Aug 2011 11:08:57 +0200
On Mon, Aug 22, 2011 at 6:58 AM, Gopalakrishnan Subramani
> 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
> 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
(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.