LP Flashcards

1
Q

What is a language?

A

A language is a set of symbols/tokens and a set of (possibly empty) strings and tokens.

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

What is the purpose of formal grammar?

A

The purpose of formal grammar is to generate languages. Using formal grammar, we can verify whether a sentence is in the language and synthesize legal problems.

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

What is V in formal grammar?

A

In formal grammar, V stands for vocabulary, which is a set of symbols.

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

How is V partitioned in formal grammar?

A

V is partitioned into two subsets of terminal and nonterminal symbols in formal grammar.

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

What is V* in formal grammar?

A

In formal grammar, V* is the set of all strings of elements of V, including the empty string. This is called the closure of V.

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

What is the set + in formal grammar?

A

In formal grammar, + is the set of non-empty strings of elements of V.

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

What does ε represent in formal grammar?

A

In formal grammar, ε represents the empty string, often informally written as ‘ ’ or “ “.

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

Can you give an example of a vocabulary V in formal grammar?

A

Sure. A vocabulary V could be {0, 1, +, -, *, (, ), <exp>}, where <exp> is a non-terminal symbol, and all other symbols are terminal.</exp></exp>

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

Can you give an example of sentences in a vocabulary V in formal grammar?

A

Sure. Example sentences in a vocabulary V {0, 1, +, -, *, (, ), <exp>} could be ε, 1 + 1, (1 + 1) and (1 + (1)), ((((, and (()) * --.</exp>

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

What do alpha, beta, and gamma range over in formal grammar?

A

In formal grammar, alpha, beta, and gamma range over strings.

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

What is a production rule in formal grammar?

A

A production rule is a pair alpha ::= beta, where alpha is a nonterminal symbol, and beta is a string of terminals and/or nonterminals.

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

What is an example vocabulary V in formal grammar?

A

An example vocabulary V in formal grammar could be {0, 1, +, -, *, (, ), <exp>}.</exp>

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

What are some example production rules for the given vocabulary V?

A

Some example production rules for the given vocabulary V {0, 1, +, -, *, (, ), <exp>} are:</exp>

<exp> ::= 0
<exp> ::= 1
<exp> ::= -<exp>
<exp> ::= (<exp>)
<exp> ::= <exp>+<exp>
<exp> ::= <exp>*<exp>
</exp></exp></exp></exp></exp></exp></exp></exp></exp></exp></exp></exp>

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

Definition of regular grammar:

A

Regular grammar is formal grammar that generates only regular languages, which can be recognised by a DFA or NFA.
Rules in form A -> aB or A -> a where A and N are non-terminals and a is terminal.
Regular grammars are of type 3.

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

Definition of context-free grammar:

A

Formal grammar in which each production rule has a single non-terminal symbol on its left-hand side and on the right-hand side 0 or more terminals or non-terminals.
Used to describe the syntax of programming and natural languages.
A language is context-free if there exists a cfg that generates it.
CFG grammars are of type 2.
S -> NP VP
NP -> Det N
VP -> V NP
Det -> the
N -> cat | mouse
V -> chased

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

Left-regular grammar:

A

When Terminals are on the left:

  1. A -> a
  2. A -> Ba
  3. A -> e
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Right-regular grammar:

A

When non-terminals are on the right:

  1. A -> a
  2. A -> aB
  3. A -> e
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Write grammar that generates the following language, over the alphabet {0, 1}:
{0}.

A

S -> 0

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

Write grammar that generates the following language, over the alphabet {0, 1}:
{1, 10, 100, 1000, . . .}

A

S -> 1T | 1
T -> OT | e

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

Write grammar that generates the following language, over the alphabet {0, 1}:
The set of strings over 0 and 1 that contain at least three consecutive repeated characters. So 100110 and ϵ are not in the language,
and 000 and 111 and 01000110 are in the language.

A

S → 0SS | 1SS | A
A → 000A | 111A | B
B → 000B | 111B | ε

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

Write grammar that generates the following language, over the alphabet {0, 1}:
The set of strings over 0 and 1 that contain no three consecutive
repeated characters. So 100110 and ϵ are in the language, and
000 and 111 and 01000110 are not.

A

S → 0A | 1A | ε
A → 01B | 10B | 0S | 1S
B → 01C | 10C | ε
C → 0B | 1B | ε

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

write a regular grammar to
recognise ( {a3^n| n ≥ 0}

A

S → aB | ε
B → aC
C → aS

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

write a regular grammar to
recognise {(abba)∗}

A

S -> aB | ε
B -> bC
C -> bD
D -> aS

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

Write a grammar for the language of properly bracketed high school
arithmetic over single binary digits. So:
* the set of terminal symbols is {0, 1, +, ×,(,)}, and
* 0 and 1 and (0) and 1+1 and 1+0×1 and 1+ (0×1) and (1+1)×1
are in the language, and
* 01 and 1 + (1 and () are not in the language.

A

<exp> ::= <term> | <term> + <exp> | <term> - <exp>
<term> ::= <factor> | <factor> × <term>
<factor> ::= <digit> | (<exp>)
<digit> ::= 0 | 1
</digit></exp></digit></factor></term></factor></factor></term></exp></term></exp></term></term></exp>

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

Define DFA:

A

A DFA describes languages. You input a word and the DFA accepts/rejects it.
A DFA A = (Q, Σ, δ, q0, F) is specified by:

  1. Finite state set Q
  2. Input alphabet Σ. The machine operates over words over Σ.
  3. Transition function
    δ : Q × Σ −→ Q
    If the machine is in state q and the present input letter is a then the machine changes its internal state to δ(q, a) and moves on to the next input letter.
  4. Initial state q0.
  5. Set F ⊆ Q of final states specifies which states are accepting. At the end of the input it does not end at a state F, the word is not accepted.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Define NFA:

A

NFA generalise DFA. They allow several (0, 1, more than 1) outgoing transitions with the same label.
NFA accepts a word if some choices of transitions take the machine to a finite state.
NFA may have different computations for same word

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

Describe Lexical Analysis

A

The process of breaking down a stream of input characters into meaningful chunks, called tokens. The main goal of lexical analysis is to identify the basic building blocks of a language, such as keywords, identifiers, constants, and operators, These tokens serve as input to the next stage, which is syntactic analysis.

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

Define NFA:

A

NFA generalise DFA. They allow several (0, 1, more than 1) outgoing transitions with the same label.
NFA accepts a word if some choices of transitions take the machine to a finite state.
NFA may have different computations for the same word

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

Describe Syntactic Analysis

A

The process of analysing the syntax of a sentence to determine its structure and meaning. It involves checking that the order and combination of tokens conform to the grammar rules of the language.

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

Describe instruction selection

A

Instruction selection is the process of selecting appropriate instructions from a set of available instructions to implement a given program.
In Ocaml, this involves translating the high-level Ocaml code into a lower-level assembly language that can be executed by the computer.
The choice of instructions can have a significant impact on the performance of the program

31
Q

Local Optimization

A

The process of optimizing code at the level of individual expressions or statements, without considering the overall structure of the program.
In Ocaml, this can involve simplifying expressions, reordering computations, or eliminating redundant computations to improve performance.

32
Q

Loop Optimization

A

The process of optimizing loops in a program to improve performance. In Ocaml, this can involve techniques such as loop unrolling and loop fusion.

33
Q

Variable Scope

A

Refers to the area of the program where a variable can be accessed. In Ocaml, the scope of the variable is determined by its declaration and the block structure of the program.

34
Q

DFA A

A

Describes its language L(A)

35
Q

Not every language can be defined by a DFA

A

no DFA accepts{a^p| p∈N is prime}.

36
Q

Transition Diagrams

A

To determine whether w is accepted by A, follow the path in A
labelled with the input letters of w, starting from the initial state.
If this leads you to a final state, the word is accepted.
w = abaab leads to state r, which is a final state, so the word is
accepted.

w′ = abba is rejected because it leads to q, which is not a final
state.

37
Q

Languages that can be recognised by DFA

A

are called regular (cf. regexps!).

38
Q

Extended transition function

A

Extend this to δˆ, giving the state after an arbitrary string of letters
is read:
ˆδ : Q × Σ∗ −→ Q.
For state q and word w,ˆδ(q,w) is the state we reach from q if we
input w.

39
Q

The precise definition of NFA A = (Q, Σ, δ, q0, F):

A

An NFA A = (Q, Σ, δ, q0, F) is specified by:

  • State set Q
  • input alphabet Σ
  • transition function δ
  • initial state q0
  • the final state set F

Using the power set notation:
2^Q = {S | S ⊆ Q} we can write δ : Q × Σ −→ 2^Q.

40
Q

Difference between Lexical and Syntactic Analysis

A

The main difference between lexical analysis and syntactic analysis is that lexical analysis deals with the analysis of individual words or tokens,

whereas syntactic analysis deals with the analysis of the structure and organization of these tokens to form meaningful sentences or statements in the programming language.

41
Q

Difference between Concrete Syntax and Abstract Syntax.

A

The main difference between concrete syntax and abstract syntax is that concrete syntax is the actual textual representation of a program in a programming language, whereas abstract syntax is a simplified, language-independent representation of the essential structure of a program.

42
Q

Define Type 0 grammar.

A

Type 0 grammars contain productions of the form
α ::= β.
α is a non-empty string of terminal and/or nonterminal symbols.
Type 0 grammars include pretty much anything.

43
Q

Define Type 1 grammar.

A

(Context-sensitive):
grammars contain productions of the form
αAγ ::= αβγ.
Here A denotes a single nonterminal and β denotes an arbitrary string of terminal and/or nonterminal symbols. You can ‘expand’ A — subject to it occurring in the context described by α and γ.
Generates languages that are context-sensitive.

44
Q

Define Type 2 grammar.

A

(Context-free):
grammars contain productions of the form
A ::= γ.
A denotes a single nonterminal. BNF is a language for describing
Type 2 languages.

45
Q

Define Type 3 grammar.

A

Type 3 or regular grammars contain productions of the form
A ::= aB
A ::= b
A ::= ε

46
Q

Example Regular grammar

A

Type 3 grammar:

S -> aS| b

47
Q

Example (Context-free)

A

Type 2 grammar:

S -> aSb | ε

48
Q

Example Context Sensitive

A

Type 1 grammar:

S -> aA
A -> aA | bB
B -> bB | ε

49
Q

Ambiguity description and why it is undesirable in programming languages

A

When a sentence can be parsed in two different ways, each with a different meaning. In a computer programming language, ambiguity is generally undesirable because it can lead to unexpected behaviour and make programs harder to understand and maintain. Programming languages strive for unambiguous syntax to ensure that programs are executed as intended and to facilitate program comprehension and debugging.

50
Q
  1. Describe precisely the language corresponding to the regular expression
    a∗.
A

Corresponds to the language that has 0 or more occurrences of
a i.ie, L {ε, a, aa, aaa,….} .
Where ε is the empty string.

Production rule could be:

S -> aS | ε

51
Q
  1. Describe precisely the language corresponding to the regular expression (a).
A

Corresponds to the language that only matches 1 a.
i.e., L = {a}

52
Q
  1. Describe precisely the language corresponding to the regular expression (a|b).
A

Corresponds to the language that matches either 1 a or 1 b i.e.,
L = {a, b}

53
Q

Describe abstract syntax

A

Abstract syntax is a way of representing the essential elements of a program or a language using a simplified and standardized notation.

54
Q

describe instruction selection

A

Instruction selection is the process of selecting appropriate machine instructions to implement a given intermediate code or high-level language construct. The goal is to generate efficient code that correctly captures the behaviour of the source code.

Here is an example of instruction selection for a simple expression:
a = b + c * d
Assuming a target architecture with the following instructions:

MOV reg, mem: Move data from memory to a register
ADD reg1, reg2: Add the contents of reg2 to reg1
MUL reg1, reg2: Multiply the contents of reg1 by reg2

55
Q

describe local optimisation

A

Local optimization is a compiler optimization technique that focuses on improving the performance of small sections of code, typically a single basic block or a small sequence of basic blocks. The goal of local optimization is to transform code in a way that produces equivalent or better results, while improving the performance of the generated machine code.

Examples:
Dead code elimination: Removing instructions that have no effect on the program’s behaviour, such as those that operate on values that are never used.

56
Q

Describe loop optimisation

A

The goal of loop optimization is to transform loop code in a way that produces equivalent or better results while improving the performance of the generated machine code.

Example:
Loop fusion and fission: Loop fusion involves combining two or more loops into a single loop, while loop fission involves splitting a single loop into two or more loops. These techniques can be used to reduce the overhead of loop control instructions and improve locality of reference in memory access patterns.

57
Q

Describe Variable Scope

A

Variable scope refers to the region of a program in which a variable can be accessed or referenced. In most programming languages, variables are declared within a specific scope, such as a function or a block of code, and are only accessible within that scope or nested scopes.

Global scope: Variables declared outside of any function or block of code have global scope and can be accessed from anywhere in the program. Global variables are often used to store program-wide data that needs to be accessed by multiple functions or modules.

Local scope: Variables declared within a function or block of code have local scope and are only accessible within that function or block, as well as any nested functions or blocks. Local variables are often used to store temporary data or to isolate data that is only needed within a specific part of the program.

58
Q

Define Compiler

A

A compiler is a program that translates the entire source code of a program into machine code or byte code before execution, generating an executable file that can be run directly by the operating system

59
Q

Define Interpreter

A

An interpreter, on the other hand, executes the source code directly, line by line, without generating an executable file. Instead, it translates each line of code into machine code or bytecode at runtime, and then executes it.

60
Q

Define Single-pass complier

A

A single-pass compiler reads the source code only once, from beginning to end, and generates the object code in one pass.

61
Q

Define Multi-pass Compiler

A

A multi-pass compiler reads the source code multiple times, performing several analyses and optimizations before generating the object code.

62
Q

Describe Local Code optimization

A

Local code optimization applies to a single function or procedure, optimizing the code within the function to improve its performance.

63
Q

Describe Global code optimization

A

Global code optimization, on the other hand, applies to the entire program, optimizing the code across multiple functions or procedures to improve the program’s overall performance.

64
Q

Stack Definition

A

In a stack, memory is allocated and deallocated in a last-in-first-out (LIFO) manner. It is typically used for storing local variables, function call frames, and return addresses.

65
Q

Describe Heap memory

A

In a heap, memory is dynamically allocated and deallocated in a more flexible manner, using algorithms such as malloc() and free(). It is typically used for storing objects and data structures whose size and lifetime are unknown at compile time.

66
Q

You are given a function myfun(int a) which takes one (integer) parameter a.
A call x := myfun(3+2) is made where x is stored in register $s0.

(i) Describe how the above function call is made when arguments
are passed using stack-based parameter passing.

A

(i) When arguments are passed using stack-based parameter passing, the caller pushes the argument onto the top of the stack before calling the function. In this case, the caller would push the value 5 onto the stack, and then call the function. Inside the function, the value of a would be accessed by first moving the stack pointer to the top of the stack, and then accessing the value at the appropriate offset from the stack pointer. After the function returns, the caller would pop the argument off the top of the stack.

Here’s a step-by-step breakdown of the process:

  1. The caller pushes the argument 5 onto the stack.
  2. The caller jumps to the function myfun.
  3. Inside myfun, the function prologue is executed, which saves the old stack pointer and sets the new stack pointer.
  4. The function myfun accesses the value of a by moving the stack pointer to the top of the stack and accessing the value at the appropriate offset from the stack pointer.
  5. The function myfun performs its computation using the value of a.
  6. The function epilogue is executed, which restores the old stack pointer and returns control to the caller.
  7. The caller pops the argument off the top of the stack.
67
Q

When arguments are passed using register-based parameter passing in the MIPS architecture, the caller stores the argument in a designated argument register before calling the function. In this case, the caller would store the value 5 in a register, such as $a0. Inside the function, the value of a would be accessed directly from the argument register. After the function returns, the value of x would be stored in register $s0.

A

Here’s a step-by-step breakdown of the process:

The caller stores the argument 5 in a designated argument register, such as $a0.
The caller jumps to the function myfun.
Inside myfun, the function prologue is executed, which saves the old frame pointer and sets the new frame pointer.
The function myfun accesses the value of a directly from the argument register, such as $a0.
The function myfun performs its computation using the value of a.
The function epilogue is executed, which restores the old frame pointer and returns control to the caller.
The caller stores the return value of myfun in register $s0.

68
Q

Describe stack-based parameter passing:

A

Stack-based parameter passing:
In stack-based parameter passing, parameters are pushed onto the stack before a function call. The function then accesses the parameters from the stack using offsets from the stack pointer ($sp). This approach is typically used when the number of parameters exceeds the number of available registers.

69
Q

Describe register-based parameter passing:

A

Register-based parameter passing:
In register-based parameter passing, parameters are passed in registers. The MIPS architecture has designated argument registers, which are used for this purpose. The function then accesses the parameters directly from these registers.

70
Q

Describe Stack Frame

A

A stack frame is a data structure used by a computer program at runtime to store information about a subroutine or function call. It is typically implemented as a contiguous block of memory on the program’s call stack, and contains the data necessary to execute the subroutine or function and to return to the caller.

71
Q

Describe call-by-value

A

Call by value involves passing a copy of the argument value to the function. This means that any changes made to the argument inside the function do not affect the original value of the argument. In other words, the function can only modify its local copy of the value, and not the original variable that was passed as the argument.

72
Q

Describe call-by-reference

A

Call by reference, on the other hand, involves passing a reference to the original argument variable to the function. This means that any changes made to the argument inside the function affect the original value of the argument.

73
Q

Describe 3 different ways to store a variable in a runtime environment. Give
one advantage and a typical use for each of them.

A
  1. Stack-based allocation: In stack-based allocation, variables are stored on a stack data structure in a contiguous block of memory. Each function call creates a new stack frame, and local variables are allocated within that frame. This method is fast and useful for managing the call stack and storing local variables.

Stack-based allocation: Allocating variables on a stack is fast and useful for local variables and managing the call stack.

  1. Heap-based allocation: In heap-based allocation, variables are stored on the heap, a larger area of memory managed by the operating system. This method is useful for storing dynamically-allocated data structures such as arrays and linked lists, as well as large objects that cannot fit on the stack. Heap-based allocation allows for dynamic resizing of memory.

Heap-based allocation: Allocating variables on the heap is useful for dynamically-allocated data structures and allows for dynamic resizing of memory.

  1. Register-based allocation: In register-based allocation, variables are stored directly in the processor’s registers. Registers are small, fast storage locations that can be accessed very quickly. This method is useful for frequently accessed variables such as loop counters and function return values, as it can result in significant performance improvements due to the faster access times compared to memory-based storage.

Register-based allocation: Storing variables directly in the processor’s registers is fast and useful for frequently accessed variables such as loop counters and function return values.

74
Q

The difference between Source-code optimisation and target-code optimisation

A

Source-code optimization involves optimizing the code before it is compiled into machine code. This can include techniques such as removing redundant code, simplifying expressions, and reducing function call overhead. The goal of source-code optimization is to produce more efficient and faster code.

Target-code optimization, on the other hand, involves optimizing the machine code itself after it has been generated by the compiler. This can include techniques such as loop unrolling, instruction scheduling, and register allocation. The goal of target-code optimization is to produce code that runs faster and more efficiently on the target hardware.