|
Stages of compiler design Learning objective: 12.5.1.3 describe program compilation stages: lexical and syntactic analysis, code generation, and optimization When a programmer uses a computer language (high-level) to write a program, the statements are called source code.
The compiler translates source code into a low-level language. The compiled code is saved as an object file. Stages in the compilation of a program:
Lexical analysis Lexical analysis is the process of analyzing a stream of individual characters (normally arranged as lines) into a sequence of lexical tokens (tokenization of words and symbols) to feed into the parser that the compiler will understand.
Source code is written by programmers using ASCII characters. During lexical analysis, the compiler breaks down this stream of ASCII characters into its parts, called "lexemes".
Each lexeme defines a token corresponding to it. TokensKeywordsReserved words that have specific meanings in a programming language.
IdentifiersNames created by the user for variables, functions, classes, etc.
LiteralsConstant values directly written in the source code.
OperatorsSymbols that perform operations on operands.
Delimiters / PunctuationSymbols that define the structure and separation in the code.
CommentsText ignored by the compiler but used for code documentation.
WhitespaceSpaces, tabs, or newlines that separate tokens. Typically ignored by the lexer but can be significant in some languages (e.g., Python). Lexical analysis
Video - Compilation - Part Two: Lexical Analysis Syntactic analysis This is alternatively known as parsing. This stage analyses the syntax of the statements to ensure they conform to the rules of grammar for the computer language in question.
Syntax analyzers receive their inputs, in the form of tokens, from lexical analyzers. Lexical analyzers are responsible for the validity of a token supplied by the syntax analyzer. Drawbacks of syntax analyzers:
Abstract Syntax Tree (AST) – the Program’s Family Tree After lexical and syntactic analysis, the compiler does not just keep the code as raw text. Instead, it builds a special structure called an Abstract Syntax Tree (AST). Think of the AST as a “family tree” of your program: each node represents a construct (like an operation or a variable), and the connections between nodes show how these constructs relate to one another. Unlike the raw source code, the AST ignores details like punctuation or parentheses placement. What matters is the hierarchy of meaning. For example, the expression: X - 2 * Y In plain text, this looks linear. But in the AST, the structure reveals the true order of operations:
This “tree view” makes it easier for the compiler to perform optimizations and generate efficient machine code. In other words, the AST is the bridge between human-readable code and the machine’s execution plan. Code optimization Making the compile time as short as possible. Optimization is a program transformation technique that tries to improve the code by making it consume fewer resources (i.e., CPU, Memory) and deliver high speed. The system programmer is responsible for optimization. Optimizations provided by a compiler include:
Code generation The code generated by the compiler is an object file. In compiler design, object code refers to the code that the compiler generates after analyzing the program's structure and meaning and optimizing it. This code is a machine-readable version of the source code, allowing the computer’s CPU to run it directly. When the file runs, the machine code is processed by the CPU. Minimum properties of low-level object code:
Using resources: Syntax Analysis Stages of compilation (Isaac CS) Questions: Give two examples of high-level languages. (Marks: 1)
A compiler is used to run them. What does it do? (Marks: 1)high-level
What is the advantage of writing a program using Pascal or Python compared to writing the same program in assembly code? (Marks: 1)
Exercises: Ex. 1 Drag and Drop Ex. 2 Fill the table Ex. 3 "Order the stages of compilation" Ex. 4 "Fill the gaps" (Author: Litvinova Olga - CS teacher of NIS Pavlodar) Ex. 5
Exam questions: Why would a company not want to distribute source code when they sell a software package? (Marks: 2)
| |
|
| |
| Просмотров: 15381 | | |
| Всего комментариев: 0 | |



