{"id":811,"date":"2024-03-05T20:20:24","date_gmt":"2024-03-05T20:20:24","guid":{"rendered":"https:\/\/baldsolutions.com\/?p=811"},"modified":"2024-03-05T20:22:19","modified_gmt":"2024-03-05T20:22:19","slug":"a-complete-front-end-compiler","status":"publish","type":"post","link":"https:\/\/baldsolutions.com\/index.php\/2024\/03\/05\/a-complete-front-end-compiler\/","title":{"rendered":"A complete Front End compiler"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Introduction<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Some time ago I started to take a deep dive into the topics related to the interpreters, compilers and programming languages in general. To learn more I borrowed a book from a university library (<a href=\"https:\/\/en.wikipedia.org\/wiki\/Compilers:_Principles,_Techniques,_and_Tools\" target=\"_blank\" rel=\"noopener\" title=\"\">Compilers, Principles, Techniques &amp; Tools<\/a>, second edition) and found out an interesting piece of code. The &#8220;Appendix A&#8221; at the end of the book has a complete front end compiler written in Java. As I am more familiar with C# I created  the C# version inspired by the one written in Java and refactored it a little so that it is a bit more readable (I hope I did not crash the implementation logic&#8230; the unit test is green for now :D). <\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Frontend and backend compilers<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">This front end compiler takes a source code and translates to an intermediate representation of the code in the form of three-address intermediate code. The intermediate representation can be further an input to the backend compiler (i haven&#8217;t created my own yet, maybe some day&#8230;). The backend compiler optimizes and generates code for a target platform.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">Example<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">Based on an example from the book the front end compiler can translate a high level source code (language syntax is based on the grammar defined in the book) to the three-address intermediate code:  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>Source code:\n<\/strong>{\n\tint i; int j; float v; float x; float&#91;100] a;\n\twhile(true)\n\t{\n\t\tdo i = i+1; while(a&#91;i] &lt; v);\n\t\tdo j = j-1; while(a&#91;j] > v);\n\t\tif(i>=j) break;\n\t\tx = a&#91;i];\n\t\ta&#91;i] = a&#91;j];\n\t\ta&#91;j] = x;\n\t}\n}\n\n<strong>Output:<\/strong>\nL1:\nL3:\n   i = i + 1\nL5:\n   t1 = i * 8\n   t2 = a &#91;t1]\n   if t2 &lt; v goto L3\nL4:\n   j = j - 1\nL7:\n   t3 = j * 8\n   t4 = a &#91;t3]\n   if t4 > v goto L4\nL6:\n   if false i >= j goto L8\nL9:\n   goto L2\nL8:\n   t5 = i * 8\n   x = a &#91;t5]\nL10:\n   t6 = i * 8\n   t7 = j * 8\n   t8 = a &#91;t7]\n   a &#91;t6] = t8\nL11:\n   t9 = j * 8\n   a &#91;t9] = x\n   goto L1\nL2:<\/code><\/pre>\n\n\n\n<h1 class=\"wp-block-heading\">Summary<\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">In my opinion the compiler related  topics are great as you can learn the internals of the programming languages you use on a daily basis. The topic itself is not that easy and it took me some time to figure out how the aforementioned compiler works &#8211; even though it looks like the the most basic one&#8230; <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you are interested in the code itself or just want to write some simple unit tests for the given language you can do so! You can find the compiler implementation  on my <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/dotnet\/DragonBook\" target=\"_blank\" rel=\"noopener\" title=\"\">GitHub <\/a>and the <a href=\"https:\/\/github.com\/tglowka\/baldsolutions\/tree\/master\/dotnet\/DragonBookTests\" target=\"_blank\" rel=\"noopener\" title=\"\">test <\/a>project.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Have a nice day, bye!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Some time ago I started to take a deep dive into the topics related to the interpreters, compilers and programming languages in general. To&#8230;<\/p>\n<div class=\"more-link-wrapper\"><a class=\"more-link\" href=\"https:\/\/baldsolutions.com\/index.php\/2024\/03\/05\/a-complete-front-end-compiler\/\">Continue reading<span class=\"screen-reader-text\">A complete Front End compiler<\/span><\/a><\/div>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[2,17],"tags":[],"class_list":["post-811","post","type-post","status-publish","format-standard","hentry","category-net","category-compilers","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/811","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/comments?post=811"}],"version-history":[{"count":5,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/811\/revisions"}],"predecessor-version":[{"id":816,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/posts\/811\/revisions\/816"}],"wp:attachment":[{"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/media?parent=811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/categories?post=811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/baldsolutions.com\/index.php\/wp-json\/wp\/v2\/tags?post=811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}