130 likes | 272 Vues
This article explores the fundamental aspects of programming language syntax implementation through compilation, interpretation, and hybrid methods. It covers the critical components such as lexical analyzers, syntax analyzers, and symbol tables, detailing their roles in transforming source code into machine language. Additionally, it discusses semantic analysis, intermediate code generation, and optimization techniques that enhance program efficiency. The piece serves as a comprehensive guide for beginner programmers and those interested in compiler design, illustrating the journey from high-level code to executable machine language.
E N D
Robert Meyer Charles Scott Compiling & Syntax
Syntax The syntax of a programming language can be implemented by any of 3 general methods: Compilation Interpretation Hybrid Implementation Source Compiler Object Code
Source Lexical Analyzer SymbolTable Lexical Units Syntax Analyzer Parse Tree’s (optional) Intermediate CodeGeneration & SemanticAnalysis Optimizer Input Code Generation Computer Results Machine Language
Source Lexical Analyzer SymbolTable Lexical Units • Lexical Analyzer: • Gathers Characters of source program into lexical Units • Lexical units: • Identification • Special Words • Operators • Punctuation Symbols • Comments are Ignored!
Source Lexical Analyzer SymbolTable Lexical Units Syntax Analyzer Parse Tree’s • 2) Syntax Analyzer • Takes Lexical Units and Constructs Hierarchical structure called “Parse Tree” which represents syntactic structure of the program.
1 farenheight 2 celcius |F|a|r|e|n|h|i|e|g|h|t| |:|=| |3|2| |+| |c|e|l|c|i|u|s| |*| |1|.|8|;| Broken down by Character Lexical Analyzer id1 := Int32 + id2 * Real ; Syntax Analyzer Symbol Table := id1 + * 32int id2 Real idn Denotes symbol n in the symbol table Determines the type of the identifier Context Analyzer :=r id1 +r *r 32int id2 1.8real
Source Note: Semantic analysis is an integral part of the intermediate code generation process. It checks for errors that are difficult to detect during syntax analysis such as type errors. Lexical Analyzer SymbolTable Lexical Units Syntax Analyzer Parse Tree’s Intermediate CodeGeneration & SemanticAnalysis • 3) Intermediate Code Generation • Produces Program in a different language. • Assembly • Something like assembly • Something higher level than assembly
Source 3.1) Optimization Makes program smaller and/or faster. Most optimization is done on Intermediate code. Lexical Analyzer SymbolTable Lexical Units Program Syntax Analyzer Lab for beginner programmers: No Optimization Compiler Object Code Parse Tree’s (optional) Intermediate CodeGeneration & SemanticAnalysis Optimizer
Intermediate Code Generation (optional) Temp1 = int_to_real(32) Temp2 = ID2 Temp2 = Temp2 * 1.8 Temp1 = Temp1 + Temp2 ID1 = Temp1 Optimizer Integer constant 32 converted to float constant Temp1 = ID2 Temp1 = Temp1 * 1.8 Temp1 = Temp1 + 32 ID1= Temp1 Reduced instruction set by 1 instruction, and reduced memory usage by 1 temp variable movf ID2, R1 mulf 1.8, R1 addf 32, R1 movf R1, ID1 *Code Depends on machine
4) Code Generation Translates Intermediate code to machine language. The symbol table serves as a database for the compilation process. Source Lexical Analyzer SymbolTable Lexical Analyzer Syntax Analyzer Lexical Units Syntax Analyzer Symbol Table Intermediate Code Parse Tree’s (optional) Intermediate CodeGeneration & SemanticAnalysis Optimizer Linker translates to executable code Input Code Generation Computer Results Machine Language
Machine Language: • To run the program in machine language form it needs: • Some other code • Programs from the Operating system (Input / Output) Machine Language OS Routines Libraries System calls executed on programs behalf Linker Executable Image Loader Fetch Execute Interrupt Program Loop
Interpret: • Slower than compiler. • One instruction at a time. • Virtual Machine introduced by IBM to test code on different platforms.