Create a Programming Language and Learn Advanced Python – Full Course

freeCodeCamp.org · Beginner ·⚡ Algorithms & Data Structures ·2y ago

Key Takeaways

Create a programming language and learn advanced Python concepts, including object-oriented programming, data structures, and interpreter implementation, using Python as the primary language.

Full Transcript

learn how to create your very own programming language and the only prerequisite knowledge is basic python Ariane Hegde teaches this course along the way you'll learn more about object oriented programming linear data structures binary trees recursion tokenization parsing and more so let's get started welcome to this brand new course where I'll be teaching you how you can create your own programming language using python now the goal of this project was to teach you how you can Implement a bunch of programming Concepts into a real world project I have always felt that especially in computer science Concepts have been taught in a sort of abstract and detached manner independent from The Real World and that's why in this course I bring to you a bunch of Concepts like data structures like algorithms recursion object oriented programming I teach how you can Implement all of these Concepts into a real world project you can also head over to blog.algalon.net or click on the link in the description where I talk about a bunch of learning methods I employ during my learning phase to have a better grasp on Concepts and a higher memory retention now the only prerequisite for this course is basic python knowledge stuff like if statements variables while loops and functions and with the basic foundations laid I'll take you to the next level talking about data structures algorithms recursion and much more and by the end of this course you'll have your own programming language so by the end of this project we'll be having the final output so this is our I've run the shell.py file over here and this is what our output is going to look like so we're going to input our shadow script code as a python input so I'm gonna say let's say we can give an input arithmetic expression so one plus one it returns two I can do 5 plus 3 it will give us 8. now I can even talk about order of operation so I can even do one plus four multiplied by 7. in the wrong order of operations that'll be 5 times 7 which is 35 but the correct way to load is multiplication first so we'll have 7 times 4 which is 28 plus 129 which is what we get if we want to make the addition we want to give higher priority tradition we can add a pair of parentheses multiply by 7 and this should give us 35. so that's all about arithmetic Expressions that we can give it apart from math it can do other stuff as well it can create variables so I'm going to say make variable a and I can assign that variable to the value 10 and like how in JavaScript you have the VAR declaration or the let declaration and this time we're using make so the make will be used to create a new variable so we create a variable called a and give it give it the value 10. I can make another variable B set it to 15. and I can make another variable C and set it to a plus b and this stores the correspond the variables and the corresponding values in a python dictionary now after variables we can also deal with conditional operators and comparison operators so sorry Boolean operators and comparison operators so Boolean operators is something like stuff like and or and not the comparison operators are greater than less than is one thing equal to another thing and so on so I can run a Boolean operation I can say 7 greater than 2 and 5 greater than 3 in this case both of the um expression 7 greater than 2 and 5 greater than three both are true and therefore this should return 1 which stands for true if I do 7 greater than 2 and 5 Less Than 3 which will return 0 which stands for false act into 7 greater than 2 or 5 Less Than 3 which will give me 1 again I can do not so I can say not 7 greater than 2 so this will give me 0 because 7 is greater than 2 and then we're not in that operation we are inverting it where um turning 1 to 0. so that's the Boolean operators we can even have two two things are the same I can check if C is equal to B so this is how I use it in Python you have two equal symbols to check if one variable is equal to another in in our syntax we Define it as C question mark equals B and if I run this this is 0 y because 25 is not equal to 15. I can make another variable d set it to 25 and now if I do D equals to C this will give us 1. so that's the comparison operators I can't even check if C is greater than a and that gives us 1 and so on so apart from the comparison operators you can now use if statements so I can say if a is greater than 3 do something so do three times three that makes no sense but let's just see if this works and that will give us the output 9 because a is greater than 3 but if I do if a is less than 3 do 3 times 3 it will give me nothing I can answer on ellips so I can know if a is less than 3 do 3 times 3 l if a is greater than 12 do 5 times 5 else do 10 times 6 so in this case a is not greater than 3 and it is not greater than 12. so both the conditions won't be met and it will go to the else statement and run 10 times 6 which is 60 as the output now the final thing that we Implement in this course is while Loops as well so how do you run a while loop I can say while a is less than 15. do and then we have to make a statement so do following a statement with following the do keyword we have to give a statement like make a equals a plus one so incrementing is value by 1. and this will print the variables list in every iteration of the loop so that's all there is to it in this course we haven't talked about more advanced functionalities like strings or possibly even styles of programming which we could Implement in our language like object oriented programming or functional programming we haven't talked about those aspects because we want to keep this course simple while also learning about a lot of other computer science Concepts like data structures algorithms and the programming paradigms and lots of stuff so we're gonna talk about all of those Concepts from a beginner level and by the end of this course we'll have this amazing project built from scratch so let's begin this course so I'm going to start this course by talking about how computers are able to read code on a high level now this is not really necessary to understand the implementation phase of this course where we will be dealing with python code and writing our interpreter but I just think that this help would helps put things into perspective and explains how computers are able to actually do this stuff in a more physical manner so ultimately um if you've scaled the microscopic level you'll find that computers are made of transistors and they are indicated by the following symbol so um the two wires over here are electrodes and in between is a material known as a semiconductor a semiconductor typically made of silicon or I'm not really going to go into the chemistry but but it composes of silicon and semiconductor is a material that allows current to flow through given a certain input from the control wire so this is the control wire when the control wire is on the semiconductor will allow current to flow through it when the control wire is off the semiconductor won't allow current refer to it so let's represent um these states on and off using two numbers zero and one also known as binary so when the control wire is on the semiconductor conducts electricity and if this let's say is the output so then and let's consider the control wire as the input so we have input and output and we have a way to represent data using binary so over here input is 1 the control wire is on the semiconductor can conduct electricity and it will generate the output 1 and if it is off the output will also be off Now using transistors we can generate logic gates there are multiple ways to generate logic gates um you can use different mechanisms as well but typically inside a computer you'll find transistors and transistors forming the logic gates so how do you make a transition how do you make a logic gate from a transistor I'm going to start off with a simple logic gate which is known as the not gate in fact there are three of them there's the not the and and the or which are the fundamental logic gates that computers use they are also known as Boolean operators that we will be working with during this course during the implementation so not essentially reverses the input so if the input is one a not operation turns it to zero the input is zero the not operation turns it to one and this not gate is built from transistors one way you can build a not gate is okay let me draw that again you can have the output go in that way and have the control y over here so control bar is the input and that's the output so over here input and output if the input is 1 that means current will flow in this direction it will go down and flow in this direction so the output will be zero if the input is zero the output will be 1. and what I'm drawing over here these tables so I know why that's not working yeah there we go so these tables are also known as truth tables they're one of the first things you actually taught in computer science anyway so it's it's just like four cells in an Excel sheet anyway so that's what the not gate does similarly you can construct the and and the nor gate using just combination of transistors I'm not going to go into those details but all you need to know is that the not gate flips the input operation the or gate has a different sort of truth table for a certain input it has two inputs i1 and let's say I2 and one output if both of them are on this will be on if one is on it'll still be on but if both of them are off the output will be off so that's the or gate the and gate is um another logical logical operator it has two inputs i1 and I2 and it has an output and only if both i1 and I2 are on will the output be on otherwise the output will be zero so that's the basics of you know and Gates not Gates and or Gates these form the fundamental logic gates there are some other Gates as well like the X socket but I'm not going to talk about them right now these Gates instead of representing them as transistors we represent them as their own symbol so let me just draw their own symbol and that's the or gate 3 Gates three simple so how do computers perform arithmetic with these logic gates there are two main functionalities we're looking for arithmetic and memory and once we understand both of them I'm gonna move on to the more um the side of this course which is more relevant to creating your own programming language so again as I said we have two operations to accomplish arithmetic which is basic algebra or uh not basic algebra basic addition subtraction multiplication or Division and the memory is refers to storing data essentially storing binary and then we can use the binary to represent other forms of data like for instance video or audio or whatever so anyway before we move on to the arithmetic I want to talk about another gate which is the Excel gate I'm now going to talk about how it's formed this is its symbol it's similarly or gate actually and that's you know yeah there we go so that's how the xorbit looks like and it looks similar to the or gate it too has two inputs I2 and i1 and the truth table and the truth table is something like this if both are 0 the output is zero if either one of them is on the output is on or one and if both of them are 1 the output is zero that's the xor gate now in binary you can also perform addition so what I was referring to as arithmetic let's say you want to add the binary numbers one and one so you would have to represent them as 1 0 essentially uh how do you how you represent binary numbers is the following you you represent decimal numbers which is the base 10 notation in forms of in the form of powers of 10 so for instance 10 to the power 0 10 to the power 1 10 to the power 2 and so on so that's the ones position tens and hundreds position and so on that we've been learning in elementary school so if you have let's say uh the number 3 4 6 so that just corresponds to 3 times 10 to the power 0 plus 4 times 10 to the power 1 plus 6 times 10 to power 2 which is 643. so that's how we represent um decimal numbers but binary is different it is base 2 so you have 2 to the power 0 2 to the power 1 and 2 squared and you can't have numbers from 0 to 9 it's not base 10 you have you can have numbers only from 0 and 1 so 0 and 1. so if you want to represent the number three let's say so that'll be 0 over here one over here a little bit it'll be one over here 1 over here and 0 over here so 0 1 1 is 3 how so um let's do that so it's 1 times 10 to the power 0 plus 1 times 10 to the power 1 plus 0 times 2 to the power 2 and this turns out to be 3. so that's how you do uh basic addition in in binary so so that's how you do basic addition in binary now if you look at the xor gate the truth table of the xor gate it actually looks pretty similar to our Edition so I'm gonna do addition for uh one bit numbers only just to demonstrate the idea of arithmetic using logic gates so I'm gonna do 0 plus 0 which is zero zero plus one which is zero one plus zero which is one okay and the previous one is also one and one plus one as I said it's not going to be um two it's going to be one zero and one zero essentially refers to the fact that it's two to the power zero and two to the power one so you have a 0 over here and one over here so that's just 2 to the power 1 times 1 which is two and one zero equals 2 in decimal so now if you look at this over here you can see that the sum which is the first number is exactly the same as the xor gate it's 0 1 1 0 0 1 1 0. but the carry bit which is this one over here that's called the carry bit because it becomes like like in decimal Edition you take one as the carry so you take one over here and then that comes down over here as one so on the carry bit is one that we have to we have to find another gate for the carry bit the carry bit is zero in these cases but one over here and that's the situation for the and gate so if you can combine the or gate the xor gate and the and gate you can actually come up with a way to handle addition in um using logic gates only and that's essentially how we go that's the starting point you go from there uh try to handle more complicated arithmetic like multiplication or Division and it's the that's the same basic idea you are trying to hook up logic gates in a smart manner to perform calculations and that's how arithmetic Works in computers so now let's talk about memory I'll be using a component called the and or latch as an example to show how computers have the ability to store data so it's called the and on latch as I said so let me just construct it so R stands for reset s stands for set and O is the output so we use the set um wire to save data so if you want to say save the num save the binary number one or just want to save the bit one so how do we do that so the first thing we need to do is turn on the set wire so I'm going to draw a truth table over here so we have set reset and let's not worry about reset for now and output so if you put set as 1 and reset as 0 what will happen to the output so this is an odd this is an or gate over here so on or gate so once once you turn on the set wire one gets transmitted over here because if either of the inputs of the or gate are 1 the output of the or gate is one so the output of the or gate is one over here and the reset is zero so zero gets knotted and that becomes one so now the and gate converts these two input ones into the output one so in the following scenario where in where you turn on the set wire and leave this reset wire as it is the output turns out to be one now afterwards if we turn off the set y let's turn off the set wire so let's make it zero and there is the reset wire is still zero what will happen to the output so now the set wire is zero but regardless since the output is one and that was fed back as an input to the or gate this is one over here and since one of the inputs is one to the auger this Still Remains as one this uh uh the input to the and gate the right the bottom input to the and gate Still Remains is one and therefore the output Still Remains as one so the output is effectively unchanged even after we uh stopped even after we turned off the set wire and that is effectively what computer memory looks like so the computer has remembered the state it was in and that is known as saving or computer memory you can delete it by turning on the reset key and that's also pretty easy to follow if we turn on the ezt that gets knotted to zero and one of the inputs of the and Gates becomes 0 and the output becomes zero and we are back to our original state so that's how basically computers store memory it's solve this feedback mechanism that they use that helps them store memory and now that we've understood these two concepts let's go ahead to the next section where we will talk about the need for programming and why we need to use programming languages so what is the need for programming that's what we're going to talk about in this section of the course now programming refers to giving input instructions to come to a computer so that we can execute them so before we understand how programming works we have to understand how computers understand instructions that's a mouthful let me say that again to understand how programming works we need to understand how computers interpret instructions and as I said all all along that they do this using logic gates so we have a set of logic gates hooked up in a smart way so that they can detect certain instructions like for instance if we have the instruction one zero zero zero so that's the 4-bit instruction over here now how do we decode this the computer has a control unit which is responsible for checking for certain instructions looking for certain instructions and once it finds those instructions it will perform the necessary action so that's how computers interpret instructions they have a control unit control units composed of logic gates so let me just say you have the instruction like this so this is the instruction it's fed into control unit and the control unit um detects the instruction checks if uh say for instance the instruction one zero zero corresponds to uh adding and adding to memory let's say so the control unit will check for that instruction if the instruction is one zero zero and again these instructions are stored in the memory of the of the computer we talked about how computers uh save Things So the instructions are stored in the memory of the computer so um once it once it so it sequentially goes through all of the instructions stored in the memory and let's say this is the instruction at first encounters and it tries to match it with the instruction one zero zero zero and let's say in this situation where it does match with this number uh this number let's say the instruction is one triple zero in that case the control unit will detect it using a logic gates and once it has detected that instruction it will go ahead and perform the operation of saving to memory so this two stages detecting the instruction using the control unit and then performing the uh instruction so you have a table like currents corresponding to 1000 uh corresponds to saving to memory maybe zero one zero zero corresponds to doing arithmetic adding two numbers let's say so you have a table of these instructions so you first design these instructions and then computers programmers will go ahead and input these instructions into the computer's memory the computer would sequentially perform each instruction and will execute them so this is how Computing works on a really high level and the problem arose and this is in the early ages of computing the problem arose was that the problem which arose was that you used harder mediums which is like Hardware interactions to input data these instructions like one zero zero uh were fed in by mechanisms like switches or plug boards or um you had some Punch Cards so these harder mechanisms are not really effective especially if you have thousands of instructions to give to the computer and that resulted in the need for a softer way a softer medium for interacting with a computer and that gave birth to a compiler a compiler converts you have high source code high level source code which is essentially a programming language like Python and that source code is written in such a way that it's human readable and you have a compiler which is just another computer which uses a set of techniques to convert that source code into binary which is also known as a machine code so that's the job of a compiler can converts source code into machine code and in this entire course we'll be learning about a lot of processes that compilers use to convert to machine code namely lexical analysis and um passing and interpretation we won't be building a compiler we'll be building an interpreter so we won't be dealing with converting the instructions into binary outputs rather we'll be using a programming language like python to convert to to interpret data in sequence to input interpret our own programming language sequentially so a compiler converts to machine code but our interpreter won't be doing that it will be using a programming language like python to understand our given input and then process it and perform the necessary action so anyway that's it for this section we've talked about how computers understand instructions how they execute those instructions and the need for compilers to convert high level code to low level code and those low level codes is just binary instructions and now with that information it's time to move ahead to the next section where we will be delving more into the implementation side of this course so I'll see you over there in our interpretation process there are three steps involved the first step is known as lexical analysis and then after that you go ahead and do parsing and the final stage is the interpretation which is done by The Interpreter so three steps and if you encapsulate these three steps into its own box so I have an interpreter over here and that box is called an interpreter and if I input some code which is in our own in our custom programming language and for the purposes of this course this programming language will be called Shadow script which is a fancy name for a not so capable language um because of course we won't be able to achieve tasks that advance programming languages like python or JavaScript can do but nonetheless we'll be talking about the core functionalities like variables like arithmetic operations um uh some if statements while loops and possibly even for Loops so all of that code will be implemented it won't be as powerful as the modern programming languages but still it's a great great opportunity to learn lots of programming Concepts so anyway this interpreter will take high level code and in our in our programming language it will take Shadow script in it so it will ingest Shadow script and spit out um the necessary actions the necessary output like say for instance you're creating variables it will spit out the value of the variable and save the variable if you want to do some arithmetic operations it will it will do the arithmetic for you and spit out the output and so on so they'll just give an output and it may also deal with some of the memory processes like saving new variables deleting variables and all of that so it produces an output which will be visible on the shell uh on on the terminal and it will also be dealing with some memory changes so it's these three steps that um okay I don't know what happened over here let's just fix that so it's these three steps that we have to talk about because this is this is what goes on inside the black spot black box and the first step is lexical analysis so I'm going to create a new page for this one and this is known as lexical analysis the process is pretty straightforward easier than the other steps and it involves just breaking down the input code into specific tokens now the tokens for our purposes can have a value and also a type like for instance the number five has a value of five and So that's its value and a type of integer the plus operator has a value of Plus and it's a type of operator so oh so the lexical analysis just breaks down the tokens breaks down the input into tokens and that's pretty much it that's all it does and in the next stage we'll feed the tokens as an input to the parsing so the input of the lexical analysis is the original code the output produced from lexical analysis will go to the parser and the output of the parser will go to The Interpreter and The Interpreter will give the final output so lexical analysis you give an input and I'm going to call it tokenization because lexical analysis is pretty hard to say and they mean the same thing so yeah so tokenization you give an input which is the shadow script code so tokenization and it will break down the input code into specific tokens and in our case it will store those tokens in in a python list I'll talk about data structures in just a few moments but probably in the next few sections but just know that it will store the tokens in a sequential way in a list so list is just like a traditional list token one token 2 and so on so that's a list and this is the first item of the list this is the second item of the list and that's how a python python list looks like now to give an example let's say the input is the arithmetic operation one plus three so we want to add the numbers one and three so after the tokenization process the output should look something like this it should store the token value of 1 and of type integer then it should store the token okay let's put it below so first we'll store token one and it will be a type integer then it will store a token the plus symbol or add symbol and that's an operator and so on and the third final one is the third token which represents the integer three so that's the output of the lexical analysis stage and now let's go to the next section where we will talk about how we process these tokens this stream of tokens how we process them so the next step is called parsing so from the tokenization process we have received a bunch of tokens stored in a python list and now we have to process that that list and generate a useful output so what is parsing first of all let's understand the definition of parsing and fundamentally parsing is just the recognition of patterns in an input like say for instance we have the input one plus one and in a very simplistic uh interpreter for our programming language we can we can interpret this expression by first tokenizing it so we tokenize this into a bunch of tokens we have the number one and we have the plus symbol and then we have the number one again and now once we have these tokens in the list in the parsing process we look for a pattern in a simplistic way let's say The Interpreter can process only the addition or subtraction of two singular digit numbers like for instance one plus one or three plus four or five plus six or something like that in this case the pattern that the parser has to recognize is that the first number is an integer the first okay wrong the pattern that The Interpreter has to recognize is the first number is an integer or the first token is an integer the second token is an operator and the operator could be plus or minus and the third token is again another integer that's all that the parser has to recognize and the simplistic scenario there's an integer plus or minus another integer and the parser just the remember the lexical analysis of the tokenization process blindly converts the input into a bunch of tokens it does not look at any structure it does not see if the input abides by the programming language rules it doesn't do anything like that all it does is converts the input into a bunch of tokens but in the parser it tries to search for patterns it tries to search for this pattern of integer integer plus integer so that's the general pattern so integer plus or minus and this vertical bar is also known as r so plus or minus another integer so that's the pattern that we're trying to look for in the simplistic scenario of course this is not how our parsing process will be in the in the implementation side we will be able to deal with extremely complicated Expressions as well but this is just to give an example of what parsing really is all it is is trying to find structure and um essentially it goes a step further from the lexical analysis and tries to structurize the input how does it do that and it does this using a bunch of grammar rules now in this simple scenario the only grammar rule grammar rule is that an expression is an integer plus or minus another integer in fact I'll write this in a better way so so it's an integer plus an integer integer minus an integer and the vertical bar here so either this is one possibility or this is the other possibility so in this simple grammar Rule and this is the this layout is known as a grammar rule there are certain formats of writing a grammar Rule and I'll come to that in just a second but this grammar rule essentially defines the structure of a programming language the parser checks for certain patterns check for such checks for such structures and C's if the input matches for them if it does not match them then there's probably an error in the input core and an error must be raised so let's talk about let's now talk about the types of grammar rules that exist so the ways in which you can write grammar rules the the one I'm going to use is called P and F and that's extremely hard to pronounce but it's called Backus nor form I believe and I probably misspelled mispronounce that but anyway B and F is a way of writing your grammar rules and let's just go through a bunch of examples now in basic math we have let's say an expression an expression just contains a bunch of terms and a term is just a bunch of integers multiplied together so what does that mean so let me let me go through this again I'm going to say an expression is equal to an in Bacchus now form you actually have two colons over here like that but I'm just going to write it as one colon because it looks much more prettier so like that expression is equal to a term plus another expression or it's equal to a term minus another expression or it's equal to just a term now this may seem a bit confusing because we're calling the expression inside the expression but what this tries to do is just ensure that there is a form of continuity in our expression what does that mean so let's say we have the expression 5 plus 3 plus 2. now that's an expression It's a combination of terms and a term can be an integer or it can also be two integers multiplied or divided but I'll come to that in just a second for now let's just assume a term is an integer so an expression just contains a bunch of integers added or subtracted together now this is where my recursion comes into play and I'll talk about recursion in probably the probably the next few sections but essentially recursion is a way is a type of programming where you're calling the function a certain functionality within the functionality so as you can see over here we call the expression inside the expression itself so what this aim what this tries to do is that it tries to ensure continuity so over here the term let's say the first term is 5 plus another expression our expression can be equal to a term plus another expression so we can say 3 so the three is a term so we can have three plus now expression can be a term as well so 2 is a term so all we're trying to do is we're trying to ensure continuity so that we can enable multiple editions over here so even if there are thousand integers added together we can ensure that all of them uh uh conform to the grammar rule because this is a pretty sound and pretty uh grammatically correct expression five plus three plus two going on for n number of um terms if you still don't understand what I mean I'm gonna use a much more easier comprehensible format so another way to define an expression would be to say okay its expression is a term multiplied or divided so you see we use the bar in fact I'm just going to use the symbols so it's a term multiplied or divided by another term like that now what does this mean this means that an expression can be equal to just one term or it can be equal to a term multiplied or divided by another term actually that should be plus or minus that's my band plus or minus so an expression can be a term by itself but this part over here where we are enclosing uh let me just get that right these two enclosures over here followed by an asterisk represents zero or more times that means now let's clean up that yeah that means this uh the contents within that curly brace can be repeated zero or more times the final that means a term can be a term plus another term minus another term plus another term whatever zero zero or more times then that is effectively what the Backus now form is saying it's calling the expression within itself to tell you that okay an expression can contain n number of Expressions so we're using a form of recursion to to tell the users or to tell the programmers that an expression can have n number of integers n number of terms added a subtracted multiple times now let's define the term so a term as I said it can be equal to just another term or it could be equal to a term it could be it could be equal to a factor now a factor is just an integer for our purposes so a term could be equal to a factor multiplied by another factor or multiplied by another term and you see I'm doing the same thing over here recursion and just to symbolize the same thing as I said the multiplication that means the term can be equal to a factor multiplied by another Factor multiplied by another Factor n number of times zero or more times so a term could just as well be equal to one factor it could be equal to two factors multiplied together three factors multiplied together and so on so that's what I'm trying to signify by calling the term within the term itself so by calling term over here I'll just get the out of her drone for some reason so by calling the term within the term grammar rule I'm trying to Showcase that you can have multiple factors multiplied or divided together so as I said a factor can be multiplied by another term or it could be a factor divided by another term or it could be just equal to a factor now what is the factor for now although in the later part of this course I'll bring in another aspect to this factor definition but for now a factor is just equal to an integer so it could be yeah let's just do integer like that an integer is just a series of digits but I'm not going to go ahead and Define that because I'm pretty sure all of you know what an integer means now in the set of grammar rules we have some terminology as well so the integer although it technically isn't and okay let's just go ahead and do it so I'm going to say an integer is for now let's just say the integer is singular digit so I'm going to say in integer is just a digit 0 0 or 1 or 2 or 3 or 4. right so an integer is just a singular digit and I'm defining that in this way just to keep things simple as you all know a digit an integer can be a series of digits like three four five is an integer or one two three is an integer and so on so in this notation in this in these grammar rules what is inside the angular brackets is called a terminal is called a non-terminal and that essentially means that a non-terminal can be defined by by using a terminal so a terminal in this case is something like an integer which we can concretely Define like a digit uh another terminal in our programming language could be the operator like plus or minus is another terminal these can be concretely defined they are the base tokens of our um of our language so with that knowledge of Bacchus in our form how does all of this relate to parsing so as I said backers now form is a way of defining rules once we have defined rules things actually get pretty simple it seems that you're still far away from having a workable interpreter which can convert your custom program and programming language and execute it it seems that we're far away from that goal but now that we've defined our grammar rules things get a lot more easier so now I'm going to take an example to Showcase how parsing is actually done now that we have the background knowledge of BNF and also about how parsing introduces structure so in passing let's say we're trying to pass the input 1 plus um let's say one now the expression using our previous notation we said that the expression is equal to terms added on added or subtracted from each other now a term can be equal to a factor a factor being just an integer so in this case the expression expression is equal to um one which is a term plus another term one plus one and that satisfies our grammar rule but even if we have something complicated let's say three plus five times two the grammar rule says that an expression is a term plus another expression so essentially saying that again I'm repeating myself but an expression is just a term added or subtracted from another expression n number of times zero or more times so the expression over here is equal to 3 3 is a valid um term which is equal to a factor in this case and even 5 times 2 is a valid term because remember we said the term can be equal to a factor multiplied by another Factor what zero or more times so this is equal to a factor multiplied by another Factor so even this input is possible using our given grammar rules what do I mean by possible that means that it conforms to our grammar rules and such an input can be processed in the parsing stage now I'm going to use a data structure known as a binary tree which I'll talk about more extensively in the next few sections but I'm gonna introduce the binary tree to talk about parsing now parsing essentially involves uh again bringing structure into your code so let's say we have the expression one plus two times five I'm going to construct a tree for this given input and the tree is the following so this tree over here we are adding the integer one and we're adding the product of 2 and 5 and this is the past Tree in fact the goal of the passing process is to generate a tree like this of course we won't be drawing a tree like this in Python but we will be representing such a tree in a list so how do we represent this tree specifically I'm going to write it over here so I'm going to say one Plus two times five like so so in this scenario plus is the operator one is the left node in fact this is known as the root node I'll talk about this terminology in just few sections but this is the left node and this is the right node so over here one is our left node plus is our root node and right node is 2 times 5. so we have effectively represented this tree using this list in Python in this section of the course I'll be talking about object oriented programming and its four main pillars that make it according to me at least more Superior than the other paradigms or the other ways of programming so the first way is functionally functional based programming which uses po functions and functions is just a method like similar to how mathematical function works you have an input you give out an output that's basically it there's some inner workings to a function um it's like a black box you have an input you have an output but you don't see what's inside so that's how functions work and just to give an example let's say we're trying to calculate the amount of fuel that you have left in your car so we create a function called remaining underscore field and this takes in the initial field that you had the kilometers that you drove and also the ratio which is like the gallons per kilometer or whatever unit you're using liters per kilometer um the ratio of that and the remaining fuel would just then be equal to initial minus kilometers times the ratio so in functional programming or functional based programming you separate the data from the actual functionality you see that we have not accepted any data like the initial fuel that he had the kilometers that he had or the ratio we didn't accept any data this is just a standalone function a standalone functionality which can uh basically work fine I mean even if I run this right now there'll be no problems so uh it can work on its own as well so the actual um usability of it comes when we add some data and the essence of functional based programming is that you separate the data from the functionalities now over here I can give in some data let's say uh let's say I say the initial fuel is equal to 50 units the kilometers this person drove is um let's say 10 units or okay 10 kilometers and the ratio let's say is 5 units per kilometer five units of fuel burnt per kilometer drove so then I can calculate the remaining fuel I can print it as well remaining Fuel and I can give the parameters initial kilometers and ratio and this works fine so if I run again this time you see we have okay we have 0 exactly zero let's reduce the ratio here a bit and let's run that again you see we have 30 units of fuel still left so this is a pretty basic example of functions and it seems fine in this level but let's assume that you have a lot of complexity in your application let's say you're building a website and you have the authentication aspect of it you have the payment aspect of it you have the content management aspect of it so many entities uh involved now how do you because if you have so many entities involved you'll have several functions you'll have authentication functions you'll have um payment functions and all of them clubbed in this messy file uh where it just becomes difficult to handle things and that's where object oriented programming comes in object oriented programming divides it divides um code into entities called objects so you can have one object of payments you can have one object of uh of authentication one object of content management one object of uh maybe like contact or server where you have chat functionality or something like that I don't know so and in object oriented programming you divide an application into objects these entities which have their own um data and their own functionality now let's try to understand how object-oriented programming works on the canvas so as I said just as a recap let's just recap what I said uh in operator into programming also called oop you divide things into their own entities called objects so you can have maybe an object for payments so it has its own data it has its own functionality but it's all enclosed in this own entity known as payment entity we can have another entity for let's say authentication which has its own set of data its own set of functions completely different from the payment and you have authentication over here and you can go on and on but you get the point your um encapsulating data and functionality into objects which are the basic unit of object oriented programming and these objectives represent entities like payments and Authentication now in object oriented programming and inside an object the data is referred to as properties and the functions are referred to as methods so now that we've understood the basic gist of object oriented programming let's try and understand what are the four main pillars that hold it and make it such a solid way of writing your code so the first main pillar of object oriented programming is known as encapsulation and it's the basic idea we talked about in the previous section encapsulation refers to basically just start encapsulating uh Properties or encapsulating data and functionality into their own entities so that we can better handle them and it just enables us to write more cleaner code so as an example of our own situation over here we have multiple entities we have the lexor we have the parser we have The Interpreter so it seems wise to use object oriented programming because it can encapsulate each of these entities and have allow designate them their own properties their own methods their own data their own functionality so we can have Alexa which has its own data its own functions or methods there's alcohol functions for now because yeah so the data is known as properties and functions and object-oriented programming is called method are called Methods so this is one object you can have another object you can have a parser which has its own data its own functions and you get the idea we can have a similar class for The Interpreter this is called encapsulation by your grouping things grouping data and grouping functionality the second pillar is known as abstraction an abstraction just refers to simplifying the inner functionality the inner yeah basically just that the inner functionality into its own entity like for instance in our situation over here all we need to do an object oriented programming is create a Lexa which is of the object Lexa and let's say we tell it to let's say we tell it tokenize stuff uh using the methods so this is some notation which I'll go over in just the next section but all this is doing is creating an object called Lexa and it's tokenizing the inputs uh the input expression so as you can see this is a much this is a very clean way of writing code because essentially you're just abstracting away the inner complexities uh and the inner functionality of the Alexa all you're doing is calling the tokenize method and you have your tokens ready all you're doing is create calling the lexor object and you have your own lecture so that is known as abstraction it allows for core reusability the loss for modularity and essentially if you have different developers working on different tasks and they really don't know how the other developers developer is coding his his stuff so they can use object oriented programming to communicate they can say okay this is one component this this component will give a certain output you don't need to know how it works inside it's a black box you don't to know how it works on the inside but all you need to know is for a given input you're getting this output so that's what abstraction is abstracting away the complexities making code looks simple and let's move on now to the third pillar the third pillar of object-oriented programming is known as inheritance inheritance is a concept that is best explained by an example so let's say we are creating a website and each website has different components it has the home page the about page and the home page has different contents in it the about page is different contents in it but if you're designing a website you typically would have the same layout for each page you'd have the heading you'd have the body in pretty much similar positions so inheritance in object-oriented programming allows you to inherit certain common properties within object so what do I mean by this let's say we have our an object called page it has properties like heading body and the images may be so this is the base class this is um this is what you are inheriting from whereas you have maybe another class like another class like the home class which is the home page or the about class which is the about page and um these classes will inherit inherit all of these properties The Heading all of these will be inherited by the Home Home object and um yeah so again the home object would inherit the properties and the functions of the page object while having its own custom functionality which will which it will add over the page object so that's basically what inheritance is you have a fundamental object and other specific implementations of it like the home page the about Pitch inheriting from that fundamental object this is a theoretical discussion here I hope you understand what inheritance mean means in a more theoretical sense in this course I'll be doing object-oriented programming in Python so um I'll be talking about how you implement inheritance and all of that as well and with that knowledge let's move on to the final and fourth pillar which is known as polymorphism so the fourth main pillar in object oriented programming is known as polymorphism and polymorphism is essentially the idea that so polymorphism is essentially the idea that you can treat object of different types so objects of different types as if they were the same type so that may sound a bit confusing but I'll explain it using an example so let's say we have three types of animals we have dogs we have cats and we have Birds now the common thing about all of these animals is that they can make a sound so how can we program a way in which we can how can we program the sounds of these animals so maybe for the dog we do make dog sound so this is what you would typically do but now in polymorphism the idea of polymorphism is that you can make a common interface now in this instance the common interface would be an animal all of three all of these three entities are a type of animal so the common interface over here I can Define it to be an animal polymorphism allows you to do is let's say we create a method in animal called make sound and now using this common interface you can add some custom functionality to this make sound method so maybe the dog does a woof upon the make sound method the cat does a meow on the make sound method and the bird does a chirp on the mix on method whatever but polymorphism allows you to call the same functionality of the dog and the cat and the bird using the same method but each of these entities dog cat embed do i

Original Description

Make your own programming language while learning advanced Python. ✏️ Course from Aryaan Hegde. Aryaan's website: https://blog.algolearn.net/ 💻 Code: https://github.com/VOYAGERX013/ShadowScript ❤️ Try interactive Python courses we love, right in your browser: https://scrimba.com/freeCodeCamp-Python (Made possible by a grant from our friends at Scrimba) ⭐️ Contents ⭐️ (0:00:00) Intro (0:07:05) Logic gates (0:12:14) How computers do arithmetic (0:17:37) Computer memory (0:21:02) Programming (0:26:25) Lexical analysis (0:32:43) Parsing (0:49:33) Object-oriented programming (0:55:15) Encapsulation (0:56:34) Abstraction (0:58:29) Inheritance (1:00:49) Polymorphism (1:03:07) OOP in Python (1:11:53) Class variables (1:14:51) Class methods (1:19:36) Static methods (1:21:50) Inheritance in Python (1:32:48) Lists (1:42:42) Tuples (1:46:32) Dictionaries (1:49:46) Stacks (1:54:19) Binary trees (1:56:13) Tree traversal techniques (2:03:36) Interpreter (2:08:26) Binary trees in Python (2:11:39) Preorder traversal (2:20:01) Postorder traversal (2:23:40) Recursion (2:35:06) Lexer in Python (2:57:16) Parser in Python (3:10:08) Interpreter in Python (3:32:10) Brackets in expressions (3:34:17) Variables (4:02:42) Unary operations (4:11:15) Boolean and comparison operator (4:26:28) If statements (4:33:34) While loops 🎉 Thanks to our Champion and Sponsor supporters: 👾 davthecoder 👾 jedi-or-sith 👾 南宮千影 👾 Agustín Kussrow 👾 Nattira Maneerat 👾 Heather Wcislo 👾 Serhiy Kalinets 👾 Justin Hual 👾 Otis Morgan -- Learn to code for free and get a developer job: https://www.freecodecamp.org Read hundreds of articles on programming: https://freecodecamp.org/news
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from freeCodeCamp.org · freeCodeCamp.org · 0 of 60

← Previous Next →
1 React: Production Server Setup Part 2 - Live Coding with Jesse
React: Production Server Setup Part 2 - Live Coding with Jesse
freeCodeCamp.org
2 cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
cookies vs localStorage vs sessionStorage - Beau teaches JavaScript
freeCodeCamp.org
3 Browser history tutorial - Beau teaches JavaScript
Browser history tutorial - Beau teaches JavaScript
freeCodeCamp.org
4 Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
Graph Data Structure Intro (inc. adjacency list, adjacency matrix, incidence matrix)
freeCodeCamp.org
5 React: Parameterized Routing with Next.js - Live Coding with Jesse
React: Parameterized Routing with Next.js - Live Coding with Jesse
freeCodeCamp.org
6 React: Dealing with jQuery Issues - Live Coding with Jesse
React: Dealing with jQuery Issues - Live Coding with Jesse
freeCodeCamp.org
7 setInterval and setTimeout: timing events - Beau teaches JavaScript
setInterval and setTimeout: timing events - Beau teaches JavaScript
freeCodeCamp.org
8 Browser and Device Testing - Live Coding with Jesse
Browser and Device Testing - Live Coding with Jesse
freeCodeCamp.org
9 Last Minute Updates - Live Coding with Jesse
Last Minute Updates - Live Coding with Jesse
freeCodeCamp.org
10 Post Launch Updates - Live Coding with Jesse
Post Launch Updates - Live Coding with Jesse
freeCodeCamp.org
11 React: Setting Up Google Analytics - Live Coding with Jesse
React: Setting Up Google Analytics - Live Coding with Jesse
freeCodeCamp.org
12 React: Masonry Layout - Live Coding with Jesse
React: Masonry Layout - Live Coding with Jesse
freeCodeCamp.org
13 Load Balancing Digital Ocean Droplets - Live Coding with Jesse
Load Balancing Digital Ocean Droplets - Live Coding with Jesse
freeCodeCamp.org
14 try, catch, finally, throw - error handling in JavaScript
try, catch, finally, throw - error handling in JavaScript
freeCodeCamp.org
15 Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
Load Balancing: SSL Passthrough Setup - Live Coding with Jesse
freeCodeCamp.org
16 Graphs: breadth-first search - Beau teaches JavaScript
Graphs: breadth-first search - Beau teaches JavaScript
freeCodeCamp.org
17 React: Masonry Layout Part 2 - Live Coding with Jesse
React: Masonry Layout Part 2 - Live Coding with Jesse
freeCodeCamp.org
18 React: WordPress API Live Search - Live Coding with Jesse
React: WordPress API Live Search - Live Coding with Jesse
freeCodeCamp.org
19 Creating WordPress Custom Post Types - Live Coding With Jesse
Creating WordPress Custom Post Types - Live Coding With Jesse
freeCodeCamp.org
20 Dates - Beau teaches JavaScript
Dates - Beau teaches JavaScript
freeCodeCamp.org
21 Miscellaneous Front End Updates - Live Coding with Jesse
Miscellaneous Front End Updates - Live Coding with Jesse
freeCodeCamp.org
22 Merging a Pull Request from GitHub - Live Coding with Jesse
Merging a Pull Request from GitHub - Live Coding with Jesse
freeCodeCamp.org
23 React + Prettier + Standard JS - Live Coding with Jesse
React + Prettier + Standard JS - Live Coding with Jesse
freeCodeCamp.org
24 React: Sortable Responsive Table - Live Coding with Jesse
React: Sortable Responsive Table - Live Coding with Jesse
freeCodeCamp.org
25 Geolocation Sorting by Distance - Live Coding with Jesse
Geolocation Sorting by Distance - Live Coding with Jesse
freeCodeCamp.org
26 Tradeoff Matrix - Agile Software Development
Tradeoff Matrix - Agile Software Development
freeCodeCamp.org
27 The Definition of Ready - Agile Software Development
The Definition of Ready - Agile Software Development
freeCodeCamp.org
28 Getting first React job without experience - Ask Preethi
Getting first React job without experience - Ask Preethi
freeCodeCamp.org
29 React: Google Analytics Click Tracking - Live Coding with Jesse
React: Google Analytics Click Tracking - Live Coding with Jesse
freeCodeCamp.org
30 Submitting a PR to an Open Source Project - Live Coding with Jesse
Submitting a PR to an Open Source Project - Live Coding with Jesse
freeCodeCamp.org
31 Should I go back to school to get CS degree? - Ask Preethi
Should I go back to school to get CS degree? - Ask Preethi
freeCodeCamp.org
32 Hero Section CSS Changes - Live Coding with Jesse
Hero Section CSS Changes - Live Coding with Jesse
freeCodeCamp.org
33 Working Agreement - Agile Software Development
Working Agreement - Agile Software Development
freeCodeCamp.org
34 A day at Pennybox with Co-Founder Reji Eapen
A day at Pennybox with Co-Founder Reji Eapen
freeCodeCamp.org
35 React: Sorting and Filtering Data - Live Coding with Jesse
React: Sorting and Filtering Data - Live Coding with Jesse
freeCodeCamp.org
36 React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
React: Sorting and Filtering Data Part 2 - Live Coding with Jesse
freeCodeCamp.org
37 React: Building a New UI - Live Coding with Jesse
React: Building a New UI - Live Coding with Jesse
freeCodeCamp.org
38 Definition of Done - Agile Software Development
Definition of Done - Agile Software Development
freeCodeCamp.org
39 Getting started with jQuery (tutorial) - Beau teaches JavaScript
Getting started with jQuery (tutorial) - Beau teaches JavaScript
freeCodeCamp.org
40 Making a React Blog with WordPress Content - Live Coding with Jesse
Making a React Blog with WordPress Content - Live Coding with Jesse
freeCodeCamp.org
41 React, NextJS, CSS - Live Coding with Jesse
React, NextJS, CSS - Live Coding with Jesse
freeCodeCamp.org
42 jQuery events - Beau teaches JavaScript
jQuery events - Beau teaches JavaScript
freeCodeCamp.org
43 React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
React/NextJS Routing and WordPress API Custom Types - Live Coding with Jesse
freeCodeCamp.org
44 React: Working with API Data - Live Coding with Jesse
React: Working with API Data - Live Coding with Jesse
freeCodeCamp.org
45 React: Refactoring Components - Live Streaming with Jesse
React: Refactoring Components - Live Streaming with Jesse
freeCodeCamp.org
46 jQuery effects - Beau teaches JavaScript
jQuery effects - Beau teaches JavaScript
freeCodeCamp.org
47 More React Refactoring - Live Coding with Jesse
More React Refactoring - Live Coding with Jesse
freeCodeCamp.org
48 animate in jQuery - Beau teaches JavaScript
animate in jQuery - Beau teaches JavaScript
freeCodeCamp.org
49 "Finishing" My React Site - Live Coding with Jesse
"Finishing" My React Site - Live Coding with Jesse
freeCodeCamp.org
50 Starting a New React Project (P2D1) - Live Coding with Jesse
Starting a New React Project (P2D1) - Live Coding with Jesse
freeCodeCamp.org
51 React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
React Project 2 Day 2: Learning Material UI - Live Coding with Jesse
freeCodeCamp.org
52 The Agile Manifesto - Agile Software Development
The Agile Manifesto - Agile Software Development
freeCodeCamp.org
53 jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
jQuery: get and set with http, text, val, and attr - Beau teaches JavaScript
freeCodeCamp.org
54 React Project 2 Day 3 - Live Coding with Jesse
React Project 2 Day 3 - Live Coding with Jesse
freeCodeCamp.org
55 The INVEST approach to product backlog items
The INVEST approach to product backlog items
freeCodeCamp.org
56 React Project 2 Day 4 - Live Coding with Jesse
React Project 2 Day 4 - Live Coding with Jesse
freeCodeCamp.org
57 Chickens and Pigs - Agile Software Development
Chickens and Pigs - Agile Software Development
freeCodeCamp.org
58 React Project 2 Day 5 - Live Coding with Jesse
React Project 2 Day 5 - Live Coding with Jesse
freeCodeCamp.org
59 jQuery: add and remove DOM elements - Beau teaches JavaScript
jQuery: add and remove DOM elements - Beau teaches JavaScript
freeCodeCamp.org
60 React Project 2 Day 6 - Live Coding with Jesse
React Project 2 Day 6 - Live Coding with Jesse
freeCodeCamp.org

This video course teaches you how to create a programming language and learn advanced Python concepts, including object-oriented programming, data structures, and interpreter implementation. You will learn how to design a basic programming language syntax, implement a lexer and parser in Python, and write a simple interpreter in Python.

Key Takeaways
  1. Design a basic programming language syntax
  2. Implement a lexer in Python
  3. Implement a parser in Python
  4. Write a simple interpreter in Python
  5. Learn object-oriented programming concepts in Python
  6. Implement data structures such as lists, tuples, and dictionaries in Python
💡 Creating a programming language requires a deep understanding of computer science concepts, including lexical analysis, parsing, and interpreter implementation. Python is a versatile language that can be used to implement these concepts.

Related Reads

📰
The Minecraft anvil is a tree-cost optimization problem in disguise
Optimize tree costs in Minecraft using graph theory and algorithms, just like the anvil repair system
Dev.to · Mark
📰
KMP Algorithm (Knuth-Morris-Pratt): The Smart Way to Perform String Matching in O(N)
Learn the KMP algorithm for efficient string matching in O(N) time complexity and improve your coding skills
Dev.to · Jaspreet singh
📰
Every Backtracking Problem Is the Same Three Lines. I Just Couldn't See the Tree.
Master backtracking problems with a simple three-line approach to improve problem-solving skills in coding interviews and challenges
Dev.to · Alex Mateo
📰
DSA From Zero to Hero #3: Sliding Window (Fixed Size) Explained With a Java Example
Learn to solve subarray problems efficiently using the sliding window technique, a crucial skill for software engineers and data scientists
Medium · Programming
Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →