Recognition: yes/no S ∈ L(G)
Parsing: proving S ∈ L(G) by providing a derivation
CFG is a 4-tuple
N: finite set of non-terminals
T: finite set of terminals
S: Start symbol, S ∈ N
P: finite set of production rules
Definition of derivation
a sequence a0, a1, a2, ...... an
where a0 is the start symbol, an is a sequence of terminals and
ai ⇒ ai+1 example of a derivation
Derivation and Parse Tree
A derivation specifies a unique parse tree.
A parse tree has many derivations.(can start from left subtree of right subtree, for example)
A input string has many derivations.
Leftmost/Rightmost derivation
Always expand the leftmost/rightmost non-terminal.
A parse tree has a unique leftmost/rightmost derivation!
Ambiguous grammer
A grammer is ambiguous if there exist a string in the language for which there are multiple parse trees, even when using the same derivation style. (i.e Leftmost/rightmost)
The first set of a nonterminal is the set of all terminals that might start that rule in the parser's input. For example, if we have A→B,B→x, then the first set of A includes x.
The follow set of a nonterminal is the set of all terminals that might follow the rule in the parser's input. For example, if we have A→BC,B→x,C→y, the follow set of B includes yy.
A nonterminal is nullable if and only if it can derive ε.
LL(1)
In the name LL(1), the first L stands for scanning the input from left to right, the second L stands for producing a leftmost derivation, and the 1 stands for using one input symbol of lookahead at each step to make parsing action decision
WLP4 uses LR1, which is more powerful than SLR(1) and LR(0)
A key thing to notice is that the Reduce function looks at the top of the state stack. If it contains reducible items,
the choice to reduce is made if the reduce is indicated when the next symbol is a.
In compiler optimization, register allocation is the process of assigning a large number of target program variables onto a small number of CPU registers.
In general words, keep variables that are used most often in registers, so they can be accessed quickly.
Method Inlining
Replace a funciton call with the body of the function.
Useful when the calle is short. Doing this saves us from saving and restoring registers between procedures.
Tail Recursion
int fib(int n, int a){
if(n == 0) return a;
else return fib(n - 1, n * a); // tail recurion
}
Reusing the current stack frame for the recursive call.
Memory Management
Internal Fragmentation vs External Fragmentation
Internal fragmentation is the wasted space within each allocated block because of rounding up from the actual requested allocation to the allocation granularity.
External fragmentation is the various free spaced holes that are generated in either your memory or disk space.
Free List Algorithm
Maintain a list of pointer to free blocks
when allocate x bytes, (x + 4) bytes are allocated, where the first four byte stores the integer x, aka the size of the block.
Since we allocate an extra word to store the size, when delete c, we get to address c[- 1] to delete the pre-allocated (x + 4) bytes.
Fragmentation may happen and we have three strategies:
First fit.
Best fit (leaves the smallest hole).
Worst fit (leaves the biggest hole).
Binary Buddy Algorithm
Creates internal fragmentations
Allocate blocks that are powers of 2
For example, when allocate 20 words, we need (20 + 1) = 21 words, (same as Free List Algorithm). Instead of allocating 21 words, we allocate 32 words. since 32 = 24