 Morning everyone, so maybe you can't tell but I don't have the mic so we're using the mic that's on The monitor, which is really bad So you all are very lucky because you're experiencing this and good sound quality when this goes up It's gonna be terrible. So I'll try to Restate your questions and all that because I'm not just gonna get everybody's Questions in maybe a couple minutes for project 3d questions No, no project 3 questions. Okay, I'll know It's a trick to get you to ask questions that didn't work It's because you're all done nice. I like that So what do you do when you're doing it by hand Right, so what do you can what are you comparing when you're doing that right at each set Changes in what so you have Right, let's say first sets right so you go through Kage first sets you compare it to the one right before it Right, there's been any changes and you do it again Yeah, there's kind of two different ways right so one way would be you have some flag that you set whenever there is a change So you you start assume there are no changes and then go through That's actually a little Can be brittle because you're applying these rules over and over again, right? So one way to do it is to just do exactly what you're doing, right? I mean by hand so create a copy of the last time through of all the first sets Right, then go through calculate the new first sets because you need those old copies right when you're when you're calculating first sets and Then at the end you just compare and you say hey you have a method that compares all the first sets and says hey has anything changed and Then if it has then you know you have to do it again and keep going Any other strategies? Handling that yeah Because I Okay Okay So about an example like What about that this is a valid context for grammar, right? This is actually why in class I mean we're doing it by hand. We don't actually do it recursively Right, we're doing it iteratively and we're this is why we start with initialize all the first sets to the empty set Right, and then we go through and every time we need a first set. We just look up what the last value is So we're keeping it around. We're not Going through to recap. I'm trying to actually recursively calculate the first sets for all of them because then you get into this kind of a case Where you run into problems other questions? A minute the other projects are well, they deal with things in this class, but they don't directly build on top of each other So you don't need to worry about it in that sense You do need to worry about though like You need to make sure your first sets are correct right to do follow sets That's one way it builds on top of it. Yeah That's what I've heard I don't know I Think it depends on you individually like the other projects are also super cool, but this one I've heard is more difficult because you're starting from scratch So that that's the way you know that you've done first sets correctly without epsilons, right? And so then doing follow sets you're basing on top of those with no epsilon. It should be Assuming you've done it correctly. It'll be fine. Cool. All right, so let's get back into Okay Okay, so we've been talking about how we resolve functions We've been talking about how do we go from a name from an invocation of a function to the actual function definition? So what does C++ use to do function resolution? Yeah name number of parameters and parameter types Right so This is how it looks up which name whereas in C it just uses the name So if we have a C++ program that includes a header file as a function foo that returns 10 As a function foo that takes in an integer X and returns 10 plus X Then if we're gonna call foo So which instance of this foo is that going to be resolved to the first one Right the compiler is gonna see the name foo. It's gonna say okay. I see a function foo How many arguments does this invocation of this function foo have? Zero so it says is there a function named foo with zero arguments Yes, right here, so I know it's gonna call that and then so then when it sees this invocation of a function foo Which function is this going to resolve to? The second one yay right here, right? So it's gonna say oh, this is a function that has One parameter and that parameters an integer so I know which one to call So what's this going to output? So first question is is it gonna compile correctly? Yes? No? Yes? Right so the function signatures here are different so one is just foo right the name foo parameters zero No parameters, but the second one the name is foo the number parameters is one and the type of that parameters in it Okay, so this compiles correctly then what happens when I run it? What is this output? What they do? 10 20 so what's the what's test going to be 10 and bars gonna be? 20 so we gonna do it and run it yay, okay any questions on function resolution? cool So is this independent of static dynamic scoping are they dependent? How are they related? That's who your language designer yeah But does it happen to me like that? No, and some languages like Python or JavaScript actually a bunch of languages you can define functions within functions So if I could define a function foo inside main, where would that function be valid for? Inside main inside that block right depending on the scoping rules So still the scoping rules come into effect here, so this just resolves How do we map this invocation of this function to the actual function definition? But finding that function definition right? Uses the scoping rules so it'll say okay if using static scoping then we look in Main scope say is there function foo declared no then look in the global scope its parent Oh, there's a function foo declared here So these are you know Separate issues, but still very tightly linked okay So it's gonna be something that Is again again gonna seem pretty Basic but Actually when you get down to it, so what what does assignment mean? It's what I mean by assignment value to a variable Right like the equals in a lot of languages But what are the exact what does this mean when I say when you have a statement x equals y what precisely does that mean? The value of y is associated with x What was that? X is being assigned the value of y Something about the memory put the memory in the y with the memory of the x Depends on if it's a primitive or an object, so what would the difference be and in what language? Is that having a C++ or Java? Definitely Java So it just depends kind of right I Guess one thing is in some languages this actually isn't how you do assignment This would be an equality check that says is x equal to y right like in I think it's sequel. This is how you do equality It's not a double equals so really It really depends on the programming language right what exactly happens here what happens with pointers What if they're pointers? What if there's values in there? What about if there's values associated with x and y? So before we do that we're going to find So we need to be able to break this down this Assignments semantics in and we need to be able to talk about them and understand them. So we're going to talk about four different things. So What's a name in reference to like a variable like this sitting Doesn't have to point to a memory location. It does depend right, so yeah, let's Think of the name as kind of the high-level like the product programmers abstraction, right? It just gives a name To something so and we also say it's refers to some declaration Right, so we know that this name uniquely maps to a specific declaration But does the name have say anything about? Where this thing is stored how it's stored is it stored in memory? Is it stored on the disk is it stored in a register? Is it stored on? I don't know. What are some other things in the network on a? cloud server Right where is I mean so at a high level. We don't really care about that We care about that name X or that name Y and where that is declared Okay, so a location is now we're getting a little bit closer to the hardware and But we're still gonna think abstractly. So we're not gonna think about Necessarily memory bytes layout all that kind of stuff We're gonna say that a location is some kind of container that can hold a value and a binding is Going to map a name to a location So this is that association between okay the name X refers to some location And that location inside of it can have a value Right, so what what are values in our systems? It's kind of like the lowest level you could possibly go Yeah integers or you know, it depends on the language, but let's say like see Right depends on the type of the variable characters integers strings Actually character pointers, so it's characters once again. Okay, so The way we're gonna use to think about and Understand the semantics between assignment statements and the relationship between these four values between the name the location the binding and the value We're gonna use what's gonna look like something that's gonna be kind of silly, but actually makes crazy pointer operations Manageable and doable and actually really helps how you think about these things So we're gonna use box circle diagrams and they're exactly what they sound like they have boxes and circles Pretty easy, right? Don't even need to be an artist. Okay So if we haven't see we have a declaration into X So from this declaration, what's the name? X So we need some way to represent to we need somebody to associate these four values So what's the value of X right now? Depends on the programming language, so in C, what's the value of X? Undefined. Yeah, it's kind of tricky Existential question what does an undefined memory value exist? Yes, it does exist, but when you use it the behavior becomes undefined, so maybe it doesn't exist. I don't know not a philosopher Okay, so we're gonna represent this with the name X incredibly simple part And so we need to say that okay Depending on where this X is used Right, there's gonna be some location in memory or in somewhere, but we actually don't care where it is But we're gonna use boxes to represent a location and say okay We know that there's some location associated with X and specifically we're gonna use this Binding to say okay X is bound to this location Some square box and then inside that box. Here's where the circle part comes in we're gonna draw circle and So whatever value is inside the circle is going to be the value associated with X Seems pretty easy, right? Good, okay So if we declared our int X and Then we say X is equal to five So how do we interpret this? What's the relationship here between this and our? Four values we're talking about. Yeah, so we're saying that our our variable named X is bound to A location containing the data value of five almost very close We're not actually so in C Right where we have an int X. We have a declaration like this We say X is bound to some location that binding doesn't change in C Right so here. We're not changing the binding of X, but we're saying copy the value five where Yeah, copy the value five into the value of the location associated with the name X Right, so we're saying okay. We have X and we have our five and so we're gonna copy five into the value We're gonna copy five into the value Into the value of the location associated with the name X and so after this Executes we're gonna have five in here in our box circle diagram So I don't know that five was a value. How do we know it's as much like copy it in there? Where does five exist? Does five have a box when I drew it here does five have a box Location associated with it Created a memory Can we ever reference the location that this five is no right? It's it's just a value It just exists right? It's a value that exists that has no location no box associated with it Yes So we're saying and this is why it's so kind of what comes to mind is like an immediate value Like it's it's it's a value right? So we take that value and we copy it into the location associated with the name X So how is this different than? We have a case here of in X in Y X is equal to Y Right, so yeah, what's the semantics of this X equals Y, right? So copy the value in the location associated with Y to the value of location associated with X Right, so how many circle blocks diagrams do we have here? To write Y actually has a location and a value So we have X and Y and we're gonna take whatever's in Y and copy it into the value associated with X What if I do this? Just change things What does this mean? You're saying copy? the value Copy the value the location associated with X into the value Yeah, same thing right, so I just copy this value and copy it into here That's the same thing as we saw before So what's the difference between an L value and an R value? You've heard those terms before Left and right actually, which is really terrible. So it's an L value versus an R value Why is it important? Yeah, you can guess it's okay. It's not Let's go back here Assignment, mm-hmm. That was very good. Cool. All right, so L value R value the difference here is what goes on the left hand side of an assignment operator Can I have something like this? But why not? Why can't you do this? Because five has no location associated with it right five is just a value You can't assign into five. There's no Five is another way to put it an immediate value, right? There's no way to Change that or do that or put anything in there. So then is this valid? So then what type of value is X an L value? yeah And R five is an R value so And this is kind of the way to think about this and keep this because Is it very obvious that you can do this? I mean that you can't do this right five is equal to x I think we all kind of intuitively from our The years you've spent programming you kind of know you can't do this. It doesn't make sense Right, but this helps us answer the question this is the add this is The anchor sand sign you could Can you Have you tried it? So that's what that is it's a little messed up, but that's Of a reason you would ever want to do that So what does it depend so how do we know if we can or cannot do that? So can is this valid? I do this assignment L value is equal to some L value So how many combinations of this am I gonna write can I do this L values you a lot R value? You know how to do that? Yeah, can I do this? Not always are there sometimes that I can? If it has a location it would be an L value what about this one can I do this could or couldn't Then What do you mean if if we know how do we not know? Are we ever not gonna know Can we flag it as what was the other question it would just be a say it again What's the difference between these two so I can do this right? definitely like this is It's absolutely what equality does but can I do this can I do this? That's the same thing Somebody said depending on the language that's is True, I guess but Possible actually wait right here Yeah, so what type of value is it It's an R value right it has no location associated with it So can you do either of these two because you're saying assign the L value to the value at the location of the R Yeah, the assignment is put whatever the value in the location associated with this into the Well, where do you put this it has no location? It's just a value Right in that diagram when we said x equals 5 right we had x at a box on our circle And we have five outside five is just a value. It doesn't have a look. There's no Location associated with five. So we just copy five and put it into here Yes, and l value has a location associated with it. That's the way to think about it So if there's location associated with it, it's an l value if there is no location associated with it Then it's an R value Mm-hmm. It would be an L value exactly. Okay. So let's think about that and then Let's kind of keep this in our heads Right, so how would we know if we could do this or not? Yes, exactly. It depends on what this address of operator does does it return an l value or an r value? Right, and that's the key idea That's why we're looking at what is l values and r values. It's very trivial when we look at well x's and fives Right, but when we have pointer operations, it gets to be a little bit more tricky So we need to think about that a little bit more So let's go back and let's look at the the assignment semantics here So we have l value is equal to r value, right? So We kind of already said this so An expression is an l value if there's a location associated with the expression So why am I using expression here? What's an expression? Uh, is this an expression? Is that an expression? Yeah, is just this thing an expression? Is this an expression? The whole thing an expression. Yeah, is this an expression? Yeah with the star. What's the star here? Point or dereference? Is this an expression? So is this an expression? Yeah So can you put expressions? Where can you put expressions? Can you do is so this be like a general Let's say a assignment statement Syntax we call this our context free grammar for assignment statements. Can you have expressions on the left-hand side? Can you just do this x plus five is equal to Ah, so Wait, okay. Just a second. Let's go to this We'll say like assignment is expression equals expression Right, so what are some things that can be expressions? Yeah, so just like an int. What else can be expressions? Uh, let's call it id's first right id's so just x. Yeah, right. What else can be expressions? So what do we say? So you've mathematic operations. What's like on the left of the plus Right, and then we have or expression times expression And then we have all of our other math operators Yes, we also have right pointer dereferences. So we can have star expression We can have an expression Is the address of operator So i'm gonna have Can I write this? So is it valid? Syntactically Yes, yeah, not actually. I don't have to see grammar Actually supports this one. I think it would yeah. What can what? Or No, because there's no operator here You should x plus plus plus five. I think it may work Right, is this an expression Let's see. I was going to do equals but actually equals is an expression itself Right x equals y you can use this like plus 10 or something crazy like that. This is the cause of a lot of seed errors So go back to here. So is it syntactically valid? Is it semantically valid? Why not or why? So what is the thing of this expression on the left hand side? It's an l value. Is it an l value or an r value? It's an r value Right x plus five. Is there any location associated with x plus five? No, right? I'm just doing math. I'm just taking the value that's in x adding five to it There's no location associated with there, right? So this boils down to r value Is equal to what's the right hand side? What kind of l value r value? r value Right, and so is this allowed? No, there's no way to do this No Okay, so that's why when we look at these rules we're going to look at What the semantics are we're going to talk about variables Okay So we've talked about an expression as an l value if there's a location associated with that expression, right? So does the expression x have a location associated with it? Yeah, right good job. We have x x is bound to a location Right is x plus five have a location associated with it? No, it doesn't make sense right there's no location associated with x plus five Okay, so then what's an r value? Yeah, so basically no location so just a value basically so um So then x equals five. So what is this an l value equals to an r value? Yeah, so what are the semantics here? So Generally with the l value and r value, right? What does that what does that mean? Don't talk about x's and fives we want to talk about l values Yeah, right so copy the value in r value to the location in l value, right? So then can we do five equals x? Oh because there's no location right there's no location so it's not semantically valid good Okay, what do we have an l value? One and l value two What are the semantics of this mean? Yeah, so copy the value in location associated with l value two to the location associated with l value one, right? This is exactly what this means So when I do things like a is equal to b plus c So what's a l value or r value? l value uh b plus c It's an r value right so what we're going to do is the the r value is the value So the way to decode this right so the r value of b plus c is the value in the location associated with b Plus the value in the location associated with c adding them together is A value right that has no location associated with it Right we're doing computation. We're taking these adding these two things together And so So they had to do with this means is copy the value associated with b plus c to the location associated with a questions on this okay To go through why this would be useful the question is I have Let's say I have an x. We're not even going to give them types right now. I have a y And a b. I don't know something like that I say x is equal to the address of y and b is equal to a dereference of y star star of y And then I say something like I don't know y is equal to the address See how many times going to do that? Oh like the address of b and We can keep going on so question could be like what's the what is the values in x b and y Right, what's the circle box diagram for this whole thing? So we've done assignment, right? But now what do we need to cover to be able to understand what's going on here? Basically what what each symbol means? As to where we how we draw our arrows in the diagram Yeah, so what does what things mean? What's that called? Semantics, which is our topic. Yes Had to do that. All right, so the address operator. So what kind of an operator is this? How many arguments is so think of it like a function? Yeah, so how many arguments does it take? Just one All right, so it's a unary operator. What does it return? The address of whatever yeah, so Uh, so then think about types in terms of l values and r values So what can you apply the address of operator to and I do what about our values? Can you apply it to an r value? Our values and our location Can you take the address of five? No, because there's no location associated with five Right, so if you think about types, right that input to the address of operator has to be an l value And so what does it return then basically like in the box circle diagram where the box Right, so yeah, so it's going to return an r value Right, and what's the type of this r value now if we think about types So let's say the type of the operand is some type t like int What's the type of the return value T star right it's going to be a pointer to an int Right, but it's going to be an r value So it's very important So what is this value so we talk about it like what you know We can go a little bit closer to the metal I can see so what is this value supposed to be an address. What does that mean? Right, so the address So the value that it's going to return is the address of the location associated with the l value that The address of operator was applied to In our in the case of t state or return type t star It would return t Yeah, so let's look at So I'm going here. I just know you guys can see that Yes, okay Exactly, okay, so let's say I have an x Equals five Now what's my circle box diagram here gonna look like No pointing Bound to a location and what's the value inside that location? Five Okay, what's the address of this location? Do we care? No, so let's give it some abstract value alpha, right? It doesn't really matter what it is So then if later on I say the address of x What's this going to return? Alpha Right, so it's going to return the address of this location associated with x So if I have this so let's go like this first And then I have y is equal to the address of x so What's my circle box diagram for this declaration? Just this line Just this I would agree, but it's a pointer. Shouldn't there be like some other thing here that points to some other thing? Exactly, right? So this is one of the things you have to kind of Get clear in your head, right the fact that this is a pointer just Only matters for types Right, it's an int star. It's a pointer But why is just it's just a name it has a name y It's bound to some location that at the start has no value in it, right? There's nothing in there. So when I get to this next line So what type of assignment operation is this? What are the types of the left hand and the right hand side? Types and like l values are values So y is what? An l value and what's the whole expression on the right hand side going to be An r value So what's this going to do? How's this going to affect my circle box diagram? Well, it's not it's going to be x alone because it's not actually Doing anything other than acknowledging x And so the value at the location associated y is going to contain the address of The box associated the location associated right so the address of x So what's that going to return? What's the r value that's going to return here? Alpha and so I have l value is equal to r value. So what are the semantics there? Yeah Right so copy alpha. So take that r value alpha put it in the location associated with y Right alpha here. So does it matter what is in here? cool Okay Then let's think about so the opposite of this kind of is point is the dereference operator, right? So what are the what's the type of this operator if we think about it as a function? What does it take in as input and what does it return as output? How many arguments does it take in one one argument? Okay, so can it be applied to l values r values? Yeah, so actually either so the restrictions are can be applied to an l value Or an r value of type type d star All right, and so what does it return? Returns of value and r value An l value either all five So can we do this if we say after here can we do this? Yeah, have you written programs that do this? So then what does it have to return an l value, right? Otherwise this wouldn't this wouldn't be semantically valid So what does it return then? So yes returns an l value, but what does it return? What was that? T Yeah, so if y is t star the type will be t. Yeah, but what does it return? Right, so what is this doing? Right, so okay at the end result. What is this going to change in our diagram here where the x is here? Does the dereference operator change anything? No, what does just this do? This expression Of location at the address of the address of y is beta At the value stored at the value stored at the address stored in the value of y At the value stored in the address of y do we need to know the address of y do we need to know anything about beta? All right, so way to break this down is Take the value associated with y and return the location associated with that Right, so here This alpha return the box associated with alpha. So star y refers to this location And then when we have an l value, right? This is the l value. So we can set this l value to be 10 Right, we know how to do that. We just take 10 Copy the value as 10 to the location associated with this l value Which is right in here Okay We're gonna go much more in depth on this and see exactly how to draw the box circle diagrams, but we'll stop here for today No Ah you return the out the location associated with the value of y so you're actually returning basically the box