Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 on how to achieve that with flex/bison-like tools? Such a block feature forces the user to write readable code.

like image 727
Lanbo Avatar asked May 12 '11 07:05

Lanbo


Video Answer


2 Answers

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.

like image 125
Rudi Avatar answered Sep 19 '22 12:09

Rudi


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.)

like image 21
wlangstroth Avatar answered Sep 20 '22 12:09

wlangstroth