开发者

How to do python-like indentation with flex/bison

I want my language to have two features that make Python such a nicely formatted language:

  • One statement per line
  • Blocks begin with another indentation level and go on until that's ended

Can anyone give me a detailed hint开发者_如何学Go on how to achieve that with flex/bison-like tools? Such a block feature forces the user to write readable code.


You could try to track the indentation level in the lexer, and add pseudo-tokens for indent and unindent. You will need to keep a stack of already seen indentation levels, and need to care about empty/comment-only lines differently. But I fear that at the end the lexer will become an unmaintainable mess and also you have some parse-specific state (the indentation stack) in your lexer.


Matt Might wrote an article on standalone parsers, with a way of handling significant whitespace using "unput":

http://matt.might.net/articles/standalone-lexers-with-lex/

(The example is half-way down the page.)


I think there is no way make a python-like syntax parser with ONLY lex/yacc, because lex/yacc can deal with Context Free Grammar only, but a python-like syntax is context sensitive.

The reason is, if you want to find whether a statement and the previous one is in the same block, you should let this statement knows the indentation of the previous one, that's the context.

I suggest you make some additional logic besides lex/yacc to accomplish that, and that won't be so hard. You could read codes here, in "grammar" modules.

The key is, let lex/yacc part parse single statement, with indentation level, and write something packing statements into blocks.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜