200 likes | 321 Views
COMP313A Programming Languages. Overview of the Compilation Process. skeletal source program. preprocessor. source program. compiler. target assembly program. assembler. relocatable machine code. loader/link-editor. absolute machine code. source program. lexical analyser.
E N D
COMP313A Programming Languages Overview of the Compilation Process
skeletal source program preprocessor source program compiler target assembly program assembler relocatable machine code loader/link-editor absolute machine code
source program lexicalanalyser syntaxanalyser symbol table manager semanticanalyser error handler code optimiser code generator target program
Type 0 Unrestricted Grammar Type 1 Context-Sensitive Grammar Type 2 Context Free Grammar Type 3 Regular Grammar Chomsky’s Grammar Hierarchy
Grammars • Type 3 (regular) • A wB, A w (right linear) • w is a string of Terminals • A and B are Non-Terminals • Finite state automata • Type 2 (context free) • A • A is a Non-terminal • is a member of T N (can be empty) • Equivalent to a push-down automaton
Grammars • Type 1 (context sensitive) • A B • A becomes B in the context of … • Complex for computer analysis • Type 0 (unrestricted) • , • and are unrestricted sequences, is not null • languages formed from Type 0 grammars can be recognised by non-deterministic Turing machines
Lexical Analysis object_position = calculate_position( robot_coordinates, robot_heading, range, error) Need to find the words i.e. tokens
Lexical Analysis • Uses a regular grammar for specifying the rules for forming “words” / tokens
Syntax Analysis • Hierarchical Analysis • Groups tokens into grammatical phrases • Parse Tree • Use syntax rules (context free grammars/BNF) to perform the analysis
Expression ::= id Expression ::= num Expression ::= Expression + Expression Expression ::= Expression * Expression
assignment statement = expression identifier position expression + expression identifier * expression expression initial identifier identifier rate 60
= + position initial * rate 60
Semantic Analysis • Checks source program for semantic errors • Type checking • Gathers type information for code generation
= + position initial * rate inttoreal 60
Symbol Table Management • Records identifiers used in source program • Collects information about various attributes of each identifier • Storage allocation • Type • Scope • Procedure names – ?????
Error Detection and Reporting • Lexical errors • Syntax errors • Semantic errors
Code Optimisation • Improve code so it runs faster • Where does program spend most of its time?
Code generation • Generation of target code • Relocatable machine code or assembler code • Memory locations are selected for each of the variables • Assignment of variables to registers
MOVF id3, R2 MULF #60.0, R2 MOVF id2, R1 ADDF R2, R1 MOVF R1, id1
Analysis versus Synthesis • Back end versus front end of the compiler