开发者

Serialize AST in parsable format

I have a DSL with Java front-end and I would like to serialize an AST that I get in front-end part in some easily parsable format to make it easier to write a back-end part that generates a code in different programming languages. Is there is anything better th开发者_JAVA技巧an XML for this purpose?


XML produces lots of text, and ASTs can be phenomenally big. (I build parsers and) our parsers will produce XML because our customers demanded it... but none of them actually use it. IMHO, it is better to design a custom format that encodes your tree densely, to avoid the time it takes to read and write the ASTs. For instance, you might settle for:

( nodetype =value childnodes )

where ( ) are parentheses, with ( opening the tree node, the nodetype is an integer representing the node type [maybe even in a high-radix format to minimize character count], = is present if the node carries a value [you don't really need the = sign if you think about it], and the value is provided. The childnodes are inlined before the closing ); if they are present, they obviously start with a left paren. No spaces needed! If you actually need to read this sometimes, you can build a simple dumb tool to indent the parentheses when you need to see it.

[Old timers will recognize this as LISP S-expressions, and this has been around since the late 1950s as a tree encoding].

(We avoid this altogether pretty much because reading and writing trees is expensive no matter how you do it, and its often just easier and more efficient to finish processing the tree you have in memory and just spit out the final answer).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜