compiler design in C++, a practical approach. | Project FPGA | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

compiler design in C++, a practical approach.

teacher avatar Project FPGA

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      00. introduction

      2:32

    • 2.

      01. copying the content of our file

      7:21

    • 3.

      2. What are tokens, their different types,

      9:13

    • 4.

      3. How the scanner , scans documents

      7:30

    • 5.

      4. What are whitespaces and how to compile them

      7:21

    • 6.

      5. What are keywords? How are they scanned?

      9:05

    • 7.

      6. How to scan character tokens

      6:40

    • 8.

      7. how to compile generated tokens

      22:44

    • 9.

      8. introduction to parsing

      9:05

    • 10.

      9. setting up parsing and semantics analysis

      22:16

    • 11.

      10. What is a parser ?

      10:41

    • 12.

      11. How to create classes for semantic analysis

      11:16

    • 13.

      12. Read these attached files

      9:12

    • 14.

      13. How to compile function declaration ?

      9:56

    • 15.

      14. How to compile variable declaration

      4:35

    • 16.

      15. How to compile function exit

      10:54

    • 17.

      16. The postfix expression algorithm

      24:46

    • 18.

      17. Assigning register to variables and reading expressions

      26:30

    • 19.

      18. Compiling expressions

      15:31

    • 20.

      19. compiling logical OR, logical AND, and Bang expression

      26:55

    • 21.

      20. Compiling while loops, and exiting while loops

      5:23

    • 22.

      21. Compiling for loop

      6:22

    • 23.

      22. Compiling IF, ELSE IF, and ELSE expression

      6:26

    • 24.

      23. Compiling switch statement

      6:44

    • 25.

      24. Compiling function calls.

      12:08

    • 26.

      25compiling tenary operator , return, break, continue, label, goto, cout keyword

      13:12

    • 27.

      26. What next ?

      3:45

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

33

Students

--

Project

About This Class

This course takes a  step-by-step practical approach to the design of a C++ compiler. The student will design the lexical analyzer or scanner, after which he/she will design the syntax analyzer or parser, then semantic analyzer and the intermediate code generation. 

All these units are compiled one after another. You will also learn about tokens. How they are generated , types and also create your own token enum list. You also be to design a scanner which scans a C++ file and generate tokens from them.  You will design a parser that parses 20 different language constructs we will use in our tutorial and also be able to add your own custom language sentence.

This course is for beginners, intermediate and advanced C++ developers. who want to advance their programming skill through a project design and for developers who want to learn about compilers and also learn how to design them. we would start from the basic tokens and develop into the more complex subject in a step by step manner.

The high level programming language used in this course is the C++ language.

The only tool you will need is a good C++ editor example VsCode, Visual studio etc..

Basic knowledge of C++ is required, our compiled file will be an assembly language code which will consist of a  mix of the standard MIPS assembly and RISC-V  which i will teach you in this course. A lot of course material including the full working compiler script which designs these different units  is also attached.

Meet Your Teacher

Teacher Profile Image

Project FPGA

Teacher
Level: All Levels

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. 00. introduction: Welcome. In this tutorial, we're going to learn how to design a compiler in C plus plus. This is a practical coding costs are written in C plus plus. So fast, we break down the lecture into series of topics. The first one we are going to learn how to open a C plus plus file and copy its contents. Because for us to compile a C plus plus code, we have the Vesto for C plus plus file and be able to copy the contents of the C plus plus file. Next, we're also going to learn how to generic tacos from codes. Took us this smallest unit of the written in C plus plus code. And for most programming languages. So after copying the contents of the file we want to compare. We're going to learn how to generate two comes from the file. Then next, we're going to learn how to check for error. So we're going to check in multiple stages who check for error, error doing the scanning stage. Also check for error during the fossa when we will be passing out, suppose passcode. So after generating the tokens, we check for error. And then after that, we're going to start what is described as passing, where we compared it to coincide with our code constructs. Then after that, we start our semantic analysis, which is ascending or tokens into language sentence. Finally, we do decode degeneration. Degeneration. Then also compare different C Plus Plus key words and different C plus plus conditional statements, switch statements, if else statements as so many other C plus plus constraints. Without wasting much time, I'll do another introduction in the next lecture. Then we'll start decoding practicals. Thank you. 2. 01. copying the content of our file: From scratch. You need is a good understanding on the C Plus Plus language. Through pad is a computer with VS Code installed in it. And you know, go, go see prosperous ID you have. So our aim in this course is to be able to open a C plus, plus five copies contents. I'll compare it using assembly language denoted H gathered from this course. You will also be able to compile in assembly language of your choice. Theory is not compulsory suffice to aid you in understanding the syntax. To my base with spring when assessor. So let's begin. The combiner is a software that converts a program written in a high-level language, low-level language, which is the assembly language dataset we would perform in this course include number one, we write a program that's opens a C plus plus file, copies its contents, then closes the file list. What is Xander is a code analyzer. Then the task is to design a syntax analyzer. And the fourth task is to take the semantic analyzer. And finally, we're going to design the intermediate code generator. Let's get started. First, create a folder named on paragraph or any name of your choice. That would be our project folder. Then open it to VS Code or any C Plus Plus is using greeting new file called File Open Door C plus plus. This file will be used for our task one, which is to copy the contents of C plus plus file. We're going to start by initializing are basically does. Is he identified that includes every standard library function. It's fees, fees for performing versions of a special pointer called file pointer is used to which is declared. On line nine, we open the file in read mode. How do I find in this case is C plus plus. That is the way we have Zoom react combatting. Online 11. We check if the file exists. By chicken you define quantized model, will print an error and it's going to copy the contents. To copy the contents, we first calculate the size of the file. No critic chapter in toward the center fi size and copy the contents into that. We created. We started on a team. We first of all move the pointer to the end of the file using the RefSeq method and selecting FCPS used to move pointers associated with a given file to a specific position. It comes to three different positions. The first one is sick, and that means you're moving the pointer to the end of the file. Then the next one is six sets. It means you're moving the pointer to the starting of the phi, that is the beginning of the file. Then the last one you sequence, it may decline position of the file. Then on line 19, we calculate the size of the file from the beginning of the file, the file pointer, which is currently at the end of the file. So we'll be using F to calculate the size of the file on my nights after in C Plus Plus issues to find out the size of the file with respect to the starting position of the file. So now we are calculated from the beginning of the file pointer, which we are now we've positioned to the end of the file besides the face now stored in file size dan online 20. We take the fat pointer back to the beginning of the file. On line 21. We could denote job buffer, and I send it to the size of the file on line 20 to do nemo, this is called prefer online 23. We check if tempo Phi is 0, then the memory allocation is no successful. Alliance with it. Read the contents of this C Plus Plus fat into the buffer using the f function and if successful, will print successfully opened. I'm delighted to delete the buffer and close the file. I'm finally online that says returns. To read more about C plus plus check out this link is fun. Gigs for geeks dots up. My command line. I will navigate to the project folder. I want a five for the first time. You only need to get it fell to a bullfight. Alloys arrow is true because I have not created the test post those C plus plus file. Then they're secreted towards C plus plus file in our project folder. Again, He's going to print five opened successfully. Then next step, since we are going to be compiling any kind of sucrose was fired, I'll call it different name, not just test dose C plus plus. So we need to find a way to be able to impute them of any C Plus Plus filename on the command line, I need to be able to be combined with to do this is to add the filename on the command line. So that would be able to redefine them from the command line. So to do this, our main function to be able to command-line arguments. Then we pass the documentary called failure open. That is going to replace our test does C plus plus friend Lane. Now we can compare and if I dig in on the command line. So with my buck, Amanda and I commend, please check out this link. Clicks for kicks. Line 29 will not just add to the Fair opened successfully. To call the next function that we call our fat false, is going to be a scanner. The scanner will be able to scan through our fat searching for what you call tokens. Next class, I'll show you how this can ask Kansas hippos both fine. 3. 2. What are tokens, their different types,: In our last class, we completed task one of our compiler design, which is opening a C plus plus file and copying its contents. In this class, who scan the contents of a file, searching for what we call tokens. So we start by creating two phase scanner dot h and c plus plus. I mistake iniquitous Canada, see, but outlet I tend to scan into C plus plus in a later video. So quit is kinda the C plus plus file now. So we include these candidates each other file in our file opener plus C plus plus file. So these include we enabled our file opener to call the scanner function on line C plus plus. That is where our scanner was called. And the argument there is of our C plus plus file. So unless we declared its function in US, Canada, THE edified and initialize it in C plus, plus. This kind of function, we scan through our file contents, searching for tokens. So we need to create articles before we start creating new header file called Dakota th, which will contain our tokens. We declare an enum called to contempt, which will contain this of all our tokens. What are tokens? How to find to cause us a sequence of characters that can be treated as a unit the grammar of a programming language. They include keywords, constants, string, identifier, known bus, operators, and punctuation symbols. These are the units of R programming language. So we need to break down our scripts into tokens and then we'll start matching them into meaningful sentences. So example, consider the sentence in E is equal to 500 semicolon. So the smallest unit will include int a equals sine 500 and semicolon. So these are the tokens in this file. Says the sample scripts and list of tokens you can find here. I've compared this of different tacos within discrete. So the list is not exhaustive, will still add more tacos in the pasture. So you can see to confer, which is used to represent integers. And I have to conform, which represents names. Then tokens, semicolon, took an identifier is used for representing any identifier. Then to Khumbu, to constraint comma two constraint to compound two less than or equal to vertical hand to conspires is four star. And assignments. Then slash and assignments, they are together. The star equal to a is a single token. Then sandwich slash. If war is a single called slash assailants, subtract a war is a single token called soap as islands to conserve as elements. Plus a, which represents add assailants. Then at the end isn't supposed to be too cold. I feel lost to come, come at the beginning here. While to cool place to greater than two coolness elements or shifts right? To container. For ternary operator to call self, then classy to come for it to contain our no, I won't be using it for this tutorial is actually a customized to include in it. You'll see two couldn't see out for printing C plus plus. So as the competitors and you also the syntax developer. So it's your responsibility to determine what tokens to use for your language. Our tokens not to include. How each took us is used under arranged to form the language sentence depends solely on your decision. You will still include intermediate to like to end tokens that some tokens will be lost along the way, like to control and to come force will be comforted to took a number and assign the values of 10 respectively. Then the end of every C plus plus file is terminated with a slash 0. And this is regarded as token end of file. That is, the end of every string. Cafe will be terminated. We slashed 0. This would be used to signal the end of our scanning process. So once when we're scanning to tacos, once we scan to call slashes or two, that means we terminates our scanning process. Next, we populate the token types along with a list of all these tokens. Then like I said, area will be generated letter C or C plus plus. Data are also numbers arranged in order of precedents. Example here, nuclear war on line two is equivalent to 0 ecological, or is equivalent to one to chronological and is equivalent to two. Equal, equal is equivalent to three to concrete. Greater than is equivalent to for us. To end of file online data for which has the highest value. For D, arrangement determines the numerical value and order of precedence. And it's very important, especially for cooper cognition. Example, let's say once referred to logical operators, instead of listing all of them, you can refer to all to cause less than two colon and two constipated and took on air for heritability, cooperators is equal to two coins greater than two and less than two combined. It's symmetric and logical operators combined together is equal to two constants and to combine and took was greater than or equal to a. Is the water to condense greater than or equal to two compound and less than or equal to token itself. So these arrangements are very important because we would use it to refer to operators in group. In our quote, DR. will be tend to want to refer to logical operators. So we're not going to list all of them one after the other. So we can see two cones less than two concurrent and tokens created to Korean War. So it's up to the competitors enter to determine the best method for code compilation is not compulsory to arrange them in order of precedence. Use competitors enter will determine what is the best method for your design in plants towards glucose. In our next tutorial, we'll see how the scanner scans, how could such a thing for these two goals and how is it able to find it? 4. 3. How the scanner , scans documents: In our first lesson, we learned how to open any C plus plus file, would like to compile and copy its contents into our scanner function. Then in our last class, we will land would do concert. And you're also define some tokens in our enum type token type. Have you C plus plus file we're going to compound must be made of two console groups of tokens. Any syntax not present in R2 con list, he says seem to be an error. So in this class, this can now scan through our code within every carton, simple searching for tokens. Once it took on his found it to be called the lexical analyzer to generate that token. Now, there are four groups of tokens this canal will scan for. The first one, unknown bias. These are token non-bias. Their consists only of the 0123456789. Another character is not in number than the next one is whitespace. Whitespace is a term that refers to characters that are used for formatting purposes. In C plus plus, this refers primarily to species, tabs and sometimes new line dino useful during compression. So find them and discard them. Then the DOD set of tokens are characters. Characters here means any symbol that is in there and alphabets, number. They can include bank, add sign, pound sign, dollar sign, or do sine, inverse sine and sine asterix for multiplication, opening and closing parenthesis and open our cuisine basis underscore plus sign minus sign for sine. Sine of Western Mac. Greater than, less than comma food punctuation mark, slash or division sign or sign. Then you have the block quotes and the block parentheses. Then the final group, the last group of tokens add the alphabets, so they are divided into two. The first one are keywords which includes o is otherwise like switch in Berlin, string, break, continue, while, if it is, why it is, the second one refers to identify us, which includes any ADA token that is not an alphabet. You fed them so as identify as non by five. Now that token is an error. So in US, Canada or C plus plus Fool Fest include autocrine dots, each header file into the scanner. So this will allow us to call out two cos. It will be renamed to pass under THE in a future tutorial. Then I've declared a function. We're using our scanner. They include the skip whitespace, generate token is numb, keyword to coin, chat to corn is Alphabet and make tokens or discuss what they do next. So in our scanner, oh, assign our file content, which is already in the buffer, has an argument to the Guba pointer variable named took on. This will allow us to access our content in any part of our file. Note the difference between asterisks token and token. While to Kuhn holds the address of our buffer. The asterisk, two components to the first character in the buffer example. So assuming you have shadow cone, which is equal to our incisor boy, then a series to converge refer to the first Carta of this chart, which is e. Then if we increment the SAS token, then you now start pointing to the second card, which is now. But using a book woods are both parentheses We can pick in to see to get the value in any part of the file without shifting the pointer. So assuming we seek for default character is formed, is going to be E. So from our sins, we are now at how we can accounts are as high as one. Any asked to z the S3 and S4 there. Remember we are pointing that out. If we incremented, again, is going to start pointing at I. So this is how we are going to scan or two cos. So that's kinda function. Again. We'll use a while loop to go through all the cartels and our compiler file or token. We loop onto you. The end of file, if I show it does true. This function returns true when the coin to Qn is equal to 0, which is the terminating token, auth token and or fire. Now why didn't the loop, we search for our four different kinds of token. On line two, int1 does keep whitespace functions such for why species as keeps them. Then D is non-function texts for token numbers. And if we find the unknown bar, we call degenerate taco function to generate the number. There is Alphabet function takes four, identify AS and keywords, and use the first letter is an alphabet. It's called the keyword function. This function checks if the token is a keyword, is a keyword, then if he's not a keyword, then is an identifier. Then finally, if we don't find anything yet, we call the chat function. This function takes four samples online that you are incremental to culture, to point to the next character study group. Again, until we get to the end of file. While we are truly do loop and get to the end or five online data tree. We make token end of file and close the scanning process. And now an S class or equal to initialize this franchise. 5. 4. What are whitespaces and how to compile them: In this lesson, we will start in shares in all the functions we call the scanner class. The first question is the end of our function, which checks if you're going to con, is equivalent to slash 0 or, and or fat. End of file. It returns true. And our scanner while loop terminates and cause the Mac to consume a token. End of file is a static function because we need it only in this file. And it's also improving function because it's written so the true or false nest is the skip whitespace function. Just below the end of our function, we also declare skip tokens functions, which we'll use to skip the way species later. Why do we regenerate now tacos, we also want to store the line number where the tokens that period. This would be useful if we want to report an error. So that you can ask though, which line the error appeared, creates the glabella in verbal and set it to one. Since all C plus plus phi starts at line one, not 0. So we're not going to initialize it to 0. We set it to one. So this software species and cools into space. New line, tab, carriage return, single-line comments, and more time Command D or more. These are the ones we use for this project. Again, in our skip whitespace function, we use this switch meant to match the current token with each of the whitespace character. If no match is found, we skip it. But if I match is found, we increment our top two points to the next character and recheck it again. So the aim is to ignore the whitespaces. So we do not accept two increments to the next character onto the coin to con is no longer whitespace. Then we'll continue with the rest of the functions. Then if the whitespace is a new line, we increment our token and also increment the line number to indicate that we have entered a new line. Twins nine, we check for single line and multi-line comments. In C and C plus plus, you do not compare comments just like the rest of the way species. So whenever we meet to single-line comments, we need to skip everything that's concept tight until we meet a new line. And whenever we meet a multi-line comment, we skip everything that comes after it until we meet the asterisk slash, which is used to terminate a multi-line comments in C and C and C plus plus. So on line 29, we check if the coin tokenism slash. Then we check if the next token is another slash, then it is a single line comment. Then using the help of the Skip to confession, we skip all the tokens until we get in, get, until we need a new line. So we use the new line as an argument to the skip the whole function. So whenever we miss a new line, it means that we are no longer in a single line comments, we have entered a new line. Then if the next token is an asterix, it means that it is a multi-line comment. So you are going to skip all tokens until we meet the asterix as lashed tokens. So we use this keep Token function which uses two arguments and as an asterix and slash to ask the arguments. The arguments to the skeptical functions are the characters that terminates each of the comments. For the single line, comment is terminated with a new line. So the argument is keep token is a new line. For the multi-line comments is terminator, which Asterix and slash, those we can test against would be the arguments. We are going to give this keep tokens for a multi-line comment. Else, if is needed a new line. Neither a multi-line comment then is just c tokens slash that is the division sign. So we make to call function to generate the token. So therefore, it is necessary to always check DNS to conduct comes after it without increments END token. So that is where we do not increment is the data we used to want to check the next token. Now let us initialize the skeptical function. We incremented using polymorphism. For the single line comments, we take one argument, the topo line comments, we take two arguments, the single-line comments. We're taking the new line and then Watson and the asterix and slash arguments. This clip to co-function eliminates comments by skipping two cars using a while loop until you miss the past arguments or new layer for the case of single line and asterisk slash foremost, thank comments. It also makes sure we do not meet the end of file unexpectedly. In the case of contaminated comments. We will loop till the end of the file. Then we check for end of file. If we are at the end of that means we do not terminate the comments. So we call the error function. The error function takes two arguments. The error to report, which is a string, and the rain lumbar. We add the error code. So this is why it's important to store, to save the line number. You're also going to use this rain on bamboo while we are dealing with the semantics. But we declared the error function in the parser dot h file, which I will explain to you later. Then online Tatooine, we incremented to come by two so that we skipped it to terminating tokens, Asterix and slash. Useful multi-line comments termination, both for single-line comments. We do not increment it to consents. It led by a new line, which will be skipped by the skeptical function. In this tutorial, we've seen how the scanner, scanner wild species and also how the end of Phi function checks whether we've reached the last token. And unless the Doria or scan Lombards and alphabets alcohol The generates lumbar and the keyword to call functions. 6. 5. What are keywords? How are they scanned?: Welcome. In our last tutorial, we saw how this can ask Can why species? And now it takes, if we are at the end of file, now in this tutorial, we'll scan numbers, keywords, and identify us. So let us begin. We'll start by defining the non-function. This function returns true if the current token is a number. You pass out to coolness, documentary it to check if it is a number. It first of all, convergence is from char to integer by subtracting 0 from it and assigning it to an integer e. Then it checks if the value is between 09, it returns true. If it's an integer, else is returns false. So in our scanner function, if the coin to coronary tons true for its known function, then it means is a number. Then we pass it to degenerate Token function to generate the token number for us. So this generate two coef function. We will check the rest of the tokens to know if there are also numbers, engineering token numbers. For the case of multiple integer, say example 214 Entry 89828983. Check character-by-character to know if they're all number and then generate token number. So it will loop through our tokens and match all of them together. Nest. The first token of every keyword. Identifier is an alphabet. To check for identifier, then we have to check for alphabets. We use the alphabet function. We simply check if the coin token is between E and Z, are between capital a and capital Z, or if he's an underscore. So these are the only characters that are used for generating keywords and identify as if he's an alphabet. We then call the keyword.com function. Now let us define the keyword to confession. Just between the scanner function and the Sabbath alphabet function. We define the keyword function. It should be a static void function because it is private to discrete and does not return any item. Already. The server function, which called this function, has already found the face that all the keywords would be an alphabet. So using a switch will match the first letter in the keyword function. If it matches the first letter of any keyword, then we use the keyword function to check the rest of the keyword. Example, consider break ampoule keyword. First, let us be, so if the coin tokenism B, then the check keyword function, we check for weak and all W0 L without DFS theta B. Since it has already passed, the switch sends the first data has passed the switch. At the end of this risk case, if no keyword is matched, we generate two con identifier. That is, if we didn't, if it does not match any of the wall, then we assume is an identifier. Check keyword takes three arguments. The first is the character. For example, in the case of brick, it we only check for our E K to check how many characters to check. So in the case of brachii to check for you for characters are E and K. Since be Azure AD match this which then finally, it takes as an argument the type of token we are generating. In the case of the normal of the tokens token break. So the total cone with the type of token we are checking for. So that's the token it will generate if it is successful. Detect key what we call the make Token function to generate the Taconic, the rest of the tokens match and also returns true. When it returns true, we announced keep degenerated tokens two points to the next token within our code file. The number we increment is equivalent to the number of remaining characters or the token. Example for break, we skip out e k, which is a, went through four so that the thumb points to the next token. So after the first letter of every keyword, detectable function checks the rest of the characters or the keyword. It is a function that returns true if he's found the keyword, else it is false. So it's four. The first thing to check keyword function does is to make sure that whatever comes after the keyword is needed and alphabets. Number. Example, brick, brick one, Greek a book 25 does not represent the keyword break, but doesn't mean we have brick equal to break in parentheses or brace semicolon. It's all presents a keyword. So it's going to make sure that after the keyword, the next character that comes after it is neither a number or an alphabet. So to do this, it adds the two tan on both too close to the cranes to corn and checks its online 89 example for to come break the non-Chinese equal to four. If the first database or position 0, then we add four plus one to two so that it took him becomes five, which is the character that concept tacky. Last letter of brick. Then we take the character's disposition to measure. Isn't there a number? Alphabets. Then online it will use a for loop, which moves to the rest of the token, comparing it with the actual names of the token we want to generate. If at any position they do not match. The techie world financial returns false. Else it is, is the loop returns true? Then there are also special case to back the n, which in C plus plus is equivalent to a new line. So for such it took one, we are going to convert it to string and assign slash n, which is also we went to new, new line. It took on pin is a customized token I created for my brain. I'll get on my website. So here we see two competing candidate. Ignore it. Since it's not a standard C Plus Plus syntax. Then if the keyword match and everything is successful, we call the Mexican function and pass the token type, which is the total agreement, as arguments with the next token. How returns true? So the next token will now generate our token. Back to our keyword to confession. If none of the first letter is your match, or if the first letter is a match, but the rest of the tokens is not a match. Then we assume is an identifier. Therefore, we generate token identifier. Identifies are user-defined names. Why keywords as standard language defined names? So that is a major difference. So we already know what keywords we have. So if we check and none of them is a match, that means is a user-defined. So as a competitor is n is your duty to define what keywords you need your programming language. So now we have seen how the scanner scans for keyword and identifier. In our next tutorial, we'll scan for symbols using the chat to confession. 7. 6. How to scan character tokens: In our previous lesson, we learned how our scanner scanner non pass whitespaces, identifies and keywords. In this tutorial, we'll learn how this can ask and symbols is in the chat to confession. Just between the scanner and degenerate to quit function. We initialize this just to call function. This function is used to check for symbols. You also used to go by verbose here called paren counts and discounts to check for invalid breeze and parenthesis. This function using the check field function declared here. Within the charter conformation. We first check to make sure we are not at the end of file. Then using a switch, It's matches the coin took on with our standard language symbol. If a match is found, it's caused the token to generate the token. Now while we match an opening brace on line, wondering 84, we call the check field function with argument of 0 and incremented the parent counts. Well, we match it goes in parentheses Online OneDrive 89. We call the check field function again with argument of one. And also decrements paren counts, where we match an opening brace on line wondering 94, we call the Chick-fil-A function argument of two and increment the count will match a closing brace at it. We call the tech field function with an argument of three and documents brand counts. These steps is taking to ensure that our braces and Boeing disease are declared properly. Detect fill function uses these two gaba, verbal brand counts and Viscount to store the number of open parentheses and braces respectively. If we match an opening brace in our chat open function, we increment the discounts. And if we match a closing brace, it decrements the base counts. Therefore, the count keeps track of how many bases are currently opened. Also, if we match an opening parentheses, we increment the count. And if we match it goes in parentheses, we document the parent counts. Therefore, the parent counts also keeps track of how many parentheses according to opened. So if burn counts are based, count is equal to one. It means an opening parenthesis or opening brace is yet to be closed. And the idea is 0. It means that no parentheses or braces is open. Right above the chat token, we declared the check field function. Now the rules of decorum parentheses and braces in C plus plus test that for key S1. That is when the coin token is a closing parenthesis, you cannot have a quizzing point. This is when all open parenthesis have been close. That is, one per encounter is equal to 0. Therefore, if you include another closing parenthesis, it means you didn't actually close any parenthesis. Now kids Tuesdays, that which is when the coin token is an open brace. You cannot have an opening brace when o opened parentheses, I've not been closed. You cannot declare a breeze within an open parenthesis. Open disease must be closed before you can declare inputs whether opening brace, closing brace. Now in case three, that is when the coin token isn't. Closing brace states that you cannot have a closing brace. When our open brace, I've already been closed. Also, you cannot declare a closing brace. One, or open parentheses have not been closed. This is how the check field function must show that the parentheses and braces had declared properly. Was we implements more error check in later tutorials. So back to our chart to confession. In the chapter confirmation. Once it took on his matched, we call the metal can function to generate the token using the token type as arguments to the Mexican function. There are also cases of tokens that consists of multiple characters as ample tabu semicolon, which we present infinite loop, or here we refer to these as token infinite double hyphen, that is double minus sign, which we present the decrement operator. Or to consult self, double plus sign, which represent the increment operator or two cannot self then minus an equal sign, double and sign the sign, the plus and the equal sign. So all these are instances of tokens that consists of multiple characters, that is double characters. So in such a case, if we detect any of the face card, example, if we match plus, we do not call make to come to generate token plus The festival for tetanus toxin. If it is also a plus ten is definitely, definitely an increment operator, then we call make to come to generate two cannot self. Else we call the metal come to generate two km plus also indicates of double symbol to Cos. He also incremental compare one to point to the last symbol. In our next tutorial, we'll discuss the lexical analyzer for Sean, make pecan and generic.com. 8. 7. how to compile generated tokens: So let us begin by comparing function declaration and function initialization. So that's the first language sentence. So remember our function declaration and function call is marked by a variable type fooled by a function token has ample list of variable types, include token. Followed by to confirm is the function declaration to Khumbu, full bed to confirm, to convert it for bed to conform to continue to constraint. So someone says or C plus, plus, we're going to define the sea to conform function, which is the function that gets called whenever we meet these vanquished combination. So in our C2 conformation, which compounds for ensuring, I'm going to take four steps. First, we are going to add our function to the function list. Going to add our function arguments to the argument list. Then we're going to compare our function is and then also going to push the function to the scope. So first we declare C to conform function. The argument point I is the index of the function type in table. Now, before we add the function, the function is, you need to measure that this function does not exist already in the function. So to do this, we're going to set index e is a verb when assembled the brand also the index to our function. So we're going to use index ii as indexed to the function list. So we set it to Fortune index to preserve it so that you can look to our function list. We subtract one because we increment the index by one whenever we add an item to the classes. Next we increment the pointer points to the function, which is to confirm. Now using a while loop, we compare the function name in our two functions in the function this class. If it matches fun, we choose an error. So here we only check for functions that have been initialized using the coal variable. Whenever this cover with 0, it means the function has only been declared, it has not been initialized. But if it is one, it means the function has been initialized as well. So you can declare it again. Then nest we declare the global variable which we start functioning and assign it to a function name or line 70, It's called F9. On line 79, we add our function to differential list. Here we are going to be the function name and initialize other things to 0 and also increasing desk next to other function arguments. Before we do that, we need to set our function arguments that index. The coins arguments index. Remember that one of the function, this parameter is indexing the argument is class, where the arguments of that function stats festival for copied that index because that's repeating decimal is first argument is going to start. Copy to differential is before adding the arguments. In this form, the simple duple will be used as arguments in Desk. So our pointer is currently pointing at the function name. After we incremented. The first time, we now we move the point that count up by two points on the first argument is going to now skip the first branch. This is opening parentheses, a non jump to the first item within the parenthesis, which will be the first argument type or a closing parenthesis. Arguments. If at this point you get equals in parentheses, then it is an integer arguments. So we preserve our pointer using counts. We set elements non batches 0 because we need to count the number of arguments for both, so we are resetting it. Therefore, if the next token in our token table is not closing parentheses means the argument list is not empty. So we initialize argument number one. Now, the number of arguments. The number of arguments is equivalent to the number of chromosomes. Separate those. Within the parentheses plus one, which we already said, Agamemnon Botswana example there one comma, it's much easier to arguments. If you Council comma. It means there are three arguments and so on. Next, we need to count number of commerce in the argument list. The loop terminates while we meet the closing parenthesis. Before you update the number of arguments to the argument number. And online Eighty-five, if there are no arguments, we simply increment pointer to point to the next token after the closing parenthesis. So using a loop on line eight, we start in certain documents, the argument table, the parameter or argument type. It, the index of pointer for by the arguments in them with the interests of pointer plus one. And finally the arguments number, which we increment whenever we're adding arguments. And we set it at the beginning of the function. So the index will, this argument number will only save the index of that particular function. The cell with the lookout, when we look at verbal, previously, one-on-one, we increment pointer. This, we skip the argument type arguments in them and it terminates in coma, often occurs in parentheses. After they want arguments you incrementally battery. So as you can skip that part circular arguments, which is terminating comma and now points to the beginning of the next arguments. Again, we skipped by three, we'll print again to the beginning of another argument. We add it to the argument is, is keep begun by three until we meet the closing parenthesis. So at this point, we create our scope counts and scope. We include the scope that when in such in scope tonight, the scope name unique. In Saturday school, we're going to add the scope that as a prefix or a surface. The idea is that if we added an increment, this scope depth, it means that everyone within this group would be unique. If you have two if statements. The first one is named Scott, There is one. That means if one. If we now as if to the scope, that we make sure that every atom within this scope have a unique name. Once we attach it to a scope, we incremented, guessing you add it to the next scoop. No other scope will have the same index of scope debts. The major syntax difference between the function declaration and initialization is that function declaration is terminated immediately with a semicolon after the arguments. Why IF function initialization starts with an opening brace. Next one we'll see is we take what we expect. Opening brace or a semicolon. Then function declaration and initialization, not just the credit declaration. We said the cover both to one, our function list. And you set to 0. It means that this function declaration only, as we're going to do that soon. And if the function is initialize them, we push this. We need to put the function into the scope. So anytime we assess the top of this group because the scope is also a stack, we always find out that we are inside the function. Major reason we need a scope. Anytime we check this cook top, we always find that, yes, we are within this function. So that is the major reason we need the scope counts. So let us know where we are is useful when we'll be terminating. One will be as it's in the scope, because different scoop have different ways to terminate them. So it needs to know that this is an if statement that I'm terminating. Therefore I'm going to compile it this way. You need to know that these y's, y's statements, you compare it in a different way. So the scope is very important. So to push scoop, we simply place the scope name into the stack. Fault form is unique identifier for function type for by the function name or F name. So since fashion then are unique and then was not the repeated within a function, you can ask worse, keep adding the scope debts when we are inserting the function. Because no 2 first shall we be able to discern them? So here let's find is used for cogeneration. So how we rename the red square function to generate Which is the major part of our comparison to co-generation stage. So there is lots will be outputting our compiled code into our competitor. Name is cogeneration. Remember that cogeneration is the last task before could optimization in competition called opsonization is not composite. As cogeneration, you can NGO comparison both to improve speed, processing power. That's why you do optimization. You remove some redone dance quotes, you manipulate it. If you're a programmer, you can add programming at that point also brought the cogeneration is the basic end of computation. Rename the red fire co-generation was we had to put data to our competitor, testify that particular stage has been compared. Now we generate our first compiled code. Then you can use the function name. So whenever you're declaring a function, you must print out the function name oscillator. Anytime you want to jump to that fracture may be deflection is called. You're going to jump to the function name. So that is why is our first code for every function before you write anything. For the next set of code, degenerate. We need the total number of local variables. So whenever I enter a function, you start companion furniture, you need to get the total number of variables that were declared within that function. I'll tell you why first Soon. We'll look chart out tokens starting from the current pointer. That is where the function is declared. We move on till we get to the closing brace of your function, that is to the end of the function. So we use this loop to check if we're going to find the pattern for verbal declaration. We didn't do loop will be checking for local variable declarations sentences. Because we need to count the two tan on both local variable in that function. Once we see one, we increment the variable counter. Before we exit the loop, we meet the token end of file instead of the closing brace. Then we'll call the functional and wonder and 28. We increment our pointer two points on the test set. Then after the opening brace and preserve our pointer using counts on, I owe them 14 versus initialized to one. Since we've passed the opening brace of the function, we wrote the incremental point and you skipped that opening increase. That is why we will initialize breast one, which is our counter. So within the loop, if we meet an opening brace, we incremented and decremented, well we meet the closing brace. Brace is equal to 0, then we see it. Now we have the two tan, number of local variables stored in the variable called variable. Now how does weeks we compile function? The first one is does, is to allocate space on the stack. Creates his initial stack frame. Talk about the stack more. Lecture. Then push all or any of the Odyssey two registers on the stack. Push out the return address on the stack. This can go permitted in the case of a leaf procedure, push the frame pointer on the stack, and then lady from point to point at the beginning of this stack frame. So why are we pushing data to the stack? Because every function has his own will cover both. It does is all done via verbose and has his own arguments. So whenever we call it function, we need to jump to that function. We jump into that function. The new function also have its own distinct arguments, variables and temporary variables that were used in it. You know what, we jumped to the new function. The new function is going to use up during these tasks that were allocated to the previous version. Whenever we call a new function, we need to preserve all these data before we begin the new function. Because once you begin the new function, the new function is going to use the same space that was used by the data function. That's five, we generates called allocate space on the stack to task basis equal to the total number of variables times 16 bits. That is true is fixed because you also want to push to system B to exist as the frame pointer on the stack pointer, pointer. So every time I went to result, that's theta2 for the two of them. Then we'll multiply by 16. Because we assume we are comparing a sys for system design. The data to bits is used for the frame pointer and return address register, which pushed onto the stack or level of force it to fall off the tree. So we assume our widgets as assets in bits each. Then allow one cell phone, use a while loop, making sure it doesn't go below that two beats men for return address or frame pointer. So in the while loop, which generates code for pushing registers into the stack, which is task, which will be used to store the variables. Then on line 44, we made the frame pointer to point to the beginning of the stack frame. For TCS. I said the number of vocabulary boots basis, we are going to use it later. If you see a function declaration is not a function, not a function initialization, then the function is terminated with a semicolon. So set the goal to 0 and increment pointer that points to the next item after the semicolon. Else we chew and error before we return point. And is it? So let me briefly discuss how the stack works. And I'm going to discuss it in lengths in another tutorial. Let me briefly talk about it. So if you read the PDF file, then you see LD and SPO as D used to move data from memory and retrieve data from the memory we use node to get data from memory. I will use SP, which is this top, this data into the memory. So all the costs are all bytes where load word or half. The only difference is the size of the data being stored. So you have S before stork bites. You can see load half for that is that they have good word. So those means size, the man command or the load and store. This. Since the stack is also on the memory, we will be able would also use LPN to pop and push data to and from the stack. So assume this is your stack, where n represents an arbitrary offsets c 0 star goes from top to the bottom. That is, when the stack is empty. So when the stack is empty, the stack is pointing at the top. And 571 is full inspection at the bottom, which is at n. As we want to push too, which is task X1, X2, and X3 to this dark one is empty, that is, one is pointing at n plus 57. We first of all, points are stuck with it. Start to S p minus three. So I'm going to subtract two from our stack, which is first, first thing we're going to do. So if we subtract two, it becomes N 257 minus three is equal to n into 54. Stop and stop points, nuts. And 2.5D for that is, you subtract two from the stack register. Now it's 3.2 n plus one rule 54. Then we'll start our che that are using the command at the address SP plus one, SP plus two, and SP plus three, which is, which we give you any 25 to 55 n vertices and n 257. So these are the places where data will be stored. Now are stuck with this ties to pointing at n 254 without data in D5 and D6 and D7. Now we want to pop to it, starts from the stack, are going to use the L D or command on the address. We first of all, unlike before. Now I want to first of all increment our stack, what we want to pop, it'll increments it. So you know, it was part of that n starts incrementing by 123, N plus 255 and plus two and plus 257. Then after popping it, we point the stack, we start to address SP plus three, which is n 257. Now on the stack is back to being NC. So now we've seen how the compiler compiles function declaration and initialization. This tutorial, we'll see how the compiler compiles. Is it from function. Hopefully you understand how the stack works from this, my basic explanation in future tutorials, C-SPAN is more. 9. 8. introduction to parsing: Welcome. In our previous lessons were learned how the competitors and our code for tokens code with develops of congenital be referred to as the tokenizer or lexical analyzer. In this lesson, we will add articles into a file. And in future lesson, you assemble the tokens into sentences and compile them. With the help automate Token function will be able to copy out tokens and output them into a fan. So declared him to confession. And above I n phi function is implemented using polymorphism. Tokens without data and tacos with data. You initialize and make a taco function will elect to do two things without token. Relax to add, put it too close to a file so you can visualize them as secondary. We also want to compare them. So to visualize them, we need to add them to a file. The help of the red F5 function. This function we could say file and copy out to concede to them. Secondly, for competition to compare them will use the Send to Bus IF function descend to pass a function we send out to constantly passer for competition in parts of this course. So just call them random variable. We use this variable to index our tokens in the output file. So we initialize it to one. So since starts from line one, we'll also declared the read file function. Takes as argument the data to write to a file. Whenever we're done with tokens. We started to come in the previous token variable, which we discussed about in the last tutorial. Use it to test for function colon. As we did earlier. This string will consist of all the token data we want to read through our output file. So the data's include line number, which we've created, equal by line number, then the token type. Then finally the line on vinyl called Phi wedded to come up here. And finally the token data for tacos are stored, data are separated, all of them with the dog boy. So whenever I write it to contract and fire, I'm going to increment our line number ten for the pasta. We'll need on this. And as arguments, it took on type the line number of the token. The token appeared, and it took on data for tacos at store data. So here we'll call the right five functions, right, our data to the output file. This line number is different from this area index in line number. This one refers to the line number in our original compact file where the two concept here, while this one is just SCI index four outputs now to consent the output file. So now let us initialize our addFive function. Just like we did when we copied our data the beginning of the course. You're also going to copy our data from our ad agreements in through the chat I record array. We first get the length and create a char array of the same size. And using the string copy function, we copy our data into the char array. The name of the output testfile will be to conduct test. Next, we need to delete any copies of it before outputs in our tokens to it. So we're going to achieve this using the delete old variable. We initialize it to 0. Once we delete, the file will increment this variable by one. So this prevents the IV plot from running a second time. If we try to open our talk on the test file in read mode. And if it's successful, it means that the file already exists. So we use the remove function to delete it and create a new one on line 25. And then we open it in append mode. Online to NTC's, we start appending are two calls to output file using the methods and also add a new line after outputs in every token. And we close it back online swings. At this point as Canada is now complete. Now next lesson, we'll start computation. Before that, let us initialize our central plaza function and arrow function. Then we'll count and example dot h phi phi, which we included in our scanner, declared a Send to Bus f function and error function. We also have our two lists. Before we run our test file, let us first comments all of them because we have no created the Fed out to initialize them. Then we'll go over to our scanner, command this into parser function and paste our error function after the test. And we're going to delete the error function. So it's going to be called from the passengers each edify. Now you move our generate function to be just below the alphabet function. Test or C plus plus file. We are going to run this sample code here. In my command line. I'll first navigate to our project folder. Then I'm going to run the file. When I open up to testify. I will get this. On the first column is our CIA lumbar, which we included in this string code. Then the next token is our token type, which is displayed as numbers. Then the delimiter, which is the top wife in unknown. Then the third column is the Rhiannon bus, where they appear on our code file. Then finally, to consume data. Since our token tab enumerators displayed as numbers. So to view them as string, we need to create an array. We can name a string, position or the index. Let us create one. In us Candace C plus plus file. I've created the array named token names, consists of all our tokens at the enumerator index nest in our mug to confirm it took on tab with the arrow values using the token type as the index of the area. Now, when we run it again, we're going to get this. Now you can see that we get the list of articles and not the norm pass. So you can run your own test program, but measure is well-written and only consists of two only now token-based because you have not included or that error checks. And our passengers age or comments the error and send to pass a function, loss function. So this, you come to the end of part one. And in this course, we studied actual computation of tokens. So once we achieve, in this course, we've been able to break our code down into tokens, is basically need to corn, we're going to start building this sentence, which we are going to compare impacts of this function. So make sure you understand every single disease of battle on. Before proceeding to more tougher, we're going to meet a lot of function. And the competition takes process here. 10. 9. setting up parsing and semantics analysis: Let us begin by comparing function declaration and function initialization. First sentence. Remember the function declaration and function call is much by variable type. For a function to call. Example. Variable types include token to conform is a function declaration to come to conform, to convert for better conform to constraint. So in this enhances or C plus plus, we're going to define the sea to conform function, which is the function that gets called whenever we meet this anguish combination. So in acetylcholine function, which compares function, I'm going to take four steps. We have also add our function to the function list. Going to add our function arguments to the argument list. Then I'm going to compile function is. And then you're also going to push function to the scope. Faced with degrees C to inform function. The argument point I is the index or the function type in the token table. Now, before we add a function differential is measure that this function does not exist already in the function list. So to do this, we're going to set index is a verb when assembled the brand also the index to our function is going to use index ii as index to the function list. So we set it to fruition index to preserve it so that you can show our function list. So we subtract one because we increment our index by one whenever add an item to the classes. Ness to increment pointer. Now points to the function which is poking fun. Now using a wire. We compare the function name in autocrine. Functions in the function dist class. Match is found. We drew an arrow. Here. Check for functions that are being initialized using the coal variable. Whenever it is covered with 0, it means the function has only been declared, it has not been initialized. If it is one, it means the function has been initialized as well. So you can declare it again. Then nest we declare the global variable, which we start function name and assign it to our function never lived 70, It's called ethylene. Now we add our function to the function, the function name and initialize other things to 0. And also increasing deaths nets to add our function arguments. Before we do that, we need to set our function arguments. That index is the coins. Arguments in this function list parameter is indexing the argument is glass, where the arguments of that function stats. So first of all, copy that index because that's repeating this way is first argument is going to start. So a copy to differential is before adding the arguments in this from this and will be used as the desk. So our pointer is currently pointing at the function name, increments its default style. So we will move the pointer count up by two points on the first arguments. So it's going to now skip. If forced, less brand. This is opening parentheses and then jump to the first item within the function, parentheses, which will be the first argument type. This is if there's no arguments. So if at this point you get a quiz in parentheses, then it is an NCI arguments presented by using counts. We set agreements non batches because we need to count the number of arguments, variables so we are resetting it. Therefore, if the next token in our table is not closing parentheses means the argument list is not empty. So we initialize argument number one. Now, the number of arguments, the number of arguments is, it prevents too. The number of chromosomes separate us within the parentheses was one. Which really sets Agamemnon Botswana. They are one comma, two arguments, if you count, comma is mu agonists and so on. Next, we need to count number of commerce in the argument list. One emits the quiz in parentheses. Nancy, for the nonprofit arguments to the argument number. And online Eighty-five, if there are no arguments, we simply increment pointer to point to the next corner equals in parentheses. Using a group that's setting the arguments, the arguments table. The parameters are acumen type of pointer for by the argument's name of pent-up plus one. And finally, the arguments number, which we increment whenever adding agreements and resets. It's at the beginning of the function. So the index will this argument number only save the index of that particular function, the cell with the lookout when we look at variable previously. So A101, we increment pointer. By this, we skip the argument type arguments name, and it terminates in coma or final quiz in parentheses. After they won arguments, you convince the battery size so that you can skip that part. So coag humans is stagnating comma and now points to the beginning of the next arguments. Again. We skipped by 3 again to the beginning of another agreements. We add it to the argument is skipped, begun by three, limits, the closing parenthesis. Satisfied once we create our scope counts and scope. The scope that when is that's in scope. Scope name, unique roles in statistics. We're going to add this as a prefix or suffix. The idea is That's if we added an increment, this scope depths means that everyone within this group would be unique. If you have two statements. The first one is name. If there is one, that means if one. So DNS, if we're not as if to the scope that we make sure that everything within this group have a unique name. We attach it to this opening comments. It's guessing you add it to the next school. Or that scope. We'll have this. In days of school debt. The major syntax difference between the function declaration and initialization is that function declaration is terminated immediately with a semicolon. After the arguments. Function initialization starts with an opening brace. Next, one says, which equals opening brace semicolon. Then it is a function declaration and initialization, not just declared declaration. We set the goal to one function and you set to 0. This means that this function declaration only as we're going to do it as soon. And if the function is initialize them, push this. We need to put the function into the scope. So anytime we assess the top of this group because the scope is also a stack, we always find that that's where inside the function. So that is the major reason we need this scope. Anytime we check this top row is fine. That's yes, we are we doing this? Suzanne says to function. So that is the major reason we need this could count to let us know. It's useful. One will be terminating, one will be as it's in this group. Because different schools have different angles to terminate them. Know that this is an if statement that I'm terminates in F. I'm going to combine it this way. You need to know that these are wise, whilst it's meant to compare it in a different way. So the scope is very important. So to push scoop, we simply place the scope name into the stack. Is unique identifier for function type, the function name or F name. So since Fashion and are unique and then what's not be repeated within a function. You can ask us keep adding the scope debts when we are inserting the function. Because if I shall we be able to discern them. Here, right? Find is used for cogeneration. So the read file function to generate, which is dementia part of our comparison, the co-generation stage is what we will be outputting our compiled code in our competitor. This fat name is cogeneration. Remember that cogeneration is the last task before could optimization and competition called opsonization is no comparison at co-generation. Can NGO comparison to speed up processing power. That's why you do optimization. You remove some redundant codes. You manipulate CCCI programmer, you can add programming at that point. Also. The cogeneration is the end of competition. Name there S Phi co-generation. Once we add puts data to our competitor testfile, that particular stage has been compared. Now we generate our first compile code on line ten, which is the function name. So whenever you're declaring a function, you must first printouts different name as the labor. Anytime you want to jump to that fracture may be the function is called now going to jump to different name. So that's this. Why is our first school for every function before you write anything. For the next set of code, degenerate the total number of variables. So whenever a function you start combining furniture, you need to get the total number of variables that were declared within that function. I'll tell you why first, chart out tacos starting from the coins pointer. That is where the function is declared. We see, we get to the quizzing base or differential, that's the end of the function. So we use this loop to check if we're going to find the pattern for a variable declaration. So we didn't do we be checking for new cover predications and tenses? Because we need to count the proton on both local variable in that function. So once we see one, we increment the counter. Before we exit, the loop will meet the token end of file instead of the closing brace. Functional. And one month since it will increment our pointer two points on the next step after the opening brace and preserve our pointer using counts on my own and 14. This is initialize to one, since we've passed the opening brace or the function, we've already incremented point and keeps opening brace. That is where we initialize breast one, which is our bruce counter. Within the move. If we meet an opening brace we incremented and decremented wearing Embrace. Unsealed brace is equal to 0. Then we add it to turn on both variables stored in the variable called variable. Now, how does this compare function? The first thing is does is to allocate space on the stack. Creates his initial stack frame. Took our part is dark more by honest lecture. Then push all or n of all the CO2 registers on the stack. Push all the return address on the stack is can be omitted in the case of a leaf procedure, which different point on the stack. Then lady from point to point at the beginning of this stack frame machine that has a stack, because every function has his own local variables. It does is all done by verbose, has his own arguments. So whenever you call a function, we need to jump to that function. Jump into that function. The new function also have its own distinct arguments, variables, and variables that were used in each one we jumped to the new function. The new function is going to use is tasks that were allocated to the previous function. So whenever you call a new function, we need to preserve all these data before we. Speaking the new function, because once you begin the new function, the new function is going to use the same space that was used by the load data function. That's five, we generate squeezed as allocate space on the stack. Space is equal to the proton on both variables times 16. States. That it is fixed because you also want to sue system betray just as the frame pointer on the stack pointer. So every time we reserve that status, so for the two of them, multiply by 16. Because we assume our components for system design. So detached two bits is used for the frame pointer return address register, which pushed onto the stack or level 4243. So as you might read this as assets in each cell phone use while making sure it doesn't go. That's two beats. Men for return address or frame pointer. Which generates great for pushing us into the stack. It was just as adhesive, which is task, which will be used to store the variables. Then allow for T4, we made the point that two prime. So at the beginning of the stack frame for TCS, I said the number of recoverable two basis, we're going to use it later. Then if he's the function declaration is not a function, not a function initialization, then the function is terminated with a semicolon. So set the goal to 0 and increment pointer to point to the next item after the semicolon. Which one error? Is it? A discourse how the stack works? And I'm going to discuss its immense in another tutorial we'll briefly talk about. So if you read the PDF file, C, L, P, and S, S T U smooth data from memory and retrieves data from the memory used. To get data from memory. I will use SP, which is stopped. So this data into the memory, all the bytes may need half. So the only difference is the size of the data being stored. So you have S before stork bites. You can see with that is they have this means size where the main commander and distort this since the stack is also on the memory. Does we'll use N, P and S T to pop and push data to and from the stack. So as soon as this is your stack, where n represents an Detroit offsets. 0 star goes from top to the bottom. That is when the stack is empty. So when the stack is empty, the stack is pricing at the end, 57, the alarm is full inspection at the bottom, which is at N as well to push just as X1, X2, and X3 star is empty, that is, one is point sets and plus 57 points are stuck with it. Start to S p minus three. So I'm going to subtract two from our stack register. That is first, first thing I'm going to do. So if we subtract, becomes 57 minus three is equal to n for our start and stop points, nuts. And 2.5D for that is we subtract two from the stack register. Now it's your turn to the n plus one or 54 star j that are using the SD command at the address SP plus one. Sp plus to an SP plus three, which is, which we give you any 25 to 55 n vertices and n 267. These are the places where data will be stored. Now has taught me this time, step n for n 255, n vertices and n 672. It starts from this time. I'm going to use the L D or command on the address. So first of all, like before, now 11, successful for increment our stack and pop it in, increments it. So last person that n starts incrementing by 123. So we get n plus n plus n plus 257. Then after popping needs with time, this TAC meeting starts to address SP plus three, which is 257. Now on the stack is back to being in C. So now we've seen how the compiler compiles function declaration and initialization. And unless tutorial, we'll see either compare compounds. Is it from function? 11. 10. What is a parser ?: Welcome. In our last tutorial, we'll learn how to compare comparison, function declaration and function initialization. Note that is different from functional core, which we're going to learn later, colonial function. Now in this tutorial, we're going to learn how to compare handles variable declaration and initialization. Syntax is shown here. A variable type followed by an identifier, marks a variable creation that is called the var declaration of function. This function we're going to accomplish this task. First one is add the verbal to the low carb. Then if the verb is not time natality, semicolon, we call the respiration phonation. So you know, if it terminates it with the semicolon, it means you just declared the variable u d initialize it. Or if we initialize it, that is when you add was saying to eat or whenever you added any of the extra computation or calculation to eat. You call their special function. So the aspiration fresher and those, every single competition is not the standard. That is, if you didn't see the semicolon, call the aspiration function, special function, and those areas competition. Let us use them later. Our local variable is identified by its function or scope. So we use the track F name to track when we enter a new function. So whenever any new function, you know that the F name or function name is going to change to the new name. The truck F9, we store the previous name. We detect that the coin sname is different from the previous name that is saved, therefore is definitely a new function. So whatever NTNU function we first, the first variable star means Acute. This block is only executed for the first variable and every function knots with them, no, because after the first variable, the next variable that's when you will start with DSM-V name. So this block is not going to, is acute urinary going to be executed once when you are adding defecit variable of every function. What it does is to start a new function name online 162 on t changes again. There will set the local variable in Desk to 0. So this is index is reset once we are in every new function is used to assign index two variables in the function. So that index is used to assign which he starts to them, just like we did for the IQ minutes. So once we need, because all the variables within a function is going to share the same region. So that is the saved register. So whenever we add a new function, we're going to set these loci VRB index to 0. Then whenever the function starts, whenever the variables are left function start coming in, then this and incrementing it. If you get to a new function, we reset back. This index is reset once in every new function and is used to assign index variables. Then the alien deaths, which is equal to the index in the local variable list. We add the function variables stats. So we use this gene is such. We need to know what index does a particular function variables task? Because there are times you want to set a variable within a part square function. Was that such India who have a Buddhist. So we need to go to that function, get the in-depth way Stokoe verbose task. That's where I go to stat on set. So instead of searching the whole local variable is we start to such at that particular index. So it's called the L index. And we are going to store it in the VS variable of the function is. So anytime wants to such a variable within a function, a local variable, we go to differential list and get the value of d v s. So that is the index position. We're going to start counting within the local variable, this nest. When the scope is empty, it means that the verbal is equal. Above, that is Guba verbose or not within any scoop, the ad defend ourselves every function. So that means this cope with BNC. So whenever this group is empty and we want to insert the variable, we are going to insert it as a global variable. Or whenever we are within any scoop. It means that a variable is a local variable. So because only go by verbal do not existing is cool. Else, once we have this scope, it must be, it will have one system. Now, we need to sash to local variable list to know if the variable has been declared before. If it has been declared which one error? Such from the L, such which is the local variable list, to the alien desk, which is index or the first reopen the function of index is equal to 0. So the l such that whenever we are searching for you, fortunately and certainly a function, sorry, whenever we certainly a verbal, we are already within this function. So we are going to start from that index to the last variable that was entered because blue is likewise in that path function. So there's no need to get where we are going to stop. We stop at the last index. So that is why we are sending it to L such a, such a successful and no match found inside the verbal integers. We'll cover Buddhist using the variable type at index 0 into a variable name at index point ab plus one and the health index, which uses index within the function increment both the luca index and the minIndex. Therefore, we need to complete for adding variable includes. There is a group of local variable. Check whether the river has previously been declared. Checked which function the variable belongs to. An essence you are non-biased to the local variable within the same function for which this task assignments. Now we've added the variable, then we need to check what is used to terminate it. Before that. If the scope is empty, then is a group of variables. So we preserve the Guba well-being index without such and such the whole of the variable. If no match is found, we simply add the variable to the list and incrementing decks. Okay, so we need to check what dominates what does this semicolon or wet or something else? If it's a semicolon, you simply increment pointer by two to point to the next item. After the variable declaration house is a comma. It's mu, that is the multiple variable declaration. So example, you have integer song, coma, add comas. Soapy means you have two integers that you just declared. So in this case, we need to call the variable declaration function again to add the new ones. I know that the verbal tap no longer follows the second variable that is after adding in song. And you find out that in essence that comes after it is a comma. That means you need to add int, add, also, go now, before the ad is a coma, you no longer have the verbal type int associated to it. So what you need to do at this point, you are still pointing or song. What's the need to do is to copy the verb comes before song and replace it with whatever to conduct calm before AD, which is the comma. What that is what we did on since we are currently ongoing idea that variable, that's this song, we simply copy the previous token, which is the verbal type a comma. Now we're going to add in some ends at comma soap. Then repeat the same procedure again. Then we add the adverb will repeat the same procedure for sub I is going to become N sub n. At n sub. I don't know if you get me, so you keep repeating it until you get to a semi-colon that needs to run. Or maybe you have the assignments. If you have to connect there, you can now coexpression. Repeat this until we are done with the whole verbal. Least. Else in the verbal is needed terminated with a semicolon. You as soon as an expression that says ample count plus plus counter equal to Petsko value. So the expression, we solve the rest, we hold a special function here, 200, return the new pointer. 12. 11. How to create classes for semantic analysis: Welcome to part of their hands-on compare design from scratch in C plus plus. When we design the lexical analyzer which generates tokens, Landau to Openness C plus plus file, copy its contents and its contents to talk about. So we are going to design a syntax analyzer, parser and semantic analyzer. The syntax analyzer recognizes and dense in the program using the syntax of the language. And semantic analyzer checks static semantics of each constructs. And finally we perform code generator and outputs are compared to file. The co-generation. Repeat the last part before we are compared data. Now I'm Before we begin, we need to start tokens, which makes two different kinds of compilers as to compare where the bizarre request for tuples from scanner, scanner scans to Kuhn and sends the bus I immediately. Our design is the second type where you scan the whole tokens, you store them and then they start passing down one after the other. So the difference between the two is that for the first one is safe space is used if you are, if you are designing a small competitor or maybe for a small program. So we add there is no much need for tokens that are far away. You can use design where the bizarre request tokens from the scanner and this can send both our design. We're going to scan all get to cod, end of file befalls that person. So before we begin, as I said earlier, we need to start to descend to pass a function with style generic tacos until the scanner confuses and process before we can start passing. I spend its area. So we either store the glucose in a class or a stroke. While prefer classes is going to hold large datatypes. Go most comparative cell will use this drops. Yeah. Okay. Underclass who quit to have them as well, add them or 10 thousand tokens, we can make it unlimited or maybe lesser. Had that five. That has a class or two contrast to start to contain it does well, we're going to stop all our tokens until after the scanning process. Then we're going to start passing the token list. So we used to construct us to store data into this class. One for two columns with data and Yoda for tokens without data. Remember, Next we're son is what took on that or do we start here? First, we are going to store the token type. Necessarily going to start the line number, which will be used to find the land on which error code. And finally D to clean that out here, which is referred to as the identifier. That is, to come, the data may be a function name, a variable name, arguments name. So those are the data we're going to store a string variable and so on. So subsequently, there will be a lot of other classes that we need to keep track of who secretes more classes. So I decided to initialize all the classes in a common class called symbol table. So here I've defined that as the friend class to our token class. Let us know undeclared the SymbolTable class. In the symbol table class, I've created an object of our token class called to contain the global variable called max, which is the total number of tokens. You can still have shown you that before. Next, we set the index of our token table. And using a for loop in the symbol table, we initialize the token table contents or no. So great to more. Samantha's does H, semantic does C plus plus and C plus plus, which will be used for semantic analysis. So in our semantic does C plus plus file, the header file so that we can access our two companies and also include the somites. Either H5 nest here is the error function is going to be declared. The arrow function, which we'll call the scanner function, is going to be declared in the semantic file. Because they're definitely going to be a lot of error. So all we need to do is when we meet an error, we'll call this function with an error statements and the random, but where the error code is, we simply output the error, why indicating the line number. And after that it will resist. As you can see, takes two arguments, the error and the error code. So we simply print them and easy to use in one nurse to need to create a firewall will store our comparative data. So hopefully the same array X5 function we using this candidate or C plus, plus, It's integrals. So I'm going to use the same type of file to our outputs compared data. We use these during the code generation stage. I'm going to change our ad, find them from ratified to cook generator. So the delete old is also used to measure the data at once. At the beginning of competition, we did this when we discussed the scanner. We initialize our symbol table. Now we're going to refer to all these friends class using S T, which means symbol table. So any data from the symbol table we are going to use, we preface it, S T dot the name of the data that we initialize our central pass a function. Observe how the Senate to pass a function of this, our token into the token class. Whenever they make function calls, It's made sure we increment the index immediately after imputing data. Next, we initialize our compile dot txt file. Whenever we get to the taco end of air, we need to add the four into a compact test file. So this will tell the assembler That's what comes next. Next, which is our program wants to combine. These are standard assembly language initializer for every function or fairly wants to combine. Then on the second line, you have a stem print health, which means that the print function is an external file. You are going to call the print f from an astronaut first, you initialize it with a stamp print f If they are called function. So inbuilt functions which are not declared in our scripts. So we need to include them like this using this time keyword. Print F represents our c out version in C plus plus. So anytime we go see how you reference, print F in an external file, then the group who does the opposite of Stan, the compiler use it to show that the main function is a Guba function and it can be called from an external file. So the difference between this ten and cobra keyword here is that you use this tail for a function that is in another file. Again, that file that for sure will be declared as Oba, meaning that it should be called from other fast. So that's the major difference. So as soon as you send the last token, which is the token end of file, we print these three lines at the top of our file. That Gabo Fernando. So we printed here. So what do we do next? We start the person. You need to tell the compiler to start passing immediately wished to the end of two, switches it to the end of file. Once we add it to go and offer to article, this article table, we start passing. The token to offer does not have any data. So that's why we added in dissent to pass faster, which does not contain data. Then nest before the arrow function will initialize some helper function. First day spec function is implemented using polymorphism. We use it in our process semantics to check for beta1 and now to retail for patterns of four to two. Finally, warm consequences to cause in autocrine list. It takes as arguments they're expected to cool, and then it's indexing the two contains the indices labor counts here. Why did took was a Lego type a, type B, type C, type D. Depending on how many consequences to cause is ticking, for example, for one to cool, his arguments is expected to cool. Also referred to as step in the argument. And index-based expected, which is counts. If compares expected to come with Astra to Canadian desk in the token table. We check for two concepts, two tokens. We use as argument, the two expected to consider that a and b with index of the first to rebuild counts. Then compare them. If they're the same as returns true does force. A semi algorithm is used when it returns three arguments and arguments. In this lesson, we will describe the help flush out professions which form the basis of our lectures. In part two, we'll discuss the buzzer. 13. 12. Read these attached files: In our last lesson, we declare some necessity of classes and a helper function that we assist us in part two of this course. This tutorial, we'll introduce the bursa. The bursa is going to scan our token list for correct sentences. Who declared the parser function, which was called with an argument of one in C plus plus file in our last tutorial. So immunity we send the last token, which is token end of the path function is called any stats passing from the first token, least on T guests today, which is also the two quanto file. So it will loop through the whole token in our two colonists searching for codecs. And at the end one in yesterday took the end of that is when our computation we are terminates. Or sentence combinations, the while loop will go out to call this Test in full sentences. This loop, as you see, does not have an incremental but at the end rather is going to cause a lot of sole function, which we take as arguments, two coins, index of two consented to con list 100 ton, disseminate this incremented function. So examples of language sentences, the first one will be function declaration and initialization consists of a function type and if function name. The passage checks if the function type is fluid bed to confirm using the aes function which we described in our last two. Yeah. So here the function types include tuh kuh, puh, tuh kuh void to convert string to come to contain. Here. The index of this function type is the pointer, which is food by to conform, whose index is point a plus one. So if there's a match, we'll call to conform and assign our token index as arguments. And it will return a new incremented index depending on how many tokens consumed. So the next one is function call. So since functional costs do not have a tab directly alongside it, so whenever the pointer points to, to conform, shrunk or they'll call the special function which will return a new implemented pointer. So for function calls, we call the expression for Fortune nest for variable the coercion to declare variables. So whenever we have the three variable types to cover, two constraints are two cuckoo followed by an identifier, then it is a verbal recursion. Next scenario, oppression will discuss about the lead time, the semantics. Here whenever we have ternary now scoop counts. We're going to introduce Lita, who called it an arrow function, which takes our pointer and returns a new pointer. Nest the prince. In C plus plus statement is marked by two currency out, who will pay shift to that? So call this C, C out function which returns a new pointer. Then you have the liberty curation. We declare labor when an identifies followed by two cocoa, we call the syllable function. Then you also have your loop while, loop. While loop is declared when to come wire is fooled by left parenthesis called the CY function for loop with the clear for loop function when to conform is followed by a left parenthesis, we call the function. Then you also have your return statements. Whenever we have a two kilonewton, we call the return function serotonin function. Then if statement. Whenever we have an if statement followed or if token, full band opening parentheses would declare new statements are called the C4 IF function. The IF function which is subpath or the if statements which is marked one to S is followed by token. If we call the CL statements, they have the else statement. So this is the default path of every IF statements. One else's full band opening left brace, we do not do anything. We simply increment the pointer by token to skip the two tokens else and the opening scenes is the default statement fulfilled if condition so we do know is a cutaneous special function for it. Simply skip and continuous equation. Then you also have the continue keyword. The two can continue food by Douglas semicolon max, the continue keyword, and we call the sequencer new function. Then what you have is singular identifier. We call the expression function. Then for switch statements, when we have it too, can switch, followed by open parentheses will decrease. Men, so we call this C switch statements function. Then the case statement, which is also part of this three statements, is marked by by the token case. So we call this C underscores case statement. Then you have the default function, the default statement. The first statement is equivalent to the course into quantiles of the if statements. So it is the last closing part of his statements. So we call the C default function. Then it seems you have the break keyword. Weeks. I have one that is in the switch statement. I have the normal that is used to skip the loop. So the first one is the grid that is used in S3 statements. So we use it to terminate cases in a switch statement. So we also forced to note that it's appeared in a switch statement. We check the scope. If we check if the keyword is in this group, this nebula is differentiated from an outbreak, uses it in a loop. So we call the switch brake function. I will introduce the concept of scope in a future tutorial. So if you are within the Swiss scope and we find a brick we didn't need. That means is the switch brick used to terminate the keys. But if we are not within a Swiss case scope, and we are seeing then is the brick that is used to terminate the loop. So they are 19. The 19th is the break keyword. This break is used to terminate the loop is much better to break followed by a semicolon. The last one is the right brace, which is used to terminate scopes. Function if statements, we statements use developers to terminates key words and sentences. So right brace or closing brace. I used to terminate a lot of scopes in this course. So what does clubs set of conditions bounded by the opening and closing brace? Examples include function is initialization while loop, if condition, It's meant for loop. So the inclusive block of instruction and are usually declared with opening brace and close rate equals increase. Whenever we meet the opening brace, we record the scoop. And whenever we meet their cuisine, this visit, this group, discuss more in a future lesson. So these are the trends. The sentence is the Merkel power gamma constraints more with CPR dead. How add more into semantics. As you can see, you can also include more on your own, on your own design. So there's no increments are better at the end of the loop. Each of the called function returns an incremented pointer. So you get to the taco and warfare. So there is no special point up plus, plus at the end of this loop. So once you call any function is going to increment the pointer inside the function and then return and incremented number already. Once we get to the token and offer, study them carefully. We discussed each function in the semantics and code. 14. 13. How to compile function declaration ?: Welcome. In this lesson, we'll start this semantic analysis. To be done before we begin. This point we've created so far is similar to C plus plus and semantics as h in our semantics. So teach failure. We're maxim, if non-defined to prevent multiple fame collusion, then we declare our function and include pasado, th, and UK or the functions we call it in C plus plus. Who declared those functional? Because initialize them in the semantics or C Plus Plus. To create a spec function and arrow function also declare two variables as they include our friendly class. Symbol table S T. T. S T would be is to refresh every variable within the class. Then the next one is the token names we used to print out two canals in our to conduct this fat in parts one. And finally, I'll scoop counts. So they are all declared as external so that you can cook them in, in the plaza. And also anti-Semites is a C plus plus file. Our scope counts is the stack. Gas doesn't shrink. So whenever n is Coke, we push this Cook name and when we see the scope, we pop it out. This will be helpful because some operation is only limited to a particular scope. So before we execute such an operation, we need to check if we are within derived scores. So useful. Again for branch instruction. That's most branch instructions jump to the end of the file or end of the scope. So we need this group name, so that should be able to get the address of the jump location. Now I'm back in our header file. Will need to store more variables. This would be useful gene resistance assignments and take you for variable type. So go facing out to companies to include two more variables. They identified type for identified tacos and register them for tacos, The nervous system. So we're going to use DVT Stan them. So as saying that just adds to the variables. That is when those variables, we'll let that be called storing them in a register. And likewise, we need to know the type also. So we're going to stop for more classes, making it a total of five classes. And they include number one, the function list. So this will be used to store this stuff, all our functions in our compiler file, Nest did local variable. So this class restore list of all we'll cover both in our file. Then you also have the Guba verbal list is class to store all the Koopa variables. Now quote, then you have the last one argument list. So we use this to store all the arguments of a function in R combined with the token, least, making it a total of five classes. In differential list, we need to store six variables. Then the name of the function, the number of arguments it as number will cover a brief task. Then the indexing the argument that his class that's in the software, their arguments in the argument list where the agreements Status, Index and the look of a bull. This class is to cover variable stats. So you know, every facial has his own arguments and local variables. Why the argument list look of a Buddhist view consists of all the variables and arguments. Will need to be able to know the index where we started. We just send the arguments, Feedback Sokolov function in the function list. And these arguments in the argument. Also be useful gene such sites if you want to check if the variable comes from a particular function and check the whole list. You can select, go to the function and get the index variable stats. So now start searching from that particular index than searching the whole list. Nest for the local variable list, we start three items. Variable name, the variable, type, and index of the variable we deem is function. That is, is locating decks that is within a particular function. We give each variable is CN number you can use to reference it. So use this serial number to assign which is the local variable doing co-generation. For the Koopa verbal least, we store only the variable name and the variable type. Since the grouper verbose asean to belong to a particular function. That is, function body belongs to a particular group. So if you are going to satisfy Guba, we're going to set the whole list. Then finally for the argument is we start to variables. The argument's name, the argument, type, and index all the arguments within this function which will be used to assign these tasks to them. We create an object for all the classes in different class symbol table. Then it is too often tests we use as index for the classes. They're using a for-loop. We initialize the class data to 0. So that is how to pass a head that failed looks now less than our semantics. Or C plus plus would declare all the registers and commands we need in a string array. So for ours, we store for different kinds of resistors. Have deceived area. We store all alcohol beverage is that they're both G, It's true G7. So all the Guba verbose to be stored within these set of registers. You can see that registers are just Alphabets, Alphabets, letters. So, but we did the memory. The memory will be able to assign those tasks to a particular fiscal. Within the memory. Then you have the arc v, which we store all our arguments, variables, suits, their belt extends through acceptance in this area. This will study saved verbal rages delivered X9, true excellence seven. Then you have the temp V area which is all temporary variable register using, we should be using gene computation is labeled X5 to x. That's one. Use the SVD stars Jim completion to store different kinds of variables. We also have ADA standard. The first one is X1, or so-called the return address register. Register to store the return address. Whenever we're returning from a function, it is told the name of the function are going to Johnson. That is the labor which is fixed at 0 referred to this resistor, whenever we need 0, value is 0, F P, frame pointer SP. This is a stack pointer, pointer nest the insertion. So this area of arithmetic instructions in order of precedence according to their position in the Tacoma and on list. Starting from two could add assignments up to to construct. Then finally the branch instruction, fetching from to cool it off to two combined gable. I've also said w 0 because the logical OR and took on logical and how much percentage in compression. So the branch command that is steady in the opposite, which I'll explain later when we are using them. Now everything is set. In our next class, we'll start comparing the semantics. 15. 14. How to compile variable declaration: So before we begin the semantic analysis, I've added a PDF file which are composites review. So as you will be able to flow in this session, but first this week card, you need to learn years only do this stuff. I would submit ecological operations and the description. So these conditions and oppression or core or the earlier using the two tasks are s1 and areas to the other column lag function 347 will be necessary if you are designing the assembler. We don't need them for now. Nest. This chapter is very important. This particular one, this PDF, is he teaches the architecture and how it is most important is that you learn how to use the stack. Also spin it along the way. What this chapter is just to warm PTC is short and concise and advise you to read this so that you do not get lost along the way. It's a very nice PDF I use. It's helped me to understand that beverage is thus more stack and how this code compile one valid because if you get out to compare it to manually, then designing the comparator will be easy for you. But if you can picture it in your head, the code to be hard for you to understand where if you can pick out the one hour is done manually with pen and paper, you can be able to design it in see easily. That is how I learned how to treat someone had in my head and I was able to implement it in code. You can find out that calls simple if statements, not complicated codes. Because the coating was easy. I used my initiative from what? I saw Atlanta to do it manually. That is compulsory for you to design a compiler. You need to know what you're going to design. How was I going to do then is our ability to understand how it works manually. So the compiler is, your compiler does n is going to automate the work for you. We asked them not to do it on your own fears that is the manual way before you then how to design the Compare that you are permitted for you. So if you don't understand it, you can design it. So these PDFs are very important that our CSP, they are within the vector, but they're too big to put as part of the lecture. That is why I get them as PDF and diverse shots. You read this, we didn't need the key. Then the next one is Chapter sees. This book is an extended version of chatbots soup. But be careful, Josh, learn how the combination works. You don't need two aspects. The most important then in this PDF is so that you understand Manoa comparison. Because you most know-how compassion is done manually before you'll be able to descend, the compiler. Compiler is just goods to automate the competition for you, for you tell it what to combine or to write the co-generation instead, Tootsie, to see what's called wants to generate how the instructions are going to be orange and so on and so forth. So there's a method this is torsion, we aren't in this course will be different in another. So it depends on the comparator you are designing. We are using the Weeks v assembly. That is the assembly language we're comparing for this standard assembly competition or MAP. Lest. You did three pdf Phi sent me a menu for fan and impacts confusing or you want more explanation. Thanks. 16. 15. How to compile function exit: In this lesson, we will see how the compiler is itself a function. The function block is terminated with a closing brace. And in our passage or C plus, plus, we called a right brace function. We'll compare it in the semantic or C plus, plus. Lot of photon scopes like loops and conditional statements or terminates. We take closing brace. So we find out, is it loose in the right breast furniture? In a subsequent lesson, West pander functions audits are task groups. When they terminate. The first time we turn this function is that we increment the pointer to point to the next item after the course. Now since a lot of scope, because we divide, we need to check if the right belongs to the function scope. So we always check which of these scopes or does it belong to. So to do this, we take the top of our scope counts. If it contains a steric phone call because that is what we add as a unique identifier for every function that we assume the cuisine brace function is it. Sufi is not asterix func. That means it belongs to another scope. So if you see a y scope, we're going to attach Hawaii to eat while we're pushing it to the stack, this group stack is eating, we check the scope. What does it contain? A container? If there's a container, why does it continue to function? So easy to separate functions go from other scope will use. Now if it's a function scope, we are going to now pop this scope. So I use the string M, N to check for if a word contains another words. So you can take it on. This link is from gigs for geeks.org. You can read more about this change m pose. You can use it to check if a word belongs to inside another word. On line 76, we remove the asterisk because our discourse you have asterisk. We use whenever you see print func, scope or print form, just know that is used to remove the asterisk in these groups because we won't print it in our generated code. So online 77, we'll print our scope. Is it pop the scope? 78. We are going to revert every step we did well, we entered the function in our previous class. So we're going to repeat all of them in reverse order while is eating the function. We said the lumbar for local variables. No local variables, variable nest out does terminate functions. So this is equivalent to the opposite of what you did. So the first step is the data that needs to be returned to the color in which is the Vizio. And then you rest are all set to register. Our return address will point out is that that's stored on the stack. We'll see two entry. Then you pop the stack firm by restoring the stack point, that is value added procedure entry. Finally, you return control to the caller. But step one, we use the abnormal, we have the tau variable to return that is already in a return statements. So we use it where we are comparing written statements both step two, step four is what you're going to perform and is equivalent to reversing everything we did. We call it a function, that is our data on the stack. Now we get a number of local variables, multiply it by system, which we added when we compare the function declarations, It's one, we initialize count CNC, which should be used. Energy starts to recount. The data Formstack. Detected to beat is fixed. We use it for film point and return address. So we made sure the loop does not go below that. Then we start popping data from the stack and looting them to do HE starlight it. Five stars are stars and the count is used for the stack pointer. So we subtract cysteine for each resistor. It's an 89. We load to return address. Mill's point in time. Finally, we increment the stack pointer line lengths. And we talked to the calling function on length to one. So this is how bad is it for me for sure. So all you need is good understanding of this stack because the only thing we did is we store data on the stack back to charities does. So you need to understand how the stack works is very simple. I've explained it in a previous class and they spend it in our assignment. I've done that, I think I'll do that one more time. So let us briefly discuss how stack works. If you read the PDF file, you see L P or L D and S P O D are used to load data from memory and store data to the memory respectively. Are that they allow cross out by the same area where load word, load half. The only difference is the size of data being stored and retrieved. So since the stack is also an in memory, we would also use LB and LSD. Lsd to pop and push data to and from the stack. As stack where an arbitrary offset, see it goes from top to bottom. That is, when the stack is empty, the stack which is dy, is pointing at register m plus 2571 is f2 is add down pointing at N. Wants to push data in a register X1, X2, and X3 to the stack. When it is empty. That is, the stack register is not entering. One is empty. So the first thing we're going to do is that we are going to Festival for DVT start to stack which is minus D2 data. You want to push wishes three. So I'm going to do S p minus three. Now I SP is pointing at n plus 157. So you subtract two from it, it becomes n plus 254. Compared to what we did in the code, you find that the first thing we did was we subtracted the two tasks size from the stack. That is, you subtract three, since we have two resistors too, bush we subtract three from the stack, which is stuff that I was going to parents with. N was 254 del star out there that are using as z, which is to store data command at the address. You're nasa certain them at SP plus one, SP plus two, and S P plus three, which is equivalent to n because it's now pointing at m plus 54. So you announced all your data. And Sonia, 55, n plus one vertices and n plus sodium or 57. So you've seen how it works. You festival for the stack point, which is that the address is going to be after pushing it. Then you now start adding, incrementing and pushing your data. Now, our stack register is still pointing to unravel 54. Because I've written out bovis has been filled with our data in n plus 155, n plus m plus or 57. Now, if you want to pop our Trinity stars from the stack, we use the Load command, the data in the memory back into our star. So the first thing we do, we are now going to subtract the first row for start adding. So if you pop the first one, that means is n plus 254 plus one, plus two plus three. So you pop 255, two hundred fifty, two hundred fifty seven. Remember in our code you've written as P plus one plus two plus three. Remember? Yes. We've written it. Then after popping, we point this takeaway to address SP plus two because we pop E total of three items. So N for sonar 54 plus three, it goes back to n was 457. I believe with this leads to a Spanish in their boots or understand house. We load and pop stack. So that is how we derived our code. 17. 16. The postfix expression algorithm: In this lesson, we'll start learning how to compare two expressions. If statements, special includes logical and arithmetic calculations. We just asked them is as important. You have F is equal to a plus c is greater than p. They are all Expression statements for their language constructs depends on this expression. As we see from our parser. One of this sentence starts with a single identifier. We call the special function. Any sentence that requires competition is also compiled by the expression for action. So cuts and pesticides distinct space because it contains so many other dependent functions. Then it takes an argument pointer which points to starting token, the token table we're dispersion stats. And the token type, which does expression that whenever points to that too, can issue stock. So this is the range two-inch starts with the starting and the ending to content. So we initialize index n, which is index two, ds pressure is data. Then desperation function uses the postfix notation. So we're going to try one as you see what you are planning to do within this unit. So assuming we are given F is equal to 25 times two plus eight divided by two minus seven, or embraces immerse nine plus three times b minus c. From them will give in desperation starts at F, ends, at the terminating semicolon at the end. So desperation function will take as arguments the pointer at the index of f. That is going to be our starting point. And then semicolon will be our ACT. You're going to give the value of the pointer at index of f. And to contain that is the range of T cell respiration. So one is the lumbar, the other one is it took her nest. We start from the back. That is, we are going to start from two colons semicolon and computes until we get to F. So we need to move the pointer to talk with semicolon and stats competition backwards from there. Because at this point, our pointer is pointing at the first taco, which is F. So we need to move up and down to get to taste semicolon. There will ask that competent backwards. So to do this, we use a while loop. We preserve our point, our best setting into parent task that within the loop we increment pointer until we get to the S press Tucker, which is the terminating tokens. So we keep looping, checking the tokens until we get to semicolon. Then we numb. They'll win them deeper into at this point, the point that end. So the value of F is referred to as the test stat, while the value of pointer semicolon is referred to as penta and we subtract one from it because it terminates into corn. In our case, to consider the colon is not part of our competition. So let's measure that's your last token, will not be used for computation. So in our instance, you know that semicolon is just used for termination, is not part of the, is not part of our burritos of variables to compute. So whenever we come to the end, we subtract by one. So for example, if we subtract by one, is now going to point at c. C is going to start at F and end at C. We use semicolon to note that we've reached the end. So now subtract by one and we are now at C. Now to Kuhn greater than token output from article is not compatible with two coins. So whenever the IPN and S pressure, we choose an error. Then if the expression is badly written, our perimeter can look to the end. Or FedEx is, if we didn't see determination to cold, maybe you add that to come colon as a terminating semicolon example. If we are solving an if statement, you know that Eve has an expression within that is enclosed in braces. So we can now see that the issue terminate at the closing brace. And unfortunately there is no cuisine varies. So in such a case, we may keep looping, looping until we get to the end of file. So in that case, whenever we see token end of phi in our loop, it means that we didn't meet our terminates in the console. Which one error also. Now FPS version was also have these opening and closing, parenthesis and tax. Yes, because if you write an expression and leave only want one of the places is an incorrect expression. So while we are looping to take our point that towards the end, as I'm paying our keys to quote semicolon, chicken or distance or so. So we use are all scanner method where we increment integer brace if we meet a left parenthesis. And the equivalent is if we meet a right parenthesis. So also, if we meet our terminating to call, when our parentheses are all close, the loop. If all the parentheses are being closed and we meet our terminating semicolon. So our loop condition is to count while we've lost seen our terminates into corn or when they have not been closed. That is the only condition we are to loop or if any of them is met, we stop. So we could more. Okay, Now, now Harper into I've moved to paint the end. That is this semicolon, which is the last two. So what is next? We are going to create two more items. The first is an order to come to class code Espresso, least. With indexes, index l, and another stack called chest stack. We then use another loop to control respiration backwards from the last to come to the first token. So at this point, we are now at sea as amping aspiration. We're now at C. Now when I have to stop looping backwards, that is the algorithm. We have to look backwards. So if we meet an operator as an arithmetic operator or any logical operator, we push them to this stack, the chest pack we created. If we meet a lumbar or variable, we move it to the specialist or espresso. Now, remember this is our expression. Now we already reduced buy into and buy one, which moves up from the closing semicolon to letter C. So, excuse me. See, now we've set the looping. While we meet deficiency. The first seat goes to the SPS list. And to combine loss goes to the stack. To compete goes to the espresso list. Now and this tag, well, we are introducing a new operator. If the operator that is already on top of the stack is of lower precedence than the incoming new operator wants to add. We simply add the new operator onto the stack. Or if they operate on top of the stack is of higher precedence. We are going to keep popping the stack. Whenever we pop the data from the stack, push it into the espresso list until the top of the stack is empty or contains it to Q1, which is of lower precedence than the operator we want to put. At this point. We use it in on this to compare precedents. So the presence increases down the list we've spoken about that nest operator is to Costar, which is higher than to combine those. We're going to just add it to the stack token tree. We add it to this plus this plus is lower than tokens. So we put tokens start. This personalised loss is greater than two command laws. So you just push token. Now, whenever we meet the closing parenthesis, we simply push it to the stack. We meet the opening parenthesis. We pop everything on top of the stack into the espresso list until we meet the closing parentheses. So whenever we are looping and we meet a closing parentheses, we don't need to compare this and we just push it into js, press this. And whenever we meet the opening parenthesis, we start popping everything. So I'm going to demonstrate we test them soon. So we push opening parenthesis irrespective of precedents, took him to the S, press the stat. If it is the first operator and the parenthesis took on a DSPS or meeting the opening brace, going to pop into the espresso, least seven to the espresso. Too complex and too commandos. Today's press this before pushing the command. Now, push token to the espresso to close lashed to the stack. To call it to the espresso, least. We pop slash push token plus to consume to the espresso list to come multiply to this stack, to 25 to the espresso least. We pop everything in this stack since two has the lowest precedence. So whenever we get to everything in this stack. Then finally took on F to the espresso. In a, in a case we are done with scanning and they ask the operators left in the stack, we're going to simply pop them to this, press this. Now, our equation has been transformed from f is equal to two is five times two plus eight divided by two minus seven are in parenthesis. M minus nine plus three plus B minus C into C, B3 times nine minus seven times loss minus 28, divided by 25 to dance loss, F minus f equals interspersed list. So what the Espresso dose and the main function of desperation function is to first transform our expression into postfix notation so that we can solve it using the postfix notation. What we just did is how to convert a simple binary operation or arithmetic operation into a postfix notation. So that is what we just did now, is a part of the complex tasks we are going to perform for the expression. So you need to lend them individually to avoid combining the whole expression algorithm and mixing them up. So at this point, we've done the first part of our expression. So you need to understand this path first before going into another path. I'm going to make some examples for it so that it will be easy to understand truism for. Now, this is the operation of our espresso function we perform in this tutorial. This first function we call a definition that will complete the competition. So we keep our Cohen's resort for later. Now in desperation, we, we are also going to assign, which is tasked to variables and also check for variable types. We need to assign these registers because variables store data while we are using them for computation, they are continually evaluate this data as a meal when you see two kilohm back to Columbus stores the value. And we can only on the ad where it has to be in a register. So we need to as sandwiches tasks to them so that the account to the variable values are variables or data without losing them. And we also check for variable type because you're going to use it to even check an example you can't use. You can add a number to a positive value, but they are all Boots identified. They take for instance, you have an identifier which is a number, and another dense layer, which is a Berlin. You can compute the two of them together. So that is why we need to know their variable types also know and assign this task to them. So these are another two duties we are going to achieve within the suppression function. So we're going to add two. We already have two more parameters in article class, which we do that the V type and the register name 3D qualitative study turquoise is standard token type to the variable type. So outlook on table will look like this now. So back to S version list. We announced QAnon backwards from the end down to print test stat. So as we already said, if we meet the lumbar identifier, which puts it to the espresso. And if we meet the lumbar non bar, then if a number or identifier is before or after a parenthesis, we should add a multiplication sign to eat. Example, if you have seven in parentheses, a minus b, parentheses close. See, our computation does not recognize parentheses multiplication sound. So we must explicitly at those multiplications sound ourselves, so that it now becomes seven times parentheses a minus b, closing parentheses multiply by c. So what we did was to create the multiplication to corn and push it through our S Press lists. Then we will also prevent a situation where the parentheses are not part of the expression. Assuming you have seven is equal to two off, especially as that from seven to 12. But the nest took the token before several was actually a closing parenthesis. And the token after 12 was an opening parenthesis. As you can see, if we go by our analogy, we're obviously going to add two constant is going to become closing parentheses as say seven is equal to 12, which is not part of what we're supposed to do. So we're going to measure the parentheses are part of the expression. So we check that the parentheses are not part of the next token that comes before the beginning and after the end of respiration. So always increment index after setting at Denison to take classes. Then registers are sent to identify as only? Yes, when we identify just store data. Lombards do not store data. So he identifies should be a sandwich this task. So before competition, because identifies our variables and their store data. So we need to assign these tasks to them. One wall, one, I will check if he's unidentified. They're now called the sandwich formation using paint the end, which is now the index. As arguments. The year I've declared their sandwich function here. So then a can, whenever true or false, is using an exploration, going to create a new token number and set it to one. If is true, we set it to 0. If a is false, and they'll push you to the list. Because now we are competing arithmetics. We are not computing true and false, but they are also equivalent to 10. So we can let them at this point. Too long bus. Then as we determine why the when we meet equals embrace, we simply add it to the stack, our meat and opening brace. We pop the stack until we meet the closing brace. So if we do not meet the closing brace and this tag gets in C, we choose an error or missing. Tokens less than two combined are all operators. So if an operator is followed by two plus example, plus, minus, plus, as there is division, we simply ignore the plus by decrementing it, Andrea in Latin. So by again to come less than two combined tau o operator. So if operetta is followed by two Kumbh Mela example triplets, my loss. This times minus e. So this is same as computing negative numbers. So we need to convert it from something like trip plasma plus minus two to three plus 0 minus two. So we need to push a new 0. We now create. And it took OH, minus to subtract. It will not add 0. To make it into a proper metric cooperation. Because if we do not do this postfix notation, we see it as an error. Then again, tokens less than two left parentheses are operators, including urinary operator. So at this point is where we moved them to the stack. But we first compare it with the top of the stack and pop the stack onto the operator where we want to add has the higher precedence than top of this stack. Or if the stack is empty, then before we push it to the stack and the stack is initially empty, we just push it out. We don't pop when we meet equals in parentheses. So at the end of the loop, if the other items still on this stack, we simply pop them into the expression. These are the points where we add identifies and lumpy letters as strings. Now, students can only appear in this form in desperation. Tokenized identifier, token equal to constraint and to quote semicolon, as you can see, car is equal to string. Hello world quizzes semicolon. So that is only operation we want to be able to do with string. You can add other operations like concatenations, the thin slicing, and so on. Therefore, because before you use this string, we must assign it an identity before we can use it. Like in example, we say k is equal to a reward. We assigned it to car. So we add some error check here are simply check if a semicolon tameness and if it took on a whole concept date. Next to finally call another function to read our espresso. This for us, the function is called the Espresso list. And it takes two variable which index to start, our index to stop in the espresso, least. Initially we set it to start from the top of this, press this to the bottom. So in our next tutorial, we'll continue desperation function. 18. 17. Assigning register to variables and reading expressions: In our last lesson, we started learning how to compare respiration. We're able to sort our expression and now it's time to actually compare them. Before compared to metals first, assign which is tasked to do variables. We achieved this using the sandwich function. Sandwich function assign registers to local and global variables. Whenever we meet an identifier or variable individually stuff, we pass the pointer to that variable in the token table. Argument to the sandwich formation, which are sandwiches starts to eat before we can add it to desperation list. Why do we need to assign which is tasked two variables. Variables are, identify us, we use the stove values. So as ample, when you say int f is equal to 23, f is a variable and this stores the value 23. So doing computation, the only way the LU or CPU can store the value of 23 is by assigning energy starts with F, where F is going to store 23. So that's whenever we refer to F, we are going to get the value of 23. Therefore, every variable must be assigned a register where it can store is value or data driven competition. Not that is only token identify as data variables, so they need to conduct, gets assigned a register. Non-bias also placed in registers. As they are constants, they are assigned wages task for them. Now, this function, we set the local variable list, the Guba variable list, and the agreement. These searching for the identifier. And a variable can either be an argument or you go by verbal or a local variable. We are going to search through these three. These two know if we've created such a verbal or is it just random value? If we don't find the variable in any of these registers, it means that they are not been declared and initialized. And in such a case, we are going to throw an error. What if we find it in any, in any of these, what are the arguments? The group or the work of a Buddhist? Then adapt points are going to assign register, and also the verbal type to saturate the stand down, we return. So we do this because you know that the only time we impute a value into a register is when there were declared. That is fine. You right in. Then at that point is well, we are going to insert f. At that point, you are going to insert the type which is int. And whatever value that comes our way, you'll be using it in competition. We're not going to be writing in F again, you only write f. So we need to know what step of values f. So we need to go back to that place where you add that did introduce you start to get the type. So this is how we check the local variable. We use SD index e minus one is index or the Queen's function. We're going to subtract one. Because when immediately we added our coins function, we incremented index c. So to access it back, we need to subtract one again to get it. And once you are searching, local variable is, we assumed that the function is the coins function we just inserted. Because local variables, you will defend them within their function. So you cannot see a local web why you are such a previous function or a yet to be declared function. So once you are searching the local variable this, you must use the coin, the coin function you are in. We assume the verbal belongs to the function else. It is a global variable. So that is why we are using S to index minus one. Next, we need a syringe. And the local variable list, because local variable is contains, these are all local variables of all the functions we've been adding. So whenever we are in any fashion, we add the local variable list. Now one wants to such DDS, wants to such only the variables that belong to that particular function. So we need to change up the highest and lowest index to check. And the local variable list. So we're going to begin, or where are we going to start? Now? Where we are going to begin is called N. V is equal to the lowest or the starting index laws, the total number of variables. The same goes for n, which is where we're going to stop for the agreement, which he studies. How do we get these? Because from the function table we already, we already know the number of the number of variables we have there. So by adding needs to dispatch an index, you are going to get to where we are. Stop searching online AT. We start searching for the verbal within the given range of stats V down to n Fi, and comparing the token names at that point with variable names in our local variable class looking for a match. So if a match is found, are going to assign the variable type and the luca index of vn of the variable in the local variable. To do this, tan, M and V type of the token we are searching for. So we added the variables to the local variable, these two when there were declared. They have for both type protein computation and you no longer defend them with the type parody is plenty. So now we need the register will use the luca index or VN as the index to the array which is reserved for sale two variables. The VN, as I spent area is the luca index. Unlike the foo, look at the local variable is we have the main index which ranges from the first verbal inserted here to the coin three variable. But the loci, and this is going to start from the first variable within the function. As such, ascending index to them. So here is referred to as Vn. Else, if no match is found, recheck again in the argument table. So if we didn't find it in the recoverable table, we are going to start checking in the argument ness. We use the same method to check the argument list. Here we use the ARG and arguments number has index in the argument v to assign resistor. And now we are starting at offset five has made choice is not degenerate. I did it this way because doing a function call, we need to move the colon for Sean arguments with these tasks into the stack so that we can our kid data for the called function Into the cell register. So what it means is that in our argument list, assuming we start assigning arguments from index is 0. As you start assigning for which the statue 123. Then if we call it function, within our function, we need to move all this argument from that register into the stack and then take the argument of the called function. The function that we call within our function is arguments. We now go to that register. Then we spin again. While in a function, we store our arguments in agreements in register 0123 of the arguments, which is that if we call a function in new function, we are going to take all those are arguments to the stack. And then the function that we called, we take his own arguments into those registers. So to avoid this issue of transferring and pushing to stack, we divided the arguments with this tie into 245 upwards, that is offset five will be used for the coins function. Why from 0 to four will be used for the call function. Now whenever we call it a function, you wouldn't have to start taking our previous arguments to the, to the stack. We simply assign the argument of the code function into register 0. So for why our main function we are in has his own arguments in register five upwards. So that is why we use that offset five. So that's we don't have to push it to stack while we call it a new register, or we call it a new function. Therefore, when a function is called, we do not need to move the arguments. Again, an example. Consider these two function void. Berlin has two arguments, vegetable and spinach. And within the void Berlin, the function was called. And board has two arguments, meats and fish. Do that. Well, we are compiling volume function that we need to store vegetables, spinach in Acumen 01, then, well, we called one board is called within Berlin. We need to move those vegetables spinach into the stack so that the argument, which is Tesla one will be free. And then we are going to place meat and fish industrial register. So we need to remove the column function arguments so that it requires space for the code function to store is register. Now once we did was this. When I'm comparing Boleyn, the new method, which I did is that while we are compiling Pauline, I stopped vegetables spinach in Aquaman five fancies. Now one board is called, I take meat and fish to eat is Tesla one. Therefore, we don't need to start pushing the stack again. So this is going to save space, time and increased speed. What the downside is that we really meet that to turn on Bao for this task, we are going to use. In a real world application, there'll be enough wages tasks that even after dividing destiny enough time, EM is just to be fast. So another method, which is beta or idea Letta found it out letter by things implemented is that when we are comparing Berlin, once we should have done is that we move vegetables, spinach away from the argument register 01 and move them into our Norma saved verbal registers. Now they have enough space to occupy a day. So that's why the town we call bought meat and fish can take up any part of the argument register. So this is the best method of implementing it. So you can mail me if you don't understand it or explain it better. So back to our program. The same method is used to assemble these tasks from boats, argument list and local variable list. Remember, this is our register. Finally, the global variable list. Here we search the whole list. If we do not find a verb in any of the two list, that means that the verbal have not been declared before. We need to choose an error. Nest from our last story IS pressure went from this to this in desperation, least. How do we finalize the computation? So in this tutorial, we are going to discuss DVT S Press lists. We finalize this by searching for patterns in des Prez least. Once we find the pattern we call degenerate code generate function to compare it force. Now, this is the pattern. So you are going to take variable, variable operator. So once we see a pattern where we have an identifier for by another identifier, full ban operator. That means we found our pattern. So we compare that to eat two coins, a fine tree to consult with two coins port for three tokens. The combination is identifier, identifier and operator identify. Here we faced to put numbers and strings. So the difference is upward toes and the rest. So if we have a data, it took constraint, or it took a number or a token identifier, or it took an output is one. The next one is upward. So we have the, if you have this, this set and another set, and then we have an operator, that means we must have our pattern. I'll use the examples to explain later. After computation, the result of the computation is returned. We call the results to connotes puts. That is, after comparing, we need the resorts and we no longer call it identifier, we call it outputs. The standard pattern is going to now become if you are have a two constraint or token number or took an identifier or token output. Fooled by another two outputs or tokenized identifier or two constraint. Finally, full band of water. So this is what our data will be searching for and the retest press this, you see how it's being compared. So there's also a second pattern which we check with Genie, a piano example is pressure. We use that pattern for urinary operator. So it's just to Khumbu followed by taco, a common pattern. Now let us solve our first example. So we after we just an assignment that C is now yours is Taiwan, is energy start Sue. Is that three and f is in register for four hours plus list in that example, the first pattern I see from the left is b. Multiply. Collins who is finished Russia and sheets are attached to this course. I will compare it as Malte. I mean immediate register 0, which is tattoo, all in three. So the command here is what B3 multiplication means, multiply b by three. So first of all, write the command. The right, D2 is this task. Db is in register two while there is a constant, so we multiply them or move them to the temporary register 0. So the first one is the command which is multiplication. And since I'm multiplying with a constant, our ad, which means immediate. Dns, is the output register, which is referred to as temporary register 0. Let's add it to that as to compute. After computation, I'm going to replace B3, multiply with tokens outputs. Here, I'll use the add symbol to represent token outputs. So nine get this. I'm using artery present economic nest. The next pattern will be nine minus. Compare it as subtract immediate, temporary, which one is 39. Then after combine that, the expression is true. Now look like this. Then the next button is at seven asterisks. Comparative taken, the block will look like this. Compare. This is to watch the transformation, the pattern of how we keep reducing them. And one thing you notice about this pattern is that if we find three pattern, we return to the F4 to pattern two tacos always get debited. But if we see a pattern consisting of three tokens, and then after comparing, it will only return the output which is only one pattern. That means to Bhutan always gets deleted. So at the end, you are going to have at F equals sign, which is translated as mood, which is tough for into temporary register 0. So the desk person list finds pattern and passes it to our area function to let the construct it's caused it could generate. For the final competition. We are going to use the for loop. We loop from the end pointer to this data, one of the espresso list. Searching for this pattern. I created a temporary urbanism temp counts here, which should be used as index to our temporary variables. The temporary variables are assigned genus rational computation. We use it for temporary storage example, storing our outputs. We also reset it before we begin as pressure on computation. This first block checks for urinary operation. Tokens greater than semicolon, we could pass, identify us and took on output. Handedness token is an operator. Plus, plus, minus, minus. It took him Bank is also unitary operator. What is treated differently. When this button is matched, we call the function using the index of the first token as arguments. Notes, we are now such India's press list and not the token table. Then we'll delete the operator and leave only the results using the for loop example, you have F is equal to increments, a, a plus, plus incrementing it. We did it. Increments are better leaving only f is equal to a. Then the next block will process this attack within the urinary operator versus the equipment, the expression list index to reflect the deleted item and also this data arguments. And finally, we steps it backward. We start the loop from the start to the end of the end on layer one run 2214, we check for urinary operation. If the pattern is found, we call the function using the index of the first tokenize documents. So from the first token we can get the dark consecutive tokens, so we need only one index. Then we can undo I plus one, I plus two to get the rest of the index. So get the second to combat simply incrementing the index function we'll call the code generator to generate are compared code. After we leave only one token and video, the one using the for loop on line a 117. The one that was not deleted. We serve as our output token. We decrement stats and index L to deflect the deleted item. Set up pointer back to the beginning of the loop, which is the N documents. To compute the pattern from my sample, that is patent that involves three token. We first check if this list has up to three items. The status, the highest index. So if it's greater than two, it means that day sat is to call 12012 nest we check the pattern. Took less than two combined girl operators and token greater than semicolon. We include non bias training and identify as if a match is found. We call the LUB function using only the index or the first token in the espresso list. Remember, we get the West by just incrementing the first index nest. We delete the combat tokens from this press list. But we return an output tokens less than or equal to two combined gift for our only logical operators and logical operators, we do not return any output. We need to delete all the, all the data because loop conditions, branch and logical operation does not return any data. So after allergy cooperation, we delete T2 it to cones. Else if is an arithmetic operation, we need the token output. So we delete the last two, has set the first one as so-called outputs and store our output there in the LDP function. So for now we do not delete the first token file, symmetric operation. Yeah, you'd be functional. We assign our output to it. Any number of atom we are deleting. We are also deducting the espresso list and index L to reflect that. Then finally, there's one last case where we have, We have just a single token which I use a Boolean value example if you have n. So in this case we are in scope wise scope. So if the variant, the resistance is equal to 0 and jump to the end of the scope. Endoscope Pierre are much more scope in this group counts. Loss on Vasco is z, which is a function of our scope counts. Use it too low. We scope to exit form. So there is compression. We output branch chief is equal to, which is done them 0. If underscore is it. I've used the brink. I use the print form online 140 to remove as theories from the top of the scope. Want 58 generated code. Then if we get to the end of the function and asks you to consider left, you call the function again. It would also be good practice to reset yes, press dist index whenever we are in desperation, function, concert are alien API function. In another lesson. 19. 18. Compiling expressions: At the end of this lesson, you'll be able to compare respiration components version ever that part should be fast, smooth, and easy. Now the last part of this expression, and this part should be interpreted in our expression. That is a main computation process. Remember we have two kinds. While for, you know, Europe, Russia, and the other for binary operation. Operation doesn't mean binary means two between two variables on one operator. So starts with the tough ones. So we could test our code. Compiled code is going to at the end. So forms for dinner, for the arithmetic operator is going to be eaten as command storage is that our S1 or S2 where our command we be commands and add, subtract, and so on. Then the logical operators we be at the end the form of command or S1 or S2 and jump labor commands, we be the logical deep branch instructions like the branch if equal, variance if less than, branch if greater than or equal to, and so on. So the ROS1 on errors to add the stars that you compute our operational for. So we need to copy our tokens into these registers. There, remember are passed down from previous class session for to go on to continue on to country. Where to come 12 consu can DDA, non bass string identifier or outputs. And to convey is our burrito. Now I'm talking to will be stored in our S1. While to come one will be stored in our storage is tie is usually temporary registers in most cases. And after the bits into Control Enter, it will move the contents of this storage is the in-situ come one. So that is why we did it's only two registers doing an arithmetic operation, because the outputs will be kept in the first two. So in arithmetic operation to come one is used as a storage is why token to another country or the dead after compilation. Why they're logical operation, we do not turn it in, so all the tokens are defeated. The jump labor will be to the end of the current scope. So we simply copy the current scope from the stack and add underscore, easy to get to the end of it. Now, therefore, our tokens will come in nine different combinations. From the table. I've made a combination of all acceptable tokens for token and token to where they are normally done on bass, string or identify as students as numbers. So since they are both constants, so wherever you see non-bias, it can also be strings. Nest the command D in arithmetic operation, we have I, which means immediate, attached to each example. And I had I. If one is in Omega, then the outputs are the competition, which is also called token output, or results of previous competition. And they are stored in temporary registers. They identify as local variables and global variables are stored and saved registers. Rs S1 must be register. Therefore, to come to is stored in OS1 only if they are outputs and identify as if they are numbers or strings. There must be copied into a temporary staff first, and the register now copied into our x1. So I've replaced all positions at our numbers into a temporary register scenarios. One one will be stored in RS two, irrespective of the type, whether they are constant, non-bias, or identify as. This target register is a temporary register. Data stores output of computation, how we call it took on output or outputs from the table. So we need to effectively manage these temporary registers to avoid using them up. So divisive means to reuse them. So when our s1 is a temporary register or token outputs, that is previous outputs, we are going to reuse it as the storage register. Now back to our code. So this ALU function will be used for, you know, Your Pareto's. Why UB function will be used for logic and arithmetic operation. The term VRA on things, our temporary register, the instruction area contains arithmetic operation. Why the branch area contains the branch instruction? We initially as to conversion, to consume, and to control. Nice, let us set up our S2. So this is equivalent to one register, one to come one is the register or the identifier when it is a norm. Or we could go for it. If is a string, then fluoresce one is equivalent to two to one, it is a register. We will move into a temporary register if is a constant. That is, it is shrink or number. For this storage is tau star. Individually star that is copied into our S1 and S1 register. One token two is not an identifier. Else we set it to the register that is copied in so far as to when to come one is an output us, we store it in a temporary register. Nest our group job version into four categories. The first one is an assignment operation that is one to continue or if the operator is equivalent to two cuneiform. The next one, our logical operation. That is, if the operator is less than or equal to two combined, give you taught one is arithmetic and assignment operation combined. That is, if operator is less than or equal to token assignments and, and are all assignment operators. And then operators less than or equal to two. Our arithmetic operators. Then define the command. Storage is our s1 and s2. Remember that from our table as two is equal to two. So we check if it has the register. That is if zeta two cannot put a token identifier. Else if it's a number, then we assign it the value for number one. Token A4 as well command which is lute. He stands for, which is equivalent to L D. We use to store data to memory. We add AI to the command if token one is a string or in number, compile it. And I wonder if 57, since strings have varying length, we use the label to replace it. And now we need to set the belt to the string. So we switched to data mode using a light bel. We assigned it over to our string and switch back to test mode online 162. For logical operations, we need to get the scope. I remove asterisk for me. Then we add the on this quiz. That is where the condition we jumped through. If it fails. Then I'm now using the token type as index to the branch instruction. In the branch area are all the insertions are reaching in opposite branch area because we jump only one, the opposite of the condition. Or for example, say, if a is greater than two, conditions is only a is greater than two, but three jump when a is less than or equal to two. So that is why I've written the opposites of all the conditions in their branch. Nests are copy this string assignments to the bottom of the function, apply it to every instance of string. Therefore, how it's made C cooperation on a 179 and the combination on and so on, so on. The command we take to Country type as index and subtract took on at us because they arrange to not start from 0. It says an offset. So I need to skip the out of range of values below the lowest value so that the lowest token type is equal to 0. From the chat, we subtract two content. We generate, decode online 175. I said to come one that is the index of point data token outputs and our sanctuary, these statutes. Competition ends at two queen at co-generation. Summary. Why did we use to control this time instead of the storage is tough for the assignment of letters. Instead of storage is tau, which we used for the arithmetic operation from our textbook. Assignments operation assign values to the sale to register. Example, F is equal to two, f minus three, b plus a four. For all these operators, assign the values to the identify as f, a, and B. This is why we use the register name of tokens. So since the identifier or to console when translated, Su F equal to three a minus equal for the policy. Note that the operators are single operators not double. Both sides. Messy cooperation example a minus two, b plus three. We do not store data in de-identify register. This is to avoid changing the original values. So that is why we use this storage is to also add ice to the command if the conversion is the number. Therefore branch operation, we do not store. We compared it to conditions and jumped into coin scope is it's every scope we have your corresponding, is it lager? Whenever we meet whispers and dark. Now into branch, we assume we have the chat of how to invite invites. A bunch insertion is given below. We use the inverted code so that we jump into two conditional fails. Example, if a is greater than d, where degenerate code will be branches, There's than or equal to a, b if reason is that the statement checks if the condition is false so that it can jump us. If it's true, it just continues to the next line. So since it's checking if a is false or the conditions are inverted. So when you compare our token list and the equivalent branch condition IRI, you will see that the branch is inverted command and not do direct command also can be add mode, mode to imitate. They assess subtraction at the higher precedence than plus by swapping them more so here than in our desk person list. After our function call, delete everything for logical operation and the so-called one for binary operation. Now that was also compared for urinary operators. So first we set out tokens, tokens, token to it cannot supply scenario operate on a string ls we call an error. We use three cases to switch the two scenario per dose, double plus dopamine loss and to combine. One is plus plus or minus minus. We measure that it is an identifier of type integer. That is to come via else which one error? In token bank we measure is an identifier of the worldline else which you an error. So for increment and decrement operator, we are adding constants one and subtracting constants one respectively. So the command is add die. While fossil trash on the constant is minus one. Flat dish on the constant is one. V does not subtract immediate. It doesn't have subtracting dates or sub I. And a subtract immediate is an add. Ios constant is negative. So study the PDFs are not the hazards commands. Then finally, for two combined, we simply saw the value with x FFF, our movie to temporary register or outputs. 20. 19. compiling logical OR, logical AND, and Bang expression: Now last tutorial, we learned how to compile expression. You compare different kinds of data. In this lesson, we'll learn how to compare the last three operators which had, which have different algorithms from the rest. Yeah, logical or operation. Logical and operation and the bank of version. In our last lesson we treat Ted edit. So part of the bank which was used for urinary operators. Boy, here we're going to discuss it in full. So let us start with bank. Those bank effect on respiration. So consider these two expression. While n is greater than 25 is compared us, we take the opposite of greater than, which is less than or equal to. It's not going to be written as branch if less than or equal to, which is n into five, then you price. So the while is it. Now while we add the bank as the bank to cone, is now going to compile us branch if greater than resist stuff. Twenty-five, your brand. So the way this is another example. Two, if X, where X is imposing register will be compared as branch if less than or equal to, which is the x and 0. If is, it's. Now all we have the bank. That is, if Bank X compared as branch, if greater damages that x is 0. Is it? What effects do you think the bank has on these two computations? So from observation, the bank changes the sign of Boolean values and logical operators. It does not affect the numbers are and identify us. It's only affects boiling values and logical operators. Therefore, whenever we NDS plus list, before we start checking file button, so we need to first call a function called check bank, which checks if bank appeared in our espresso list. And if there's a bank, we change all the logical operators, sign also boiling values, and then delete the bank. So remember, the status variable is a reference to the top of the espresso list, or the maximum number of tokens in des Prez list. Now, the cheque bank, after changing the Berlin and logical operators, will also delete the bank token, which we changed the total number of variables and just press list. Therefore, the cheque bank function is going to return a new start position. That is why the function is equal to start. The end is always 0, but we use a different value for the end. Whenever the end is not 0 with the new value is always added as an argument. What's, how do we know which parts of the espresso least to start out where to stop. So we're going to use a simple trick. Consider a minus two, which is indexed first, needs to transform into a minus bank. Also quasi day again, Bank n is greater than two and desperate and this is written as two, n is greater than bank. Then consider again. Bank X. X is a Boolean value, is also equal to x bank in their space. So do you find the pattern minus student the bank sign? A complete binary operation involves two variables. And one operator at is whether it's symmetric or logical, is going to have two variables and one operator. So if we can count the two coins, then whenever the variable is greater than the operator by one, then the bank operation is complete applies to you now your operations equally. Well, we have bank X. So while we count x, the variable is already greater than the operator Belt One is a complete expression. So just like we counted races, we use the COUNTA. We can detect when a token is an operator and increments it. When the token is a variable, then when count is equal to one. We can see the aspiration is complete. So we need to do this because most times the bank does not affect every token in SPSS this. So we need to change only the affected tokens. So we need to find the complete expressions and apply the bank effects on it. So if we use a count of two counts as ample into L minus bank, we increment operator while we count an integer and equipment, when we count an operator, excluding the banks and itself. Then desperation, we always end when operator is equal to one. So now we know how to work with bank. Let us go back to our code. And I'll read this first. This, we define the function check bank, which you check for bank is going to return a new stats. Because if it fires bank, depending on how many it finds, it did, is it a 100 tonnes a new value to the start? We also pass as an argument to start an end of our list to such for bank. Now within the cheque bank function, we create the operator variable, which we increment if it took one is a variable, LC decrements if it's an operator. Ness is the first for loop that will loop through out the S breast, especially list range, even as arguments as such in 40 token bank. At the end of the Satcher we're tante new start variable. Now why are we are in default loop? We use an if statement to check each token. Ness is, and it took one is two. We call the second for loop. The index of the token bank is x. So according to the if statement, the second loop, we loop through our tokens below it. For my example, course Sida to aqueous is greater than two combined. So we need to loop from to degrade as sine because those are the token It's covers. But be careful because at times this coupon test tends to 0. There may be other etomidate is coming after that. So in our example, so that's the operator we increments on line 210. Tokenism a non bar function call or identifier. How we decrement it. If it is an operator, how we break the loop, one operator is equal to one. So the loop must not get to 0. Aligned to test yourself on, we check if the identifier is the plug-in identifier and compared it to you using exactly the same method we discussed for scenario operator, evolve into bank in this block of code. Copy this block of code because I couldn't call the functional form here. Then on line 251, if operand is equal to one, we preach any token that comes after this is not affected by the bank operation. Now we are done. I'll add 256, we see another group. To delete the two combined loop takes the position of the token band, which is x plus e to the nest, add them to the end of expiration is we took immense that an index. Afterwards. The line 260 to return the new start variable. If no bank is found, Latinas executed, return the Zach's stats that was sent as an argument. So this is how we compile the bank operator. Now let's talk about the logical and operator. So how does the compiler handle logical AND operators? Operation has ample given. If n is greater than two, vertical and x is less than two. This is going to be compared as if stuff n is less than or equal to, to pass through if is it on the second line. If n is greater than or equal to two binds to it. So you can see that it's suicides. So the logical and operation compared separately. And if anyone else we jumped to the z. So far, compare to compare it. We need to first of all, find the location of the logical and ingest press list. And we will move it. Then we'll find the two sides of the logical and, and compare them separately. Example. If we have this in their specialist. To solve this, we find the location of an indivisible and DVTs. So that is becomes two n grid data to x less than, using our old trick to know what an aspirational ends. That is, if we meet an operator, we decrement, else, we increment the counter is equal to one. We know that one of the logical AND is complete. So we know we reached the end of desperation. But since is a logical operation now which is used between two distinct respiration, we repeat the process twice. So for the first time we counts, we get two x greater than, I will send it to the desperate taste and is compared as if x is greater than or equal to two branch three phases. That we repeat the second time we get to N greater than a centimeter again, to do it this first list. And is compared as if n is less than or equal to two brands. Is it? So we get are compared code again. So we're going to use this algorithm to compile Logic. And therefore, we create our new function called checked logical and, or Checkland nest. We have three variables. Operand we increment, if we meet any to conduct is not an offer to and decrements when we meet an operator. Then second C measure that you compiled on it's wise to pass or the logical and, and count. We count how many tokens in each part and delete it. Once you send them to deal with this versus this for competition. Nest. The first for loop checks if we have a logical and in the espresso, if we don't is simply a Z. If he does have a logical hand, first off or delete it using the for loop and also decrements the stats and ESI index. Then using another loop it loose true index of the logical and down to the end. Then increments the operator. If we see any non operator to corn and decremented, if we meet an operator onto, operator is the one that we know that the onset of the AND logic is complete. Whenever the operator is one, it means one part is complete. So we increment seconds see how we set our brand to start counting up fresh nest. We are going to move the whole path to do it S plus these two, compare them or delete them from this post this online. So 193. Well, this situation we are caught when there are multiple Angela Jiekun one path, which means multiple deletion of the same token. We use it VT1 school buffer created here. Measure that the if block is only repeat that when we return from the espresso, is that is why we said This one's online, tonight's one after we return from this plus this. So for my last example, assuming we have double and so when we, it could differ spots and we find another logical Andy needs. While we come back, we are now going to execute demand if block or add that we are going to execute just the line that's called the desk. Press this without deleting any token because you are going to delete it once. So in a case where the tokens that were sent to devides first list on thin and logic Q2 is acuity else block on line 297 on theory Tom from the width desperate, the first one online to 90. And this one's research on tonight to one that is only when we can is secret this. If block Aiken. Within the block, they can verbal, we count the number of tokens that were compiled and we just press this timeline, so 90, by getting their difference between these stats and the end, then delete them from the express this using a loop on to 93. Online 22. After the first path I've been sent to the desk, press this for competition. We set the end as the stats of the second part. You can see it from the diagram. So example you have two is greater than, P is less than, and we have two paths to a and to B. D to E has greater than Andy to be half as done. So the first time we call this breast least for the first path, we said stats and n document the position of the end and three respectively. Then for the second part, we set starts and end argumentative position of 32 respectively. So two, which is the tail of the first part, becomes the head of the second path. That is what we did online TV or two. Then align tool for we break the loop. Second C is equal to two. That is, well, we've compared the second part of the AND logic. And then we toss that line. Threonine. Ness is illogic core function trust equity for bank. And, and you do the same for the logical OR. So how does it compare, compare logical OR compare the vertical or the comparative tests puts condition. If the first condition is passed, then the second condition is keeps us if the first condition, the second condition is also tested and if it fails, the scope is easy. I'll see you. The second condition passes. The scope is also executed. So consider the example. If a is greater than two logical OR B is less than three, it's going to be compared us branch when the branch, if a is less than or equal to two events. So if one is it. So you now undeclared jump to if underscore one F1, is it branch if greater than, which is type B? Do we run through if is it? If one? Here we first test if the first condition, first going to jump to F1 is we add the second condition is going to be tested. And if it's first-order, so we exit the loop. If the first condition is executed successfully, we instruct it to jump and skip the first condition. And the scope is, is acute. So in our code, we will separate the two path like we did for the logical hand and also use the operator and second see and count variable. And they'll do exactly what they did when we use them in logical and we set the new scope here. We had the first condition we jump when it fills which we are now bullets enable us to security. Second condition. So let's go back to our code and see how it's being executed. Czech law, we check for logical OR logical, or is the, shall we are going to use for this nest, we declare five Foucault variables. Operand. We check if one part of the logical OR is complete, then it will send it to DVD expressly. Second, see we break the loop when the value is two. That is one, we've compared both sides of the logical or cancel. It counts how many tokens saying each part and it talks it from this press least scope, we temporarily store our scope counts. Use it later. I'll show you is another group of other variable, count which performs the same duty as a date, counts and logical and makes sure to consider the dead ones from the espresso, least. Since most of them we are using logical and I won't explain them again. The first loops scans this first list and using the if statement checks for the logical OR operator. If he sees one, then uses the for loop to delete seeds and decrement the stack. And then desk. Using that point, that position as point moves down, incrementing the operand. If a number identifier token outputs is scanned, and decrementing operand if an operator is looped. So this step is used to calculate the right and the left side of the logical or operation, which is complete when operand is equal to one. So he says that block was used in logical and operation. How could it counts which perform the same function as the needs, wants and vetoed. Whenever the operand is equal to one, it means one side of the operation is complete. We increment second, see, our second C is equal to two. It means that both sides of this statement is complete. We exit the loop on line 264, also a set of parenthesis that counts enough fresh whenever I is equal to one on line 207. Now 1 second C is equal to one. And before we send the first part of the espresso list, we upload a new scope is in-demand scope online 200 and that's it. Example, if we're in an if scope than the top of the scope will be as ample if two, then now we add a new scope as we append the scope depth to it. It's not going to be something like if underscore for this new scope, we prevent the first path from jumping to the end of the if condition. When the first conditional fails. When the first conditional first, we jumped to the new scope, which is before the second condition. So in our example we jumped to if for under quizzes instead of if two is this, we enable us to test the second condition, delete council and turned around 40 to perform similar function to it once. It's enabled this for loop, once. After sending tokens suggest press least, we need to delete them using this for loop. And do a wonderful job to delete ones on line 247 when the function returns, but unfortunately it can reach more than once if they send tokens contain more logical operators. So it needs wants prevents this function from the 1890 degree desperate list returns. Else if we need to send another took when we call the width espresso list on line 253. Since we send tokens from x down. So I aligned journal 44. Then the difference between i, between S and iodine 246 gives us the number of tokens you ascend into the express. This using IS index, we do it all the tokens from the espresso list online journal 47. After sending the first part of the vertical or the DSPS list, we call the unconditional jump. That is using branch when 0 is equal to 0 on line 259, which is that 0 is a constant, is 0. This jump we enable us to skip the second path if the first part is true. Next we is it Dao scope on line 360? This is, this is where our first path lengths if it fails instead of demand scope is it, which includes boots condition. After it is the second path condition, S21, we pop the temporary scope, leaving our main scope is a compounds. We now use our main scope to jump to the end of the main scope. If it's first line drawn down 5758, I used to remove a steak knife. The second conditional fills its way to the main scope, is it after the second condition? So line 264 is where our unconditional jump on 259 lands. So we read this Todd scope, so that's our condition and jump lines here. Aim is to jump the second condition. After we've written these stats. Now, declare it espresso is on top here because we included it. Who the editor bovis initialization. When we discussed move on conditional statements, we test these three operators. Particular method of comparing logical AND logical OR, and bank, we have invented by myself. As such for an algorithm to complete the two of them we stopped me from completing this project for Fast is most, I do find one until I sat down and studied the operation and invented one. If you ever going to use it in your own application, this can be refunds this course. Thank you. 21. 20. Compiling while loops, and exiting while loops: In this lesson, we'll compare the while loop. Conditional statements and loops expression with logical operators. In contrast to arithmetic operators, they share the same computation but different repair name. Example. Why a is greater than two and if a is greater than our combat, exactly the same way. Especially for show, combines them the same way. The difference is that they have different scope name. Therefore they'll have different label for punching. My money. How we will compare this? While n is less than two irrespective of whether as a while loop if condition of for loop is going to be compared as this. So we start by the current developer. Since is the loop, I will need to jump back to the beginning oftentimes, so we are going to print the leper. We're going to jump back to nest. We perform the condition n is greater than two. This rational function handles that as you've already discussed, but it's really jumped to the needs of the current scope. So how do we know where to jump to is simply get the scope name and add underscore is it treats as it's in this group. For report. Pete, you first add on to this group name. I'm presently. So back in our code. We start by the candy CYA function with the current parent, which we used to count braces. We increment pointer which will move from wild to the opening parenthesis. You preserve pointer is encountered. Opening braces and closing braces and are only two currencies to market scope. Without these braces, we might be able to tell when we enter the scope. What is definitely had to determine when we leave the scope. Whenever we enter the scope, we need to check if the opening braces is omitted. There's no need to check the closing brace because if it's omitted and opening braces rating than the scanner we try and error. So lots we will do is to get to the end of the wire condition and check if the left brace is available. This will preserve our pointer. I use the accounts for the loop. Then we loop until we meet the left brace. Then if we meeting the left parenthesis, we increment. And if we made the right parenthesis, we decrement. If we only expect that the inmates token end of file, which one error? Now when the parent is equal to 0, that is when all parenthesis has caused. The next token is supposed to be the opening brace. So if he's not there, Which one error? Else? If it's there, we break the loop. This block of code we be used in the if statements and for loops to check for hours. When discussing them. Ness we print our scope labor and push it to the scope and increment is called debt. Finally, we'll call the special function starting from the point, which is currently pointing at the opening parenthesis. The opening left breast, especially for Azure, handles the condition and returns in new pointer nest. How do we exit the while loop? That is, well, we meet the closing brace, brace function immediately after the function cuisine block. We add our wire, the closing block. You first check the scope to know if the tip of the scope has a wide in it. If it's successful, then we enter the while loop. We remove their stereocilia in 57757 and so on. Using an unconditional loop, we send it back to the beginning of the loop. Whenever we get to the end of the while loop, we use an unconditional jump back to the beginning of the loop. When it gets to the beginning, need to check the condition again. The loop condition fails, it jumps to this. Is it. This is the only way to escape the unconditional jump after that report, the y scope and is it less class? We are going to discuss the for loop. 22. 21. Compiling for loop: In this lesson, we'll discuss the for loop. So how does the compiler compare for those? Given the example is going to be compared us would generate code for the initializer that is high is equal to 0. Reason is that that part is fixed and doesn't change in the loop or less is incremented. So is a constant which does not change. We now loop to increment it. So we have to put it outside our loop. So that's why we don't keep repeating a constant. So we initialize it even before our loop nest when initialize our follow-up libel than the logical expression is handled by desperation function. Then I'll jump. Libel is the coin scope which underscore is it added to it. So we use uniquely able to differentiate the levels from water levels are the same kind. That is where we add the scope debt and increments it in our scope. So the numbers are unique to the liberals. Then we skip the increments and execute our loop body. Well, we meet the end of the loop, our scope closing brace. We perform the increments. And finally with it, how does our compiler compile it? Remember that the increment part of the loop is executed at the end of the loop. We will create this group out for a book called four counts to save the position of the counter at the beginning of the increments paths so that we can use it at the end of the loop. Since the increment but is obviously created at the end of the loop. Before the loop is it quit default loop, see follow-up function. Then reset default count to 0. At the beginning of every four loop, we increment pointer by two so that it now points to the first token within the follow-up parenthesis. Then we set theorem which we called our parentheses. Since we skipped opening parentheses, we set it to one. We'll use this variable to check if the for-loop scope has an opening brace, just like we did for the y loop. Then we use this if else block to check what type of variable we have. Well, there is the verbal declaration and initialization, or just inertia as an annuity declared variable declaration, we call the variable declaration function. If he's just an initialization, we call the aspirational function. Example, int I is equal to 0. We'll call the value correlation function. While I is equal to 0, we call the aspiration function. Else we drew an arrow. Ness we push our for loop on line 752 and print deliver on line 754. Can we increment is called depth online. So I wonder if 55. We add the beginning of the for loop condition. Line 750. Sys. Call this version to execute the for loop condition. The condition terminated semicolon. After the condition yes, is increment, but the forecasts, does it point to this point to use it at the end of the for loop. And therefore four counts minus one, we point to determinate in semi-colon of the follow-up condition. Dell copied this blog from the wild loop. This block is used if the opening breweries for the scope is on with dead. After they check the counter, points, add a closing parenthesis, are incremented by two. To skip the opening brace, I'm pointing to the first token in the follow-up scope. Cannot do is the for loop. The for loop we go to our red press function because it is instead using the right bruce. We check if the top of the scope has an asterisk four in it that we know. Is it in a for loop? So we need to compare the increment parts. Now, to do this, remember we only have two cars in parentheses at this point. The opening parenthesis as past and before we call the expression to compute it, we need to complete desperation that we have bought his opening and closing parenthesis. Now we need to create an opening parenthesis for it. So we now convert the quizzes semicolon off our for loop condition, which is just before the four counts, one opening parentheses called the threshold function to compile it. That is, we use for counts minus one for it because it's just before the forecasts. We don't need the return value of the expression because it's not a true position, we only to perform it. Nice time to visit. So we first remove asterisk from the scope count top. Then just like while we set an unconditional jump to the beginning of default loop, before we use it. Only on to the loop condition is false. We will be able to jump to the exit of the loop, continuously jump back to the beginning of the loop. Then we pop the scope. 23. 22. Compiling IF, ELSE IF, and ELSE expression: In this lesson, we discussed the if, else and else if condition. Given the respiration is actually simple. If token we push the scope, we send the rest are desperation function to do the rest or when it returns, when the scope equate DC. If function. In our code, we declare our power, which we check for this opening brace. Then we increment the pointer to point to the opening parentheses. Then we preserve it and use count to check for opening brace using the while loop block of code, which we used for the for loop also, after which we push our scope and increases steps. So then we send every token within the if statements to this special function to compare. That is, all the tokens within the IF binds. This is our return pointer and NTD scope body. So how do we eat? And also compare the else and the else-if condition. Now we've created a C function. Let us create the sales function, which is accused doing an else if statement. So I'm going to quit CROs function. The only difference between the if statements and the else-if statement is the scope. For the else if the scope is a leaf. So we copy this code and see if the CEOs and change the scope NAM nest. Well, we have just the L statement that is the last part of every day default, but of every statement, we simply incremented by two and continue parsing. We do not put any command because whenever if else-if statement lands on the else statement, it means that the if or else if statements filled, so we simply execute the else part without any command. Now, how do we, is it the if statement or do we is it the else-if statement? And I'll do is if else statements. All the scopes using our cosine parentheses, sorry, our course in Paris. So we go to our Add press Function. Then first, let us first compare if statement *********, when is followed by an else statement. What does your closing statement or an else-if statements? This is how we're going to compare it. Now before we see two, we need to tell the if statement to jump to the end of the loop. If it's successful. Else issue, continue with the else or else-if statement. On line 594. We take an unconditional jump to the end of the loop. The name of the scope is print, L was created and five-ninths. So the unconditional branch on line 594 appeared within the loop. So before is termination on line 595. So it is only when the if condition is successful. If the if condition fills, it jumps life if a 195 and continuous equity in the house. But the prints from scope on line 595 was original scope with the if conditional used. And we assessed it on line for fungi sees before popping it and live it and adding the new scope online five-ninths. See the new scope online five-ninths. So we will now be demand scope. Nest letters, compound if *********, when there is no else clause after that is when is the final end. So simply prints the scope, is it's on line C, So one, I will pop the scope and is it finally letter stem needs the else if statement. On line 11, just like we did for the if statements, we take an unconditional jump to the end of the main scope, which is print L, which was generated and latency. So nine after the else, if scope was spot on line six or seven, removed asterisks on energy sources and assistant after branching on minus dc level, if the else, if the if else condition is successful, we print the else-if scope online Susana and tough. It was integrated analyses. So five and pops on and see if the else-if condition fails, it jumps to line s2 off and continue execution on the next line. If it's successful, it means it meets Linus's level and jumps to the end of the loop. Starts and we check if da is no else statement coming after you eat. If the else-if is the last statement. Last statement, it means that you're also going to pop them in if scope printed on an icy cysteine. And is it so every successful conditions we as equity is block, then meet the unconditional branch on line 11 and jump lines 616, pop disco. And this is how if, if else and else statements are compared. 24. 23. Compiling switch statement: This lesson, we'll learn how sweet statements are compared. This statement is, we went through a chain of if-else statements. We discussed in our last class. Syntax is written us. So how do we compare these three statements in our code? So far as the courtesy switch function. Then we increments the pointer by two. This moves the pointer from the switch to con, to the identifier. We want to. Then using the Koopa variable called as the bouquets created here. We see pointer at this point, just like we used four counts to save default loop, increment, a use case to save the switch identifier because we need, we need it indicates cross example. Consider switch a case one, brick. We need to save so that we get to decay statement. We can compare the eye this is or equal to one. So that is why we see the UK's ness. We increment pointer now points at the closing brace on line 876. Before that, we push this switch to the switch to our scope and increment the scope depth as we check if what comes after the identifier is the closing parentheses of the switch, the opening case statements, and the case itself. That is true, then the statement is correct. We increment the pointer again to 0.20 case token. How to measure tax was accomplished here. One is that we push this switch to the scope. And two is that we, the switch identifier in this case. So decided to men we achieved in this particular session. So how do we handle the case statements? When we meet a case statement, how do we handle it? First, we need to push this scope. So that's if TK's conditional fails, we can move to the end of this, end of the scope or end of the case. Example. We, if we add a case statement in this to compute the case, we need to transform it into this. That is to be able to compare the switch identifier with the keys. So we are going to convert a switch opening brace to switch identifier and the kids token into Taconic, who has shown. It must not be the switch identifier. We're just going to come VAT the last two quiz before the case identifier converted to desorb use case. And the last one to the, which is the case token itself. You convert it to a double equal or took on a coil. On line eight, Andre 83884, we do the confession. He said they took on case to to Conoco and whatever comes after it to the SMU case. Then we call the expression to compute its transpiration is terminated with a case to come colon. We do not allow opening brace in a case statement. Because DK scope is terminated with a break statement and not equals in Paris, we measure that we do not have a corresponding opening brace. Some high-level languages allow opening brace or a case statement. So we measure that a user doesn't want mistake any edits here. Nest. How do we compare break statements? Now, brexit meant terminus, decays and the default statement written as bricks semicolon. So first let us go back to our parser and differentiate between these brick and the brick that is used to is it loops. Now if we have a case in our scope, then it means the break statement is used to easily take case statements. Then we call as a cubic function. Otherwise is the normal loop week four, case break, we call C switch brick function. While for normal lab week, we call this C brick function nest. We declared the SW brick function to compare the brief statements within this SOB break with Festival for Gettier case is it's an puppies from this group counts leaving this risk group on top. Before we pop it, we measure that is not a switch brake. Swiss callee terminates with a closing brace. After the Pope, we are now left with the mess we scope. So we get it into printers of your variable. And if the case is successful, we jumped to the end of the main scope unconditionally on line 90 for else, if it fails, we jumped to the case. Is it online n over five, and is it and this acute De Nest Case underline NO cell phone. We increment pointer by two to skip the brick token and it's terminating semicolon is the default statements. We increment pointer by 22 pints on to the next token after the default tiny semicolon, do an error check. And so finally we terminate the switch within the red press function. When the scope is, is we scope, we simply print the scope is it's unpopular. Scope. 25. 24. Compiling function calls.: In this lesson, we'll learn how to compare financial costs. The Lanza to compile function declaration and initialization in the previous lesson. Functional cos z data sheets this that we must perform for task. First, we have to push all coins function argument to stack, pass any arguments to the arguments which is to push our temporary stack, which is tasked to stack the jump and link instruction to the calling function d z MEPS data sheets. So we're going to skip task one. Why? Remember the arguments which is tau, was divided into two when we're sending, which is as two arguments. So at this point, we are saying as, we ascend from offset five upwards to arguments. Therefore foremost, offsets four down to 0 will be used for the new arguments in tasks to offset the coins function arguments and the called function arguments have shared the register so they're no longer use this average distance of only perform task 234. Next, a function can be called singly example or in an aspiration example. That way we first call this rational function. The function call is declared single like in our example, is thus an exploration who first called a special function as shown if it appears in an expression, especially flushing is also called Dan within their special function. While we are moving the pointer to the end, if we see a function call, we call the C2 conform co-function. Within this function, we start by declaring some local variables. There'll be reused multiple times to fashion indexes, index of our function list as we use it to search for the called function. That's six, we start searching the function tuple for the called function. The pointer is pointing directly on the called function. We are using the function in texts for this etch. Whenever match is found, we increment count. So when count is greater than 0, a match is found else the function does not exist. We also copied the number of arguments into the argument verbose just online, Andrea, 43. If count is equal to 0, it means the function has not been declared, so we call an error. We perform tasks to remember the term counts is the index template variables. We use it to know how many temporary registers we've used a movie to this dark line 950, the stack may space for the registers by multiplying the temporary accounts by system beats. I'm making space for it on the stack. Then on line 53, we start pushing them into the stack. At this point we are using counts as index to the temporary register. So a discount. So as we said, it's on line 949 ness, we increment the pointer from the function call to the opening parenthesis as toys into verbal codes, dorsi. We mark these points which stores C, because after moving the code function arguments, the arguments which is going to ignore them. To India rest of desperation. That is why we return back to the S version. So that is why we will mark this points so that we know we're starting token to ignore. So online now Andrew and S21, we incremented again by pointing on the first token within the code function parentheses, where the arguments are stored. And if there is no argument, then it will be pointing on the closing parentheses of the function. And the closing parenthesis will be marked as two can start. Very soon. Once we ignore all tokens between two constants are to end, as we will see later. Than assuming the code function have some arguments. We set the number of arguments that you go above, called Fun, which we created here. We set counts so that you can reuse it again. Now we keep sending the arguments dispersion to compute using two comma as a delimiter or end of expression. And the red parentheses if it is remaining only one arguments. This is task two, where we pass the arguments, the arguments, which is to use the function arguments as arguments, which is ten decks and desperation function down in another special function just before we call it the re dispersed list, we have to add task, so ds plus this down and just press this. We are assigning tasks to all the arguments. We also use this for the ternary operator and the return statements. So what are the arguments is not 0. We create an identifier on line 551. Who's register is the argument register using the function arguments as the index to the hagfish. We add this to the espresso list and online 554 we add the Taconic. For example, consider this example. Call a, b, c minus d are all arguments. So we need to push a, b, and c minus z into arguments, which is 012. So we call the aspiration for Sean has already shown. When we look to the arguments, then if the number of arguments is 34, core a, B, C, and D. So this is what the type is going to look like. So first we have the arguments numbers, then we have the data that we are sent to the suppression function. Then you have the two coins into a special list. Then we are going to add two registers. And this is what is going to look like after adding the stars and then the function arguments values. So you can see that the argument register will add two registers using the function arguments index can see where is written to one or 0. So this is where we assign this task to the arguments and is now in postfix notation. By the time we, we assemble the tokens and devides press list. The first one is going to be, which is that two is equal to a comma b, which is t1 is equal to b. And register 0 is equal to c minus d minus c. So the actual arguments, so we set the function arguments with three. Whenever we send arguments with Jespersen list, we decrement seats. So the column keeps decrementing. Therefore the arguments register, we have indexes to 10. Therefore, if they are no more arguments to send, function argument is 0 and this block, no wrong guy is acute. So now I'm back to our token phone call function. This was where we call the special function after the last documents, which is when arguments minus count is equal to one, are we terminate our expression articles in parentheses comma. Then we decrement pointer to point to the closing parenthesis. Since it's pointing to the next token after the parenthesis. Now, this is where we call the function using long conditional jump and link to the function name. After that, we set to opening and closing parenthesis as END tokens stat respectively. And the function call, we set it as tokenized identifier and give it the function called token. We set it to as tokenized identifier and give it to the arguments register 0. This is because when the function returns is return value is also stored in argument is 0. You also see here we copy the return value. Well, we discuss return statements. Noun, the return value we precedes the function call, and we return from the function. We said the opening parentheses, some quizzing parentheses of the function as token end and tokens that as sample return from the token phone call, it becomes this. Now as you can see, the call to Cornell has been replaced with the return value, which is data, that is register 0. Then to opening parentheses album replaced with two coins, end. And the token with the closing parenthesis, as we've placed with tokens stat function name, opening and closing parenthesis are replaced with the return value register to query engine to start. So back to JS pressure function that's called the token foam core. One is returns the special function continuous counseling from where I stopped, including counting the function arguments. There while we're looping from Pinter end down to point stats in desperation list. We skip everything in-between the two end and tokens that. So this is how we avoid counting in the function arguments. Second time, we get to two entity nasa verbal token is the return value, which is tau is simply push it into desperation list. Therefore, this becomes this in desperation list. C because the whole function arguments are gone. And the call to coolness transform to the return which is the this is how we compile the function call. 26. 25compiling tenary operator , return, break, continue, label, goto, cout keyword: In this chapter, we'll discuss the ternary operator, returns statements, the CO2 operator, the continuous statements to break statements. And finally, the multiple variable declaration. Think we've done that already. So the ternary operator, the sentence is defined as an identifier, is equal to expression one, which is usually a logical operation. Then the tenor operation, then aspirations. And expression tree, which I separated by a colon. So as sample. Some language, we'll also add this does, especially on one ternary operator, has permission to come colon and especially on to example. Here would be using the first construct is up to you as a designer to determine which, between the first two, which ones to implement in your design. Well, whichever one you choose to implement, you have to write the codes accordingly. For our course, you could do before the first expression which is given us identifier, is equal to expression one ternary operator. It's pretty sure on to color, especially on three. Sad face, is treated as an expression on TO YOU CAN, there's pressure function with a compare StumbleUpon, the ternary operator. So if this statement is given as this are called, parent needs to convert it to a simple if statement of the form. If a special one then identify as equal to especially else identifies equal to expression three. So delete our identifier from its initial position and copied into two new places. This is the logic. We add the tenor operation as a scope, so we could treat it as an if else statements like this. If, especially in landfills, can I identify as equal to expression to you branch out conditionality to turn out one, is it? Then you declare an array of receipt. It first checks. If the condition fails, it jumps to ten. Everyone is z and performance F is equal to 0. Else, if it passes, it is accused. F is equal to 12 and jump to the end of the scoop. So assuming we have in Norma expression, we don't know there's a ternary operator in needs. Special function. First while loop, or lay a fun run 55. Once we point to the top container, you can marry me first, push it into this group. This will be useful later for jumping for the logical expression. Next, we set ternary, which is a global variable to point, to the point that starts this. We are now bull loss. Mark these points for later because we need that identifier in especially to under special interests. So we use the ternary to market because the first identify your expression is our disposition or we need to copy it into respiration. Respiration three, then we increment the position by two. This keeps the identifier and the equals sign. Now I'm points at the beginning of the condition as pressure on loan. So break the loop at this point. So our expression now contains only the condition of expression one. Then we'll compile it. Are we turning in our parser is not going to execute this eaten our function, since ternary is in our scope, this function is declared in our semantics or teach file and you initialize and disadvantage or C plus plus file. Now we need to get the scoop. I removed the asterix. Remember the Guba variable is pointing at position. In our example, therefore, 100 plus one is going to point ats equals sign. Now we are back from the condition. We are saying F equal to two tacos before the beginning of the second expression. And taught desperation, which is at index minus two and minus one. This second expression starts at 0. So we are located them point minus two and minus one. I'll send it to the S version. Notes we are now using colon. How we send data dispersion. We use semicolon. If the second expression was executed, we use an unconditional jump to the end of the scope. On line ten, twins, one. If it fails, we jumped to the first condition scope is it's on line 1022 and perform the operation. Now I'm pointer is pointing at the beginning of the third expression. We add F equal to, again to two tokens behind nodes. Our expression when asked that to, to cause behind for both of them. Finally, we SED scope and pop it out of the scope accounts. The unconditional jump on line ten to one, we land on demand is the scope lab online 1027. This is our general Persians are compared. Now let us discuss the print statements, or C, or C plus plus. C out is an inbuilt function. How we handle it like a function call. That is, we will add all the data we want to print into the arguments register and call this the odd function, just like we did for the function call. So this is how assembly language combat compares in boot function. So given this example, how can you tell me how many data objects to print? Yes, the number of objects to print here is equal to the number of the shift left operators. Therefore, using a while loop still emit determinants in semicolon. We can count the number of shift two left two coins, and this is equal to the number of atoms to print. Then we also need to assign arguments with these tasks to them. So that desperation we be of the form register 0 is equal to a, which is Taiwan is equal to b, and so on. Then after sending DVDs that we now jump to the c out function and is equity. So back in our code, the first, we first preserve our pointer and set the arguments counter to 0. So this argument count, we count how many data to print. And using a while loop, which loops until we meet it dominates and semicolon within the loop, we keep incrementing arguments. If we meet talk, we shift left. So if we want to expect that the media token end of which you an error. If the argument is equal to 0, that means there is nothing to print. We also drew an arrow. So after that we set the function arguments to the number of data to print. We're increment pointer by two points on the first token to print. Just like we did for function call, we use a while loop called a special function using the Shift Left as determination until the last data which we terminate with semicolon. In desperation function, it will use the same block to assign registers to the data just like function calls. So this block is accused whenever the function arguments is greater than 0. And this we are sandwiches that to our output to go back in our code after assigning registers to the data, we jumped to this function. Now let us talk about the return function. Grandma is this font return function is, is excreted exactly as the function termination as sets and return function data is returned at its root on it to control the arguments, which is the df for instead of repeating the same tax for the return function, we simply, I thought you were done that I introduced the star and jump to the end of the function. So in our code, we increment the pointer to point to the next item after the return to Kuhn. Then we check if it's written in any data. By checking if a semicolon constant nest, then it does not return any data else, which returns the data. Then we set the function arguments one because we tossed it's meant to return only one argument. So if he does not have a semicolon immediately after that function, is returning a data, we set the function arguments or one. This we are now pulled. This block of code here is acute professional function. After that, we decrement pointer points to the semicolon. This is because, as you mean, there is no return value which will be pointing on this semicolon. After that, we jumped to the end of the function to perform the rest of the task before we turn it. When we jump to the end of our function, or the function termination operation will execute at that point. So that is how we want the return statement. Then nest the break statement. The break statement in C or C plus plus is a loop control statement which is used to terminate the loop. As soon as the brief statement is encountered from within the loop, the loop iteration stops there and control returns from the loop immediately to the first statement after the loop. Given this example. Here, the break statement, we exit the while loop in our code. We are going to pop all the scope until we get to D for loop or a while loop. Now we measure that is actually a for loop or a while loop, and not just an empty scope that dominated the first loop. Then we jumped to the exit of the loop and pop the loop scope. Else which one error? Now we increment pointer by two to skip the big toe colon semicolon. Then we return pointer nest for the continue keyword. We repeat exactly the same operation as the break keyword. But we will jump to the beginning of the loop and not to the exit of the loop as shown here. You go to keyword. For this, we will simply increment the pointer back to two points to deliver. Then we take an unconditional jump to the left. Then we check to measure that is it terminates in semicolon after the lead bell. Then we increment pointer by two to jump to the next token. Finally, deliberate keyword. We simply print the liver. 27. 26. What next ?: Thank you for joining me to the end of this course. I'll leave you with small practices as size. So what did you learn in this course? Most importantly, you will, and that's competitor design is not some Bash command line PowerShell command language spent five years of programming tax or a set of simple we're eating now Goya team in high level language, you now have a better understanding of how the programming language you use that design and implement that. You've learned a handful of important fundamental data structure and gotten some practice doing low-level profiling and optimization, lands new ways of working and solving problem, even if you never work on a language, again, you may be surprised to discover many programming tasks. You can be seen as language like there can include. You've learned a lot of things about which is Thomas Stacks. You've learned also about the whole length of function design. Most importantly, the scanning process. You always see the implementations of our scanner. The ability to go through a fair reading Carta. Carta is not only limited to compare design for a huge, lots of problems in programming language. So what Nest after now? You can go further for code optimization. Here you can scan the code, write it either by, by rising. Programming, decode garbage collection, removing redundant bits, which is trash, compacting them into lower lines of language. Adding more grammar constructs like for the include as during copulation. Then you can optimize the d's compare for your particular design of visual machine, of processor. Yes, is very important because you can choose any competitor. What is your target at where you have to modify it for your own particular hardware design, which these meats and number four, you can either design a virtual machine, has CPU and system on chip. I have a visual machine and the system on chip on my website, budget FPGA.com, which I designed. You can modify them and optimize is compare for it or your own. Also modify virtual machine design. So for the SOC few, one design for a virtual machine, I've also this CPU design. You come tonight, optimizer compare for it other than the CPU alongside the virtual machine, all free, my website. Then you can design assembler. If you're able to design a comparator, assembler will be very simple. You just convert intuitive compiler calls into zeros and one. Who, finally, you've learned a lot about important fundamental data structure and got him some low-level optimization walk stack. Now hope I taught you new ways of approaching problem. Thank you want to say again for framing throughout this course. And content issue, can send me an email. Thank you. We should do based on God bless.