2

The most unusual aspect of Python is that whitespace is significant
instead of block delimiters (braces → "{}" in the C family of languages), indentation is used to indicate where blocks begin and end.
how can python interpreter recognize code block ?

  • 5
    I don't understand get your question - you're answering it yourself. (i.e. using indentation.) – Mat Jul 8 '12 at 11:43
  • i think my asnwer is clear"in c family we show block of code using { and }...but in python we dont use this character but how python can recognize block of code?? – AminM Jul 8 '12 at 15:33
  • 1
    The same way you do. By seeing how the code is indented. In my mind, this is one advantage of Python. The python interpreter recognizes code blocks in the exact same way the developer does. (Well...assuming your editor converts tabs to spaces.) – Gort the Robot Jul 8 '12 at 21:49
  • ok...so brief answer is "python use tab to recognize block"....nice! – AminM Jul 9 '12 at 7:29
11

The Python documentation, in the Lexical Analysis section, describes briefly how the indentation parsing works. In short, the tokeniser generates special INDENT and DEDENT tokens that are used by the parser when deciding where blocks of code start and end. These tokens (roughly) correspond to the { and } tokens in C-like languages.

8

Remember that whitespace appears as just another character to the interpreter.

  • A tab is 0x09 in ASCII
  • A space is 0x20 in ASCII

An interpreter is little more than a pattern matcher that then triggers predefined rules.

2

At the parser level, it's not all that difficult. The parser just needs a variable to keep track of the indentation level of the current block. And when it goes to read a new line, it counts leading spaces (or tabs) and compares the value against the indentation of the current block, then apply an algorithm that looks something like this:

if currentIndent = currentBlock.indent then
   parse line in the context of currentBlock
else if currentIndent > currentBlock.indent then
   create sub-block of currentBlock and parse line in that context
else finish currentBlock and run this same comparison on currentBlock.parent
  • There should also be a special case for blank lines - Python skips those. – btilly Jul 8 '12 at 13:21
  • There is also a special case for whitespace-ignoring contexts; for example, argument lists. – asthasr Jul 8 '12 at 15:23
  • 1
    Actually, for pretty much all existing Python implementations, the parser works exactly like it would in an ALGOL-like language, because the lexer keeps track of the indentation and injects fake BLOCK_BEGIN and BLOCK_END tokens into the token stream. IOW: the parser is kept mostly context-free by not making the lexer regular. – Jörg W Mittag Jul 9 '12 at 1:44
1
  • A block begins when the indentation increases.
  • A block ends when the indentation decreases.
  • Multiple blocks can be closed in the same decrease.
  • Blocks can nest.
  • Sequential blocks at the same indentation depth join up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.