Compilers, Interpreters and Grammars Flashcards

Week 11 Stuff

1
Q

How does a compiler work?

A

Takes a source program and translates the whole thing into machine code.
The translation and execution are separate

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

How does an interpreter work?

A

Takes one instruction at a time from source code and does it.
No binary code is generated.
Source program analysis and execution are interlaced

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

Which executes code faster, compiler or interpreter?

A

Compiler

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

Which is better for keeping source code hidden, compiled or interpreted?

A

Compiled

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

Which is better for rapid prototyping (changes are frequently made), compiled or interpreted?

A

Interpreted

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

Which is better for moving between platforms, compiled or interpreted?

A

Interpreted

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

Why are interpreters sometimes called virtual machines?

A

Because they take 1 instruction at a time and execute it (similar to a CPU)

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

Is Java compiled or interpreted?

A

Both, it’s compiled into machine-independent virtual machine code then interpreted when run.
javac generated virtual bytecode, JVM interprets this.

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

What is translation?

A

The act of turning source code into binary (machine) code which the CPU can understand

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

What kinds of programs do translation?

A

Compilers (translate HLL into machine code)
Assemblers (translate mnemonics like MOV into binary code)

Sometimes compiling requires a compiler and assembler as 2 steps

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

What are the steps to making then running a program?

A

Edit -> Compile -> Link (to external libraries) -> Load/Run

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

What are the 3 components of a coding language?

A

Lexical (the tokens)
Syntactical (the structure)
Semantic (the meaning)

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

What does a lexicon contain?

A

All the legal words (elementary expressions) in a language together with the information about them.
In Java, these are things like “import”, “public”, “else”, etc.

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

What does syntax define?

A

It defines the correct form legal expressions of the language must take.
For example, “double private x” is lexically correct but “private double x” is the syntactically correct version with the tokens in the correct order.

Basically, syntax means tokens are in the correct order

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

What does the semantic component define?

A

It defines what an expression actually means.
if (m < n) { doSomething(); } is correct lexically and syntactically but unless we define how if statments work in the semantics, this has no meaning.
Semantics require are statments to have meaning which makes sense

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

Why does a programming language need a precise description?

A

In order for the translator to analyse it and generate unambiguous code, a description as to how it works is needed

17
Q

What is a grammar?

A

A grammar is a set of syntactic rules which define how statements are constructed in a language.

18
Q

What does a grammar consist of?

A

A grammar consists of rules which are either:
Terminal - they contain no other rules in their definition
Non-terminal - a rule which contains another rule in its definition
Most rules are non-terminal

19
Q

In a syntax diagram (see notes for image), what is the difference between circles and ovals?

A

circles show terminal expressions

ovals show non-terminal expressions

20
Q

What is the Backus-Naur form (sometimes called Backus-Normal form)?

A

The BNF is a way of describing the grammar of a langauge. It’s similar to the syntax diagram but in a more text-based way a computer may find easier to understand.

21
Q

What is the notation for BNF?

A
-> rule definition
| or
 non-terminal symbol
symbol (in italics) terminal symbol
[tokens] optional symbols
{tokens} symbols repeated 0+ times
* symbols repeates 0+ times (suffix)
\+ symbols repeated 1+ times (suffix)

See notes for better structured version of the above

22
Q

What is Parsing?

A

Parsing is looking through a program and comparing it to its grammar to see which parts of the grammar apply to which parts of the program.
If the program doesn’t fit the grammar rules then its incorrect (there’s syntax mistakes)

23
Q

What are the 5 steps of the compilation process?

A
Lexical analysis (scanning)
Syntax analysis (parsing)
Semantic analysis
Code generation
Code optimisation
24
Q

What is scanning (lexical analysis)?

A

Scanning involves converting the text of a program into a sequence of tokens which represent distinct parts of the program.
As tokens are encountered, identifiers get put into a symbol table.

25
Q

What does a symbol table contain?

A

Variable names
Class names
Function names

Basically any name created by the programmer which isn’t a reserved legal word in the lexicon

26
Q

What happens in semantic analysis?

A

Information in the symbol table gets built up about the data items being declared (whether they’re functions, variables, arrays, etc.) and memory locations are assigned where necessary.
Checks are performed for type inconsistencies (in a strongly-typed language)

27
Q

What happens during code generation?

A

Code is generated for each of the constructs identfied during parsing and semantic analysis.
Everything found and put in the parse tree is walked through and low-level code is generated for each aspect of the tree.