Preparing Our Lexer
No lecture description Lesson locked purchase
Description
Learn to create your very own C compiler from scratch. In this course we develop a compiler that compiles a subset of the C Programming Language. By the time you finish all modules of this course you will be able to compile C programs that use pointers, structures, unions, arrays, functions, for loops, while loops. do while loops, if statements, switches and much more! This course includes all course modules!
Our compiler also has a preprocessor macro system allowing you to include header files and create definitions just like you would in any C file.
Your compiler is advanced enough to use the GCC standard library so we are able to call C functions from our compiler. Your compiler will be able to compile the C programming language.
This course does not rely on any frameworks we do everything from scratch to ensure the best possible learning experience for students
Module 1
In module 1 of this course we load our C source file that we wish to compile, into memory. We create a lexer to preform lexical analysis on the source input which will convert the source code into a bunch of tokens that our compiler can easily understand. We then pass the tokens through a parser to produce an abstract syntax tree. An AST describes the C program in a logical way that makes it easier for our compiler to understand. For example for the expression 50 + 20 you will end up with a root expression node that has a left operand that has a node of value 50 and a right operand that has a node of value 20. Breaking down problems in this way makes it much easier to create compilers.
Module 2
In module 2 of this course we create a code generator that produces 32 bit Intel assembly language that can then be passed through an assembler to produce a program binary that we can run. We also in this module create a resolver system which is responsible for taking a complicated expression such as "a->b.c.e[50] = 50" and breaking it down into simple steps and rules that our code generator can then easily follow. This abstraction is essential to ensure that the code generator does not become over complex. With the use of a resolver system we can ensure the code base remains clean.
Module 3
In module 3 of this course we create a preprocessor and macro system. This preprocessor system allows us to include header files in our C programs and also use a variety of macro keywords such as "#define" "#ifdef" , "sizeof" and many more.
Module 4
In module 4 we build a semantic validator which validates our C code. A semantic validator ensures that we are not setting variables that do not exist or accessing structures that arent there.
This is the only video course in the world that shows you how to create a C compiler, come and learn today!
Requirements
-
You must have a basic experience of assembly language.
Who This Course is For
People with an interest in compiler design
People who are interested in assembly language
People who are interested in the C Programming language
What You Will be Learn
How to build a C compiler from scratch
Full understanding of stackframes and how assembly language is generared for a C source file
Complete Understanding of lexical analysis and parsing
Stronger Assembly language skills will be gained
Compiler Design
Dragon Zap Instructor
Daniel McCarthy is a seasoned software engineer, boasting an impressive career spanning over 14 years in the industry. Holding a Master's Degree in Advanced Computer Science from Cardiff Metropolitan University, his broad spectrum of experience encompasses everything from web development to complex compiler and interpreter development. Daniel has honed his skills in bootloader and kernel development. In testament to his proficiency in the field, he has designed two proprietary programming languages: Craft, a general-purpose language, and Marble, a web-focused language akin to PHP. Moreover, he has successfully developed compilers for the C programming language. A testament to his versatility, Daniel demonstrates proficiency in an extensive list of programming languages that includes C, C++, Java, x86 Assembly language, PIC assembly, SQL, PHP, HTML5, JavaScript, CSS, and of course, his own creations, Craft and Marble. His professional portfolio also includes the development of Linux kernel modules, a task he has executed with proficiency in a professional context. Currently, Daniel is channeling his wealth of experience and expertise into the education sector, with the aim of nurturing the next generation of professional software engineers.
Ask a question
Questions (2)
Lane Beals
1 year ago
It's pretty difficult to follow along when the course isn't moving "laterally". What I mean by that, are things being explained in real time rather than making structs at the moment and then being explained later. Do you have any recommendations for how to go about keeping up with this in mind?
Daniel McCarthy
1 year ago
Hey lane, Compiler development isnt a walk in the park its a very difficult topic so I am not surprised you are facing difficulties. I understand your frustration but you should just stick with it, this is a best selling course and its very long as we cover a lot of detail. You know as it stands this course is 38 hours long so a lot of detail is covered. If your not getting the detail are you skipping the theory lectures. As generally I have an entire presentation made and then we do the practical later. If your skipping some lectures then this could be why your having trouble now. Some students will skip a lecture or two now and again but I highly recommend people don't do that because a lot of content is covered 38 hours worth. If you can explain a particular senario I would be able to assist further but from your question it seems more like some feedback that your struggling with the course. My recommendation is to ensure your watching all the content. If you are doing that then my recommendation is to ask any questions you have here so I can further clairfy.
Stephen Smith
10 months ago
Compiler error and warning functions in compiler.c. In case of a compiler error, these should display messages containing the error message and the line, column and file where it was detected. These are values in compiler->pos.line, compiler->pos.col and compiler->pos.file. While I can find the code where line and col are set, there doesn't seem to be any statement in the whole (completed) program where compiler->pos.file is set. This is confirmed by all specifically provoked compiler errors returning only a NULL string for the filename.. pos.file is set in the parser but does not make it into the compiler struct. Can you help here? Regards Steve Smith
Daniel McCarthy
10 months ago
Hi Stephen, When you think something is missing its best to first check the master repository as things do get added later throughout the course. Here is where it is set: https://github.com/nibblebits/PeachCompiler/blob/9348c6ee8fab335915468ddea583137b7019df9c/cprocess.c#L86 Thanks Dan
Stephen Smith
10 months ago
Hi! I checked the REPO you indicated. The indicated line no. 86 in cprocess.c shows compiler->pos.col being set. Later, line nos. 90 and 91 show compiler->pos.col and compiler->pos.line being set/manipulated. This is OK, but there's no reference to compiler->pos.file being set. My problem is only getting a NULL string pointer in the (provoked) error messages, although line and col values are OK. This seems to indicate a missing operation. Regards Steve.
Daniel McCarthy
10 months ago
Hey Stephen, Yes I see what you mean its odd, can you please file an issue report on the Github repository so I can take a look further into this to ensure if its an actual bug or something I cant account for this moment in time, we can continue the thread on there when I have more information, I will take a good look this week for you Thanks Dan
Stephen Smith
10 months ago
Hallo Dan! I must apologize for taking up your time. I found the error and it is absolutely my fault only! I am running the compiler under Windows, using C++Builder to compile it and NASM to process the assembler files it generates. Builder is a GUI I'm very familiar with as opposed to VS Code. Under windows,certain changes are needed in the code. There is no "realpath" function, I substituted the equicalent windows function "_fullpath" but I was calling it incorrectly, reversing the order of "filename" and "path" both char* values. So while id didn't complain, it didn't work. This was why I was missing "filename" output. As soon as I corrected this, everything worked! Thanks for looking! By the way, I'm now also running Ubuntu in WSL to compile the original files with complete success. Regards Steve
Daniel McCarthy
10 months ago
Ahh perfect and no apology needed when in doubt ask away im here to help you with any issues you face on the course. All the best Dan