31
Jul

9.2| Scope – Compilers and interpreters 8m09s



welcome back in this video we're going to begin our discussion of semantic analysis with the topic of scope the motivating problem for talking about scope is that we want to be able to match identifier declarations with the uses of those identifiers we need to know which variable we're talking about when we say variable X if variable X might have more than one definition in the program and this is an important static analysis step in most programming languages including in cool so here a couple of examples taken from cool this definition of why this declaration of Y that it's a string will be matched with this use and so we'll know at this point here that Y is supposed to be a string and you'll get some kind of an error from the compiler because you're trying to add a string and a number in the second example here's declaration of Y and then in the body of the let we don't see any use of Y and that by itself is not an error it's perfectly fine to declare a variable that you don't use although you can imagine generating a warning for that that doesn't actually cause the program to behave badly but instead what we see here is a use of X and there's no matching definition so the question is where is the definition of X we can't see it and if there is no outer definition of X and we'll get an undefined or undeclared variable error here at this point so these two examples illustrate the idea of scope the scope of an identifier is that portion of a program in which the identifier is accessible and just as are the same identifier may refer to different things in different parts of the program and different scopes for the same name can't overlap so whatever the variable X for example means it can only refer to one thing in any given part of the program and identifiers can have restricted scope there are lots of examples I'm sure you're familiar with them of identifiers x' whose scope is less than the entire program most programming languages today have what is called static scope and cool is an example of a statically scoped language the characteristic of static scoping is that the scope of a variable depends only on the program text not on any kind of runtime behavior so what the program actually does at runtime doesn't matter the scope is defined purely syntactically from the way you wrote the program now it may come as a surprise that there's any alternative to static scoping in fact probably every language that you have used up to now has had static scoping but there are a few languages that are what are called dynamically scoped and for a long time actually there was an argument about whether static scoping was better than dynamic scoping although today I think it's pretty clear that that the static scoping camp has won this discussion but historically at least Lisp was an example of a dynamically scoped language and it has switched in the meantime this is actually a long time ago now that it changed to static scoping another language which is now mostly of historical interest isn't really used anymore a called snowball also had dynamic scoping and the characteristic of dynamic scoping is that the scope of a variable depends on the execution behavior of the program so let's take a look at an example of static scoping so here we have some cool code and a couple of different declarations of X and also some different uses of X let me erase these under lines so they can use the color to indicate binding so let's take a look at this definition the question is which of these uses of X we have three uses of X here actually refer to that definition so it is in fact these two the ones that are outside of the inner let these actually refer to this definition so here if you refer to X you get the value 0 but this other definition here the inner definition of X is is used by this use of X so this use of X gets this fat this meaning of X which in this case returns the value 1 and what's going on here is that we use the most closely what's called the most closely nested rule so a variable binds to the definition that is most closely enclosing it of the same name so this X the closest enclosing definition of X is this one but for these two x's the closest and only enclosing definition of X is this outer one so in a dynamically scoped language a variable would refer to the closest binding in the execution of the program meaning the most recent binding of the variable so here's an example let's say we have a function G and g defines a variable a and here it's initialized say 2 4 and then it calls another function another function that isn't in the same syntactic scope so here I've written F right next to G but actually F could be in some completely other part of the code and F refers to a and the question is what is the value of a here well if it's if we're dynamically scoped then it's going to be the value that was defined in G and here f of X will actually return 4 that'll be the result of this call because this reference to a will refer to this binding or this definition of a in G and we can't say much more about high dynamics how dynamic scope works until we talk in a little more detail about how languages are implemented so we'll talk about dynamic scope again a little bit later on in the course and cool identify our bindings are introduced by a variety of mechanisms there are class declarations which introduce class names method definitions which introduce method names and then there are several different ways to introduce object object identifiers and these are the LED expressions for all parameters of functions attribute definitions in classes and finally in the branches of case expressions now it's important to understand that not all identifiers follow the most closely nested rule that we outlined before so for example a rather large exception to this rule is class definitions in cool so class definitions cannot be nested and in fact they are globally visible throughout the program and what does that mean that means that a class name is defined everywhere if it's defined anywhere in the program that class name is available for use anywhere in the program where everywhere in the program and a particular class name can be used before it is defined so as an example take a look at this fragment of cool code here and here we see that in class foo we declare Y to be of type bar and then later on we declare a class bar and this is perfectly fine cool code the fact that bar is used before it is defined has no effect on whether the program is correct this is a completely legal cool tone similarly with attribute names attribute names are global within the class in which they are defined so I that means they can be used again before they are defined so for example I can define a class foo and I can define a method that uses attribute a and then later on only later on do i define what attribute a is and that is perfectly legal so normally we list attribute definitions before method definitions but that's not required actually the method and as we definitions can come in any order we like within a class and in particular an attribute can be used before it is defined finally method names have quite complex rules for example and Method doesn't have to be defined in the class in which it is used it could just be defined in some pairing class and also methods can be redefined so it's possible to do what's called overriding of a method and give a method a new definition even though it has been defined before we don't actually have the language yet to talk about these rules with any precision but we'll be going into this in future videos

Tags: , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *