12.5.1.3 describe program compilation stages: lexical and syntactic analysis, code generation, and optimization Stages of compiler design 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 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:
Video - Compilation - Part Three: Syntax Analysis
Code optimization Making the compile time as short as possible. Optimization is a program transformation technique, which 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 is the code that the compiler creates after checking the program's structure and meaning, and optimizing it. This code is a machine-readable version of the original 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)
| |
| |
Просмотров: 10806 | | |
Всего комментариев: 0 | |