How does the linker or interpreter recognize the indentation in python?

The lexical analyzer (tokenizer) uses a stack to store indentation levels. At the beginning, the stack contains just the value 0, which is the leftmost position. Whenever a nested block begins, the new indentation level is pushed on the stack, and an “INDENT” token is inserted into the token stream which is passed to the parser. There can never be more than one “INDENT” token in a row.
When a line is encountered with a smaller indentation level, values are popped from the stack until a value is on top which is equal to the new indentation level (if none is found, a syntax error occurs). For each value popped, a “DEDENT” token is generated. Obviously, there can be multiple “DEDENT” tokens in a row.
At the end of the source code, “DEDENT” tokens are generated for each indentation level left on the stack, until just the 0 is left.
The parser then simply handles the “INDENT” and “DEDENT” tokens as block delimiters — exactly like curly braces are handled by a C compiler.
So we learned a new thing in python:Cheers:)
PS:I am awesome

Comments

Popular posts from this blog