1.Lexical Analysis:
oThe source code is first tested by the compiler’s lexer, which breaks it down into tokens (such as keywords, identifiers, operators, and punctuation).
oThis step identifies the individual elements of the program.
2.Syntactic and Semantic Analysis:
oThe compiler performs syntactic analysis, checking the source code for syntax errors and ensuring it follows the correct language-specific rules.
oIf any errors occur, the compiler throws an error and stops the compilation.
oAdditionally, the compiler performs semantic analysis to find the meaning of the code.
oIt checks for logical errors, such as type mismatches, undeclared variables, and incorrect usage of operators.
3.Optimization:
oOnce the code is syntactically correct, the compiler performs low-level optimization to improve performance.
oThis may involve reducing memory usage or optimizing code for speed by rearranging instructions or eliminating unnecessary operations.
4.Output Code Generation:
oFinally, the compiler generates machine code corresponding to the original source code.
oThe resulting machine code resides in a binary file that the computer’s hardware can execute directly.
Why Do We Use Compilers?
•Compilers catch syntax and semantic errors before running the code, saving time and preventing crashes.
•They optimize code for efficient execution, resulting in faster, more compact programs.
Disadvantages of a Compiler:
•Compiled code is platform-dependent, meaning it produces a machine-specific executable file.
•Code compiled on one type of machine won’t run on another type without recompilation1.
References
1.GeeksforGeeks
2.Built In
3.Wikipedia
Submit