I want my language to have two features that make Python
such a nicely formatted language:
Can anyone give me a detailed hint 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.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With