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