How I went about designing a compiler for my game engine scri pting language (BisonC)
Date : 9/2/2016 1
The job of the compiler is to turn high level code into low level assembly language. This document discusses how this is done.Translating simple instructions with a fixed set of opcodes such as mov var,3is relatively simple compared to translating high level expressions such as x= cos(y) * sin (x) The heart of the compiler and the main difference between compiling assembly and C rolves around parsing expression. There are two primary methods for high level language parsers, top-down and bottom-up Top-Down Because of the recursive nature of parsing this method is also known as recursive decent parsing. Recursive descent parsing is best described with an example, consider the following. for (int i=0i
Bottom-Up parsingBottom up parsers do not branch off to a specific parsing mechanism for a given token, rather it uses inductive reasoning to piece together a more refined picture of the source code. Although more complex bottom up method is certainly a more flexible and efficient approach to parsing because it uses a single loop to parse the entire source file rather than branching off to specific method for specific circumstances. Rather parsing is handled through large procedurally generated lookup tables that helps detect patterns in the token stream. Bottom-Up parsing is analogous to a state-machine methodology whilst top down is geared more towards a brute force approach.
Designing a Parsing strategyI decided to use top down parsing method for the BisonC compiler. The main reason was that it was relatively simpler than that of its counterpart- bottom up parsing. This would allow me to complete and test the approach in the given time frame. The second main design strategy was whether to use single or multiple pass compilerA pass is defined as a complete scan over the source code regardless of what information is collected . Single pass compilers are faster (because they only make one pass).Multi pass compilers are easier to work with because you can gather information about variables and functions before processing the instructions that use them I decided to go with multi pass approach because it allowed future extensibility to be implemented easier. If I decided to add such features as macros or dll support in the future having all function and variable information before hand will makes things a lot simpler to implement. Another driving factor was that most modern compilers are built around the two pass method, it seemed to be the given consensus in the compiler writing community.
This resource was uploaded by: Arysha