Program construction Flashcards
What are regular expressions?
- Lexical analyser looks through code for specific patterns of letters, numbers or symbols. These are known as regular expressions
- Once recognised they are applied to source code + a stream of tokens is generated
- Can be represented as diagrams
What is lexical analysis?
- Views source code and replaces keywords and symbols with tokens
- spaces and comments are removed and tokens have replaced keywords, variables etc.
- Invalid tokens pull up error
- Once completed stream of tokens sent to syntax analysis
What are abstract syntax trees?
- As code is parsed, a tree structure is formed
- Abstract syntax trees (AST) are syntactically correct and only readable in one way
- Operators are at root node
- Numbers are operands
What its semantic analysis?
Tries to imply meaning of code
Checks against aspects that are not related to syntactic form or that are not easily determined during parsing
Typical semantic errors:
-Type mismatch
-Undeclared variable
-Reserved identifier misuse
-Multiple declaration of variable in scope
-Accessing an out of scope variable
If error is found then report generator displays an error message.
What is code mapping?
Matching common patterns from AST to block of machine code
Repeated until all parts of AST are mapped
Allocate blocks of code to registers
What is code optimisation?
Code improvement e.g constant folding(e.g replace x= 4*4 with x= 16)
Remove redundant code
Tries to improve code by making it consume less resources and deliver high speed
What are the stages of compilation?
Lexical analysis
Syntax analysis
Semantic analysis
Code generation and optimisation
What are translation and execution errors? #Needs more (look at P.C test)
Translation:
Occurs when program is being compiled
e.g Syntax, linking and semantic errors
Execution(runtime):
Occurs when program is running
e.g. division by 0, reading past end of file, stack overflow ect.
What is machine code generation and optimisation?
- Final phase of compilation
- Once AST is created + checked against rules of language, code generation can then happen
- When program can be lexically and syntactically analysed without errors, machine code can be generated
- Tweaks code so it can run as quickly and with as little memory as possible.
What is the assembler?
- Assembler is a program that translates assembly code into machine language
- Relatively simple process as in general one assembly code statement translates into one machine code statement
- It can allocate memory to variables
Compare compiler and interpreter
Compiler
- Source code hidden from other developers- intellectual property protected
- Compiled code will only run on CPUs with same instruction sets and matching OS
- Executable files can easily be distributed on CD or over internet
Interpreter:
- Source code shown to all developers and users- intellectual property risk
- will run on any platform from same source code base interpreter is required to run
- Systems need to be set up before code can run. Can include installing library files as well as interpreter
Disadvantages of interpreter?
- Must have interpreter running on computer to be able to run the program
- As source code must be compiled each time, programs can sometimes run slowly
- Have to rely on interpreter for machine level optimisation rather than programming them yourself
- Developers can view source code which means they could use your intellectual property
Advantages of interpreter
- Source code has to be written only once, then can run on any computer with a compiler
- Can easily inspect contents pf variables as running
- Can test code more efficiently as well as being able to test single lines of code
- code can be run on many different types of computers and OS
What is interpreter?
- Converts high level language program to machine code line by line
- Error found will be reported immediately to user + halts further execution
- Any machine coed produced while interpreter is running will not be saved meaning it will have to be interpreted each time it wants to run code
What are compilers?
- Convert high level language source code into machine code
- Produces standalone executable file which can be released to others without the need for further compilation
- Protects intellectual property of developers
- Code is converted all at once
- If compiler encounters syntax error, it cannot translate statement so no object code is produced
- When errors are found they are placed in error file and error message displayed to user.