Compiler frontend
The compiler frontend is the part of a compiler which interprets and translates a programming language into another internal language (called intermediate representation) which the compiler can understand. The frontend processes the source code through several different phases to parse and remove syntax, semantics and lexical errors. After the compiler frontend processes a source code, symbol table are created to store their data and context.
Overview
The frontend of a compiler takes vital role in removing whitespaces and converting various language features and macros into an intermediate representation that resemble a lower level language. There are several phases which the source code is analyzed through.
The source code of a computer program is read by a lexical analyzer which splits the text into words and symbols known as "tokens". The tokens are analyzed by a parser which looks for grammatical patterns in the use of the tokens. The parser collects the language data for an intermediate-code generator to convert the data into that form of coding. An optimizer reads the intermediate code, which was generated from the parser data, and simplifies or omits extra code. At this point the compiler backend takes the intermediate representation and writes a more-efficient language text, which is then changed into the target computer's machine code.
Frontend Processing
Lexical Analysis
Lexical analyzer, also called as scanner, is the first phase of a compiler which gets source program as input. It scans the source program from left to right and produces tokens as output. A token can be seen as a sequence of characters having a collective meaning.
Syntax Analysis
Syntax analysis or parsing analyzes the stream of tokens to determine whether or not a text conforms to an expected format or grammar. Syntax analysis mostly concerns with determining that the source code of a program is correct and to convert it into a more structured representation (parse tree or abstract syntax tree) for further processing such as semantic analysis.
Semantics Analysis
Semantics analysis checks for context of a grammatically correct sentence. A syntatically correct sentence may not be semantically correct. Therefore, a complete understanding of the language standards is required at this stage of source code processing.
% //Grammatically correct but contextually incorrect
% int x = 5.2;
% //Grammatically correct and contextually correct
% float y = 20.45;
Frontend Output
Every compiler frontend stores different amount of data structure details in the intermediate language. This implies that each compiler has different internal representation for the intermediate representation. Therefore, one intermediate language from a compiler will likely be incompatible with another compiler.
See Also
- Compiler
- Compiler-compiler (or Parser generator)
- Compiler correctness
- History of compiler writing
- Lexical Analysis
- Parsing
- List of compilers
- Overhead code
- Semantics encoding
References
- http://www.semdesigns.com/Products/FrontEnds/ Semantic Designs Compiler frontend overview, General compiler knowledge
- http://gcc.gnu.org/onlinedocs/ Open source 'gcc' compiler manual
- http://clang.llvm.org/ Open source 'clang' compiler manual
- https://www-01.ibm.com/chips/techlib/techlib.nsf/techdocs/852569B20050FF7785256996007558C6/$file/cwg.pdf Closed platform IBM compiler manual
External Links
- Basics of Compiler Design by Torben Ægidius Mogensen