Code Generation Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

Quick Summary -

A

LEXICAL ANALYSIS -
Comments / White space removed
Remaining code turned into a series of tokens
Symbol table is created to keep track of variables and subroutines
SYNTAX ANALYSIS -
Abstract syntax tree built from tokens produced in the previous stages
Errors generated if any tokens break the rules of the language
CODE GENERATION -
Abstract tree code -> Object Code
Object code is the machine code produced before the final step is run
CODE OPTIMISATION -
Tweaks the code so it will run quickly and use as little memory as possible

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

1ST PHASE LEXICAL ANALYSIS -

A
  • Lexer starts by converting lexemes in the source code into a series of tokens.
  • Lexer reads the code letter by letter
  • When encountering a white space, operator symbol, special symbol, it decides that a word (lexeme) is complete
  • It checks if lexeme is valid using predefined set of rules allow every lexeme to be identified as a valid token
  • Keywords, Constants, Numbers, Operators and Punctuations symbols are all tokens e.g. [tokenclass:token]
  • if stopping the compilation process it would look like a screenshot, with all whitespaces and comments removed
  • Then put token streams into a symbol table
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

2ND PHASE SYNTAX ANALYSIS -

A
  • Receives tokens from lexical analysis, analyses syntactical structure of the input, checking if it is in the correct syntax, does this by analysing the token stream against production rules to detect errors -> checks for errors / reporting them and builds a abstract syntax tree
  • Lexer couldn’t tell within the syntax of the language whereas syntax analyser can, to see if it strictly follow the language, if it fails it can report the error letting them know the line and location even suggesting a possible correction.
  • Main output -> abstract syntax tree created from input token stream
  • When identifier is added, the symbol table checks to see if it exists, this info can be used to update the data type of identifiers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

3RD AND 4TH PHASE CODE GENERATION / OPTIMISATION -

A
  • In these final phases, machine code is generated
  • Code Optimisation attempts to reduce execution time of program by spotting redundant instructions / producing object code, removing subroutines never called, removing variables and constants that are never referenced
    It can considerably increase compilation time for a program.
  • At source code level, the user can write anything
How well did you know this?
1
Not at all
2
3
4
5
Perfectly