 Hello class, my name is Isaac and I'll be going over midterm one. So first question we have a set of regular expressions and some strings. So first question asks if M5 is in the language of alpha. So alpha letter or digit gives us M and digit star gives us 55 and letter star gives us epsilon. So it is in the language 58 and language row. So digit or letter gives us five and letter or digit star gives us eight. Language of five. So letter star gives us epsilon and digit gives us five and there's no way for us to get eight, so it is not in the language. Eight five, language of omega. So digit gives us eight, letter or digit gives us five and letter star gives us epsilon. M, P and Q. It's in the language of alpha, letter or digit gives us M, digit star gives us epsilon and letter star gives us P and if you look at the rule for letter you'll see that there's no Q in it so this is not in the language. M, P, M, P, that's a big M, five, P, M is in the language of row. So digit or letter gives us little M, letter or digit gives us M, five, P and there's no way for us to get little M so it is not in the language. M, P, M, five, eight. It's in the language of five so letter star gives us M, P, M and digit gives us five but we cannot get eight so it's not in the language. Last one, five, P and M, language of omega. So digit gives us five, letter or digit gives us P and letter star gives us M, M, M, P. Alright next question. So this question asks what tokens would be returned if we call it get token on the string multiple times. So first we look at the first character and we see what potential matches we have and we have digit, alpha, row, phi or omega. So if we read in five eight, digit no longer matches. Alpha can still give us five eight since letter digit gives us five and digit star gives us eight. Row can still give us five eight, five. Five can only give us five so that's no longer potential match. And omega can give us five and eight. Next we look at five, eight, M. We look back at alpha. So letter digit gives us five, digit gives us eight and uppercase letter cannot give us M so that's no good. Row, digit or letter gives us five, letter digit gives us eight and it cannot give us little M. So omega, digit gives us five, letter digit gives us eight and letter star gives us M. And we can keep going and get N and P so we can't get big P so all of this is omega. Next we look at big P, potential matches. So we look at P and letter will no longer match. Alpha can give us P with letter digit, digit star will give epsilon and letter star will give M and five can give us PM also. So if we look at PM five, alpha will give us P and M and there's no way for it to give us five so that's no longer. Any good. And the five can give us PM with letter star and five with digit. So this is the longest match. So now we look at little M and these are only two choices. So if we look at MP, we'll see letter no longer works. Row can give us, digit letter can give us N and letter digit star cannot give us P. So this one's also no good. So what we do is this is our longest token that we can match. And either letter or a row worked. So the tie is broken by the one that shows up first. So in this case it's letter. At least we look at P. So if we look at P five, letter will no longer work. And row, digit or letter gives us P and letter or digit star gives us five. So row will match this. That's the answer. So problem two gives us a grammar and question one asks us to give it a string that can be generated by two different pastries. So if you look at the rules for S, you'll notice one of them has two symbols that are the same following each other. D, D, D, E, or little E, big E. And if you look at, well, this already gives us a hint that maybe something going on with the two symbols. So if you look at the rules for D, you'll see D can generate any amount of terminals, little D. Or you can generate no terminals at all. So for example, you can have, say for this section of the pastry, you can have little D, little D. And this one can generate both of these. And this can generate just epsilon. If you draw, you can draw, you can do this another way by having this symbol generate both of the non-terminals. I mean the terminals. And this one generating epsilon. And then for these, you can just do whatever you want. You can have D give us epsilon and D give us epsilon. This is a mistake. This should be, oh, this is right. All right. So in our case, the string was D, D, E. Next question asks us to generate first and follow sets. Okay. So first we look at the first rule. S goes to be D, D, literally big E. And we see we should add the first of B to first of us. But first of B is empty currently. So we add nothing. We look at the next rule. S goes to D, E. Again, D is empty, so nothing gets added. So next let's look at B goes to little B, big B, D. So little B gets added to the first of B. Next rule, B goes to little B, big C, little C. My bad, that's a little E. So we already have B in first of B. So nothing gets added. Next rule, B goes to epsilon. So we add that. Next rule, C goes to little C, big C. So we add little C. C goes to epsilon. First of D goes to little D, big D. So we add little D. And D goes to epsilon. E goes to little B, big E. So B, E goes to E, little E. And E goes to epsilon. Okay, so since we added a couple of symbols to our sets, we go back to the top of the rules and go through it again. So S goes to B. So first of B is added to first of S minus epsilon. And since B has epsilon, we look at the next symbol, which is D. And first of D is added to S minus epsilon. And again, since D has epsilon, we look at the next one, which is also D, which has epsilon again. So we look at little E. So we have little E. Next rule, S goes to big D, big E. So first of D is added to S. So D is already in our set. And since D has epsilon, we look at first of E, which contains B and E. Again, we have those. And since all the rules in S goes to D, E contain epsilon, first of S also contains epsilon. And if you were to go down to the whole rules again, you would see there's nothing to add. These are the final sets. Okay, so follow. So the start symbol always gets the end of follow. We look at the first rule, and we start at the end of the rule list. So we look at big E. Since big E is at the end of the rule list, we add the follow of S to follow of big E. So that gets that. So now we just go left. We keep on going left through the rule list. So little E, that's just the term now. So let's get that. Big D gets little E since that follows big E. And now we look, we go left again, and we see D gets the first of D, which is itself, so nothing gets out of there. Then we go left again to see big B. And follow of B gets the first of D. So first of D is D. Next one, you don't add epsilon into follow sets. Also since D contains epsilon, you add the follow of D into the follow of B. So the follow of D contains E. So B also contains E. Next we go to the next rule. S goes to D, E. E already contains the follow of S. And follow of D gets the first of E. So in this case, we add B. Since E contains epsilon, we add the follow of E to the follow of D. So D also gets the end of the file symbol. Next rule. D goes to little B, big B, little D. So we'll just get to the non-terminal in the rule, big B. So B gets D, which already has nothing else there to add. So the next rule, B goes to little B, big C, little E. So C gets E in its follow set. The next rule always contains epsilon, so we skip that. C goes to little C, big C. So C gets the follow of C, which is nothing to add in. So there's nothing to add in this rule. C goes to epsilon, D goes to little D, big D. Yet again, nothing to add in here. D goes to epsilon, E goes to little B, big E. E just gets the follow of big E, so same thing. E goes to little E, and E goes to epsilon. All right, let's go through this rule this one more time. So first rule, big B, big D, big D, little E, big E. E gets the follow of S, D gets E, D gets the first of D. And follow of B gets the first of D. So D, E, B, and no, this is wrong. B gets the first of D, D, E. Okay, next rule, S goes to D, E. So E already contains an symbol. D gets the first of E. So in this case, it has that. And nothing else gets added here. And if we go through the rest of the rules, you'll see nothing else is going to get added, so we're done. Okay, question three. Approve the grammar supports a predictive parser. So to show grammar as a predictive parser, we have to show that it's a rule, let's say, A goes to X, alpha, and A goes to Y, alpha. So the first of X, alpha, intersection with first of X, alpha, it will be an empty set. And to show that for each rule, for each non-terminal, it contains multiple rules. The first sets of each of the rules must be completely disjoint. So if you look at our grammar, B has two rules. B goes to C, D, and B goes to little B, big D, little B. So first of C, D intersected with first of B, big D, little B. So we add first of C to the first set, which is C and epsilon. Next, since C has epsilon, we look at the first of D, which gives us D and epsilon. And since all the non-terminals in the rule list give us epsilon, we add that also. Okay, first of little B, big D, little B, it's just going to be B. And if you see their intersection is empty set. So next we look at C, since that contains more than one rule. So first of C, big C, intersect with first of epsilon. So this just gives us C, this just gives us epsilon. Actually, I think I'm going to stick up here. So since this gives us epsilon, we would have to look at the follow of D. So if you look at the first rule, you'll see B is followed by E. So we add that, which gives us, which is still the empty set. So since this contains epsilon, we look at the follow of C, which is D or E. And this is empty set. Okay, last rule. First D, big D, first of epsilon. So this just gives us D. And here we'll look at the follow of D, which will give us little E, D, which is empty set. So these all meet the first rule for having a predictive parsing. So now we look at the second rule. If a rule contains, if a non-terminal contains epsilon in its first set, then first of A intersection with follow of A must be the empty set. So let's look at all the symbols that contain epsilon. So the first symbol we have is B. So first of B and follow of B. First of B contains C, D, epsilon, B. And follow of B contains E. So this is empty set. Next we look at C. First of C contains C and epsilon. And follow of C contains D and D. So this is good. Now we look at first of, or we would check out D. First of D contains D and epsilon. And follow of D contains little E and B. So the second constraint is noticeable. So for the next question, it wants us to write parse B, the function parse B. Okay, so the first thing we do is call get token. And then we'll look at our grammar. So we'll have B can either go to C, D or little B, big D, B. So if B goes to big C, D, it will expect to see first of C, which is C or epsilon. So if C contains epsilon, it can also see the first of D. You want me to write that out. So first of C, D is C, D. And since they both contained epsilon, we will look at the follow of B, which has E. So if T type is equal equal to C or T type is equal equal to D then we will parse C and after that we will parse D. Actually, I forgot to un-get token before this. And then we will print out the rule B goes to C, D. Okay. Else if... Now we check B goes to little B, big D, little B. So the first of that rule is just B. So we'll just check if T type is equal to B. Here we let get token consume the character B and then we call parse D. Print B goes to B, big D, little B. And after that there should be another little B left out of parse D is called... Actually, we should check that before we print. So T type equals to get token. If T type equals B then... Or if it's not equal to B, then that's a syntax error. Kind of running out of space. So I'll just keep writing over here. So after that we call print F to print out the rule and then we leave the else if. And then we'll have another else that will just print out some text there. And then we're done with parse B. So now I want to say write parse C. So get token. C contains two rules. The first of C, big C is just going to be C. So if T type equals C, then we consume the character C and we call parse C again. Else if... Check that we look at the second rule first of F's month. Then we look at the follow of C which gives us D and E. I guess I should really say first of C equals F. So D and E. So here we check if T type is equal to D or E. If it is, we un-get token and we print out what rule we followed. C goes to epsilon. So I forgot to print here. Print C goes to middle C, big C. Else syntax error. So that's parse C. So now we look at problem four which gives us a program and it asks us the output assuming static-sculpting or dynamic-sculpting. So if we assume we have static-sculpting we'll have a global J and the function main, the first line J equals one and crackle is called. So in crackle, if J is less than four so this J is referring to the global J and it's one so J is equal to J plus one. So two and we print out J. We leave crackle and we go back to main and bar is called. So here we have a new J that's created and set to zero and we go inside another scope where another J is created. That's set to five and then crackle is called again. Since this is static-sculpting you look inside the function crackle and you see if any J is defined there and there's no J defined in there and then you look outside crackle and you can see the global J. So that's what J is referring to J equals two. So if two is less than four J is equal to J plus one and we print that out then we go back to bar and we leave this scope so that's freed and then print J. If you look what we're printing is called you can see that this J was created there so we print zero and then we call crackle again and again this J refers to the global one so three is less than four so we increment that to four and we print that out and then we leave crackle and then we leave bar and this is gone then we call crackle in main which refers to the global J four is not less than four so it's no longer incremented and we print out four again and then we return so that's the output of that now with dynamic scoping global J is equal to one at the beginning we call crackle and we have J being called we look at the callee of crackle which is main and then we look at which we see that the J there's no J defined here so we look at it we look at the autoscope of main and we see that it is global J so crackle is referring to the global J so J is incremented and we print that out and we leave crackle we go back to main we go to bar and here we create another J equal to zero and we go into another scope where J is equal to five and we call crackle so crackle is called inside the scope where this J was created so it's referring to that one five is not less than four so we don't increment it and we print out five we leave crackle and we leave the scope so that's called here we call another print and this one's referring to this J so we print zero we call crackle again so crackle was called in bar with this J and it's called so it's referring to this one so this is incremented and we print out one we leave crackle and then we leave bar it's gone then we call crackle one more time and this is looking at this J so we increment that to three and we print that out and that's how I'll put this in dynamic scope so problem five asks for some box circle diagrams for the variables A, B, C, and D given this program so first off the first four lines create A, B, C, and D and it wants us to go to point one and we'll stop there so the next line B is we allocate a size we allocate memory one and memory one is a it's a pointer to a char and we set the value of B to the address of memory one so this gets the address and memory and the next line we allocate a char and we have C points to that the next line D is equal to to the character D open a mistake here this should be so here it just contains D and then A is given the address of B or yeah, D's value is assigned to the address of B so this points to that so at point one this is what we should have, okay? so that's down to that so now it wants us to draw how the diagram looks at point two so we just keep going so so the next line D references A so we follow that to here and it assigns it the address of C so this is the longer points to that it will point to C the next line goes we look at A gets the reference once and twice and one more time and then we assign the value of D into this so that gets D also the character D next D is set to A so now that is our final answer and the last question asks us for two sets of aliases at point two so for an alias you just have, so here we just list pretty much two different names or ways to access the same memory using different variables so for example this points to whatever B points to also so there's one and we can also do something like star star star B is equal to star C so if you see D for reference one and twice which gives us a character D and this will also give us a character D and those are two sets of aliases