Welcome back to “Let’s build a compiler”. In this episode we will build in our language the minus, times and division operators. My name is Yankee and I hope you have a lot of fun. Initially, we will update the specification of our compiler, which is also known by the name “CompilerTest”. Then we’ll go to our examples, that we want to support, copy one line as template and put down here some examples. In fact: What will we build? Firstly, we’ll have a subtraction, taking “3-2” for example, then we will have a 1 as output. Then we might as well build multiplication, taking “2*3”, so that should result in “6”, then Division, namely “6/2” as example of it, so comes 3 as output. Then, we can think later on what happens if no even result returns as output, for example, what should happen with “7/2”? Typically, when we deal with integers we cut the “.5” from behind, Until this moment, we’ve worked only with integers, later we naturally will have floating point types but for the beginning we continue with what we have and the result stays 3, without the .5 behind. Then one could naturally combine the operators: For example, if we have: “8-2+5”, then it is really important to calculate first the “8-2” (making 6) and thereafter “+5”, so we’ll have 11. In place of “2+5”, which would be 7 and so does “8-7”, which would be 1. The same happens to division, were we could take “8/2*4”, for example. “8/2” are 4, times 4 makes 16. Here again, another thing outcomes, if we calculate first the “2*4”, it would be 8 and then we would have “8/8”, naturally 1. Then we can also combine subtraction with the multiplication: We can take for example “2+3*3”, we should first calculate “3*3” (being 9), “+2” being 11, if we would first calculate “2+3”, then would be 5, times 3 would be 15, that’s another thing again. and the same, once again, with the subtraction: Like in the example “9-2*3”: “2*3” are 6 and “9-6” are 3. In reverse, “9-2” would be 7, times 3 would be 21. Then we save this and next… Ah, first we will check if our test fails already. Yes, we have many “Failures” here, 7 failures, these all are our new tests and here at the top we have many “Token recognition error”s, because ANTLR still does not recognize all these operators over here. So now we go next to our grammar and we will extend it with our new operators. At first, we rename now some things in our grammar. Because our “addition” rule will either way be expanded, to make it contain not only additions, but any math expression, for this I call the “expression” rule and then I will be using English terms consistently here, because at the moment it is a mixture of German and English terms and this is not pretty. Furthermore, I like the idea of translating this series to english and then it is very unpractical if there are too many German terms. After we have this done… After we have this done, we go again in our usual known way build our parser again, pressing “F5” on parser, then we go to the visitor and adapt our terms, this thing here is naturally called “right” now, “ZahlContext” doesn’t exist anymore, now we name it “NumberContext” with “Ctrl+Shift+O” we can fix the imports, and we save again and we’ll check later, if our tests are working just as good or bad as before. Yes, our first three tests are working and the new ones, those, that we generated just now, aren’t working anymore. We go back to our grammar and start now with the improvements. At first, we will build our “minus” operator. We’ll go here, copy our previous “plus” rule and insert an alternative for the “minus”. We call this “Minus”, we change the operator to “minus” here, we save, we return again to the console, we regenerate our parser anew, go to the parser, press “F5”, we go to the visitor, behind the visitPlus now we can insert a new “visitMinus” method, it works similarly to “visitPlus”, Now, here I simply copy the template, we use in place of “iadd”, “isub”, for subtraction. Wonderful, we close it and fire up the test run once more. What’s the result? In our minus test, the “println(3-2)” worked! Hey! That was simple! Let’s check if it is just as simple for the other operators. So we copy again our previous rule too, again for the division and multiplication, our parser must be generated again with the old known command, let’s go to the parser with “F5”, we go to our visitor and implement the new “visit” methods. For division the same again what we have had with minus, just with “idiv” standing for i-divide and then again, another for multiplication and there the instruction is called “imul”. We save, let our tests run and what do we see here? Eight of our tests worked, two didn’t. Times worked, division worked, the one with “7/2” also worked, “8/2*4” worked too, but, unfortunately these two didn’t work. And as you can see here: we have expected 11, but found 15, obviously, the operator order was not properly observed here, here “2+3” was calculated first, making 5 and then times 3, making 15, and it’s what we found here. but, sadly, this is not what we wanted. At this local it gives us a little Cliffhanger. You will learn how we manage correctly the operators priorities, so that our two remaining tests work, in the next episode. In this episode we learned how to build minus division and plus operators in the grammar and how we generate the corresponding instructions: We can define multiple operators using sub-rules and the matching instructions are generated with new visit methods, that look almost exactly like “visitPlus” method. On our next episode we’re expected by, as mentioned the right order of operators definition and after that we are expected by variables, loops, conditions, functions and much more. Write me in the comments, for what you want for the next episodes. Naturally, I am also looking forward to further comments: Do you have praise or criticism? How do you take advantage of the videos? Do you program along? Do you use the videos for research about concrete problems? Or do you watch the videos without any project, just for interest? Thanks a lot to Gadarol, who allowed me to record in his studio and thank you very much for paying attention. My name is Yankee, I hope you’ve had fun, goodbye and see you later for another: “Let’s build a compiler”.

hello yankee, thank you very much by the videos explaining the concept of a expression tree and the implementation in java bytecode.

I was working in a code compilator but i don't make it a easy task, i wrote my own tokenizer, my own tree parser, my own code analizer, my own bytecode generator and my own .class writer, too much effort to a such simple thing, println(3+2); of course, i learned a lot writing this modules, like how java language works, how bytecode works, and a lot about expressions and operators.

but watching your videos i just see that we don't need to re-wrote things that have already been wroted by someone-else and distribucted in a nice and complete library.

just a simple suggestion:

make a test when loading numbers,

if is less than 127

bipush <number> // this means put the next byte in stack like a integer

if is less than 32767

sipush <number> // this means put the next short in stack like a integer

else

ldc <constant>

/*

*this means put the constant at index of prox short located in constant_pool in stack

*/

(I am brazilian with no knowledge about deutsch and a little about english, the automatic youtube translator make some magic in some videos '-')

Thank you for these videos. I'm studying by them to complete homeworks and final program work in Compilers course.

Thanks for the videos. Excellent explanations. you are helping people all over the world! Student from Brazil here