How is a scope block identified in a language like Python?

Asked

Viewed 240 times

3

How a language identifies a block where it does not use keys to open and close?

function exemplo(){
  // ...
}

I believe that in the example above the parser create a scope block from the keys, but in a language like Python

def exemplo():
  // ...

It takes indentation level as a priority?

How is the reading done? How is it generated?

  • First, what is a language Duck Typing? What does this have to do with the scope or the command block? You want to know how Python identifies the beginning and end of the command block?

  • That’s right, how is identified the beginning and end of a scope. I asked the question without reading anything about Duck Typing just what I thought it would be. Reading this article https://osantana.me/duck-typing/ I saw that the title is totally wrong.

1 answer

3


A language like Python, as well as several functional languages, just to name a few, the delimitation of a block of commands (which can generate new scope or not) is basically indented (the offset of spaces at the beginning of the line).

When the parser identifies that there are additional spaces at the beginning of the line relative to the previous line he considers that there was a indent. When he identifies that a new row has returned to the previous column that was before on the previous level (he goes piling this up), then he considers that there was a dedent. The mark of indent works as key opener and the mark of dedent works like the key lock. You may not be seeing anything but the compiler knows that there is a tag there that has been calculated.

Obviously you need to format the code well for everything to work and the parser "not get lost". Of course some rules can facilitate this.

Python still uses the two dots in the line before the block to identify that it will have a block ahead which even facilitates the parser at least understand better what the intention was and anticipate (Lookahead).

This has nothing to do with Duck Typing (as the original question asked).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.