Tuesday, February 20, 2018

[MATHEMATICS] Mendelbrot and Julia set

Background
When we define functions recursively, such as
f(t) = f(t-1) * f(t-1) + c
A few cases might arise, as we keep increasing number of iterations, t.
Converge: Eventually, f(t) might converge to a zero or non-zero number.
Diverge: f(t) might keep getting bigger and bigger.
Periodic: f(t) might oscillate among 2 or more values.
Chaos: f(t) might produce random values.

There are cases where a function behave might differently with the choice of base case and other constants in this case f(0) and c. For,
Example: f(t) = r * f(t - 1) * (1 - f(t - 1))
f(0) = 0.2, r = 1, f(t) converges to zero.
f(0) = 0.2, r = 2, f(t) converges to 0.52
f(0) = 0.2, r = 3, f(t) is a attenuating-oscillating function
f(0) = 0.2, r = 4 f(t) is a chaotic function

For a function, for a specific set of points in a closed boundary used as the choice for f(0) or c, the f(t) might converge or diverge fast or slowly.

Mendelbrot and Julia set
For Mendelbrot set, the function is defined as the following
f(t) = f(t-1) * f(t-1) + c
f(0) = 0 and c is chosen from numbers from complex number domain from the following rectangular boundary,
bottom-left = -2.25 -1.5i
top-right = 0.75 + 1.5i

Algorithm to generate the plot
We count number of iterations, t, for which magnitude(f(t)) reaches to 4. If f(t) converges, we say it took MAX_ITERATIONS to converge. We put computed iteration counts in a 2D array. Plotting the 2D array in 2D plane gives a visualization of the Mendelbrot set. Yellow regions depicts those points that converges so for them iterations count = MAX_ITERATIONS. Darker points say that they diverge very quickly.

f(0) = 0, c ranged from (-0.22 -0.7i to 0.75 -0.21i)


For Julia set, we fix c, and vary f(0) from a complex number boundary.

                              c = -0.35 + 0.65i and f(0) ranged from -2 to +2 in complex plane

c = -0.77 + 0.13i and f(0) ranged from -1 to +1 in complex plane

Reference:

Sunday, February 18, 2018

[Algorithms][DP] Dynamic Programming Explained - 1

Dynamic programming systematically checks all possibility of a given problem.

To solve using dynamic programming, we need to come up with a way to recursively solve a problem. The recursion should be of format of DAG, a bigger sub problem should be depended on a smaller subproblem. Dynamic problem will not work if a smaller sub-problem depends on a bigger sub-problem.

Dynamic programming problem is a favorite choice of interviewers, at it tests understanding of recursion of an interviewee.

Steps to apply dynamic programming for a given problem
0. Check if the problem can be solved using recursion. Think if you have n items, can you solve the problem if you already know solution for n-1 th item. Consider each sub problem an optimal solution. If you know an optimal solution to all reachable lower state, how can you reach higher state with that knowledge.
1. If 0 holds, write the problem in terms of smaller problem. Observe how many variables you need. The number of variables determines the dimension of the array you will need to store solution.
2. Guesses is the number of steps your algorithm needs to combine small solutions to big solution.
3. From variables, determine how many sub problems will be there. Remember, recursive function calling is cheap O(1) time if dynamic programming is used. Guesses * number of sub problems will give the order of the algorithm.

Example:
problem: find fibonacci number

Using recursion:
fibonacci(i) = fibonacci(i-1) + fibonacci(i-2)

number variables is 1 (only i). Which means Number of sub problems = n;
Guesses = O(1) [fibonacci(i-1) + fibonacci(i-2) can be evaluated in O(1) time. Recursion is cheap for dp]

O(algorithm) = O(subproblem_count * guesses_at_each_subproblem) = O(n)

Another Example:
Rod cutting problem. An i lengthed rod can be cut in 2 spots. We need to find the maximum profit we can make out of it.

profit(i) = max(profit(j) + profit(i-j)) for all 0 < j < i-1

here # of variables = 1 => # subproblems = n
#guesses = O(n) [need to generate all points from 1 to i-1 for length i]

O(n * n) = O(n ^ 2)

Yet Another Example:
Knapsack problem
profit(i, capacity) = max(
                                         profit(i-1, capacity), //don't take i th element
                                         profit(i-1, capacity-w[i]) + val[i] //take ith element
                             )
we see 2 variables here. #subproblems = item_count * bag_capacity
#guesses = O(1)

O(algorithm) = O(item_count * bag_capacity)

this is a pseudo polynomial runtime. if bag_capacity is increases, we will need bigger array to store the intermediate results.

[AlgorithmS][DP] Thinking in DP -1

5. Longest Increasing sub sequence:
Problem: Given a sequence, find the longest increasing sub sequence. LIS for {10, 22, 9, 33, 21, 50, 41, 60, 80} is 6 and LIS is {10, 22, 33, 50, 60, 80}.
Recurrence: Given an Input array, I. Build an auxiliary array of same size as I, A such that.
A(i) = max(A(j)) + 1, if an entry is found in I, such that i > j and j >= 0 and I(i) > I(j)
else A(i) = 1
Base case: A(0) = 0
Runtime:
Number of sub-problems = O(n).
Time to solve a sub-problem = O(n), as to build A(i) we need to traverse A(0) through A(i-1).
Total runtime = O(n ^ 2).

6. Given available coin values, {a, b, c} what is the minimum number of coins required to give change for n Cents.
Recurrence: f(n) = min(f(n-a) + f(n-b) + f(n-c)) + 1
base case:
f(0) = 0
f(a) = 1
f(b) = 1
f(c) = 1
f(-coinval) = + infinity
Runtime: Number of sub-problems = O(n), time to solve a sub-problem = O(m), here is m is the size of coin types.
Total runtime = O(m * n)

7. Edit distance
Problem: Given two strings a and b, find minimum number of elementary character operations (insert, delete, replace) to convert a to b.
Example:
f(UTEP, KTEP) = 1 (replace U by K)
f(RATS, BAT) = 2 (replace R by B, delete S)
Recurrence:
f(i, j) = f(i - 1, j - 1) if a[i] == b[j] 
f(i, j) = min(f(i, j - 1), f(i - 1, j), f(i - 1, j - 1)) + 1 if a[i] != b[j]
base case:
f(0, 0) = 0
f(i, 0) = i
f(0, j) = j
Explanation: f(i, j) is the edit distance of string a[0...i] and b[0...j]. Let, a = FIAS, b = FITA.
Here, we want to compute f(3, 3). Here, a[3] != b[3] so we need to insert, delete or replace.
In string a, if we choose to,
insert   A then we need to convert          FIASA -> FITA   >> f(i, j - 1)
delete   S then we need to convert          FIA -> FITA        >> f(i - 1, j)
replace S by A then we need to convert FIAA -> FITA     >> f(i -1, j - 1)
Runtime: Number of subproblems = O(n ^ 2), we are constructing a 2D array.
Time to solve a sub problem = O(1)
Total order = O(n ^ 2)
Naive order: f(i, j) ~ 3f(i-1, j-1) = O(3 ^ n)
                                                                                                    
8. Cutting Rod
Problem: A rod with n length is given. Value of all rod length 1..n is given. Find the maximum profit can be obtained by cutting the rod in optimal way.
Recurrence:
f(n) = max( max(f(n-i) + f(i)) for n > i  > 0, val[n])
base case:
f(0) = 0
Explanation: n = 4. We need to know which one gives the most profit, the max of val[4], f(1) + f(3), f(2) + f(2).
Runtime:
Number of sub-problems = O(n).
Time to solve a sub-problem = O(n), as we need to traverse all the values f(0) to f(n-1).
Total runtime = O(n ^ 2)

9. Subset Sum
Problem: Given a set of numbers, S and a number n, find if any subset of S sums up to n.
Recurrence:
f(i, n) = f(i - 1, n) | f(i - 1, n - S[i])
base case:
f(0, n) = false where n > 0
f(i, 0) = true where i >= 0
Explanation: f(i, n) represents if a subset from S[0...i] sums to n.
f(i - 1, n) is if a subset of S[0...i-1] sums to n.
f(i - 1, n - S[i]) is if a subset of S[0...i-1] sums to n - S[i] if so then including S[i] would sum to n.
Runtime:
Number of sub-problems = O(|S| * n)
Time to solve one sub-problem = O(1)
Total runtime = O(|S| * n)
The order depends on the value n holds, this scenario is known as Pseudo-Polynomial.


Saturday, February 17, 2018

[Algorithms][DP] Thinking in DP - 0

We will discuss a few problems and how to solve them using binary programming in this post.

0. n th fibonacci number:
Recurrence relation:
f(n) = f(n-1) + f(n-2)

Base case:
f(0) = 0
f(1) = 1

Complexity:
Number of sub problems = n,
Time to solve one sub problem = 0(1)
Total order = O(n)

Naive complexity: O(2 ^ n), as each recursive call will do full computation instead of using previous computed value.

1. n choose k (binomial coefficient)
Recurrence relationship:
f(n, k) = f(n-1, k-1) + f(n-1, k)
Explanation: 
From ABCD how many combination you can form taking only 3 of them?
Total combinations: ABD, ACD, BCD, ABC.

Ignore D first, compute how many 2 element item can be formed from ABC
AB, AC, BC. We can just add D at last position and we will get
ABD, ACD, BCD, this gives f(n-1, k-1).
Now, lets see how many 3 length String we can form from ABC (still ignoring D). Just 1, ABC.
this gives, f(n-1, k) 

Base case:
f(n, 0) = 1
f(n, n) = 1

Complexity:
From the equations, we will need to fill up a 2D array. Intuitively, it tells the order would be O(n ^ 2).
Number of subproblems: O(n ^ 2) [for each value of i of n, we will have to iterate all the values from 0 to i]
Time to solve one sub-problem: O(1)
Runtime = O(n^2)
Naive Complexity: O(2 ^ n)

2. Longest Common Sub-sequence:
Problem: Find the longest common sub-sequence of two strings.
Recurrence:
f(i, j) = if (a[i] == b[j])
                f(i - 1, j - 1) + 1
             else
                max(f(i, j - 1), f(i - 1, j))

Explanation: a = ABABDCD, b = AXBYCZ are two strings. Note that, the longest common sub-sequence is ABC. f(i, j) means find the length of the longest common sub-sequence in a[0..i] and b[0..j].

Base case: f(n, 0) = f(0, n) = 0
Complexity: O(n ^ 2). Same reasoning as above.
Naive Complexity: O(2 ^ n) if all the f is computed separately.
Backtracking can be applied here as well.
Generate all possible subsets of characters in string a. (2 ^ n)
Generate all possible subsets of characters in string b. (2 ^ n)
Find the sub-string of maximum length of the 2 subsets.

3. Maximum sum contiguous sub array
Problem: Given a 1-D array, find the contiguous sub array with the maximum sum.
Recurrence: At first compute all the sum of starting from i ending at j.
f(i, j) = f(i, j - 1) + f(j, j)
find the cell with the maximum value in it.

Complexity: O(n ^ 2)

4. Maximum size square sub array with all 1s:
Problem: Given a 2D array A holding 0 or 1 in cells, find the maximum sub 2D array that is square and that has all element 1.
Recurrence: Construct an auxiliary 2D array S such that
S(i, j) = min(S(i - 1, j), S(i, j - 1), S(i - 1, j - 1)) + 1 if A(i, j) = 1
S(i, j) = 0 if A(i, j)  = 0
Base case, S(0, j) = A(0, j), S(i, 0) = A(i, 0)
S(i, j) essentially holds the size of the maximum square sub array all holding 1 whose bottom right corner is A(i, j).

After constructing, S, find the S(i, j) with maximum value.

Complexity: O(n ^ 2) as we are constructing a 2D array.

Wednesday, February 14, 2018

[Algorithms][DP] Dynamic Programming Explaiend

Dynamic Programming (DP) is a technique of solving a problem that depends on a smaller instance of a smaller problem

When DP can be applied:
0. If the best solution of the larger problem can be computed from the best solution of the smaller problem(s). This property is called optimal substructure property.  If you can formulate a recursive equation such as f(n) = f(n-1) + f(n-2), you can solve the bigger problem f(n) with the result from smaller problems, which means the problem shows optimal substructure property. To know if the optimal substructure property exists or not ask the following -
"Can I solve the problem recursively?"

1. DP can be applied on the problems that shows overlapping sub problems. For instance, fibonacci(n) = fibonacci(n-1) + fibonacci(n-2). Lets see the tree structure of this problem,
                                                  f(n)
                                                 /    \
                                           f(n-1)   f(n-2)
                                            /     \         /   \
                                     f(n-2)    f(n-3) .......................

From the tree above, we can see that f(n-2) appears twice. To find if there is overlapping sub-problem property or not ask the following question-
"Am I solving the same problem more than once?"


In short, to check if DP can be applied on a given problem or not, see at first if it can be solved recursively, then see in the recursion tree if any sub-problem is occurring more than once.

Steps on formulating a problem using DP
Lets consider the problem of finding the shortest path in a graph.
0. Find variables that express the problem. To find shortest path, we need a vertex to start from (u) and a vertex we want to reach (v). For this problem u and v are the minimum number of variables to express the problem at hand. DP(u, v) would be state of the problem we want to compute.
1. Find relationship of the main problem with sub-problem states.
DP(u, v) = min(DP(u, k) + DP(k, v)) for all k
3. Build table to memorize sub problem's solutions.

Determining Order of a DP problem
0. Find the total number of sub-problems (s). For the shortest path problem, u would take all possible vertices. So, s = |V|
1. Determine how much work is done to compute a problem (w). Order of a recursive call to a sub-problem would be constant, as for DP it will be only a table look up, so for the shortest path problem,  DP(u, k) + DP(k, v) takes constant time.  We need to iterate through all k reachable from u, so w = degree(u).
3. Runt time, O(DP(u, v)) = s * w = O(|V| + |E|)

Reference:
0. MIT OCW
1. GeeksforGeeks
2. Dr. Fuentes' lecture
3.

Monday, February 5, 2018

Github Troubleshooting

# git push fails with the following

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

Steps
0. copy your public key to github.com
cat ~/.ssh/id_rsa.pub  >> copy the content

1. go to github.com > settings > ssh and gpg keys > new ssh key > paste your public key there > save

Try git push again. 

My Favorite Movies

Preference
Movies are retreat for me from my life. Usually, I like to watch a movie based on true story. These movies help me to see other perspectives of life. Danish girl, Boys don't cry, Speak, The Wrestler, Cinderella Man are name of a few movies. I am a big fan of western civilization, my movie preference reflects this. I sometimes love to see movies that has a simple story line, sometimes I like to see movies with an interesting plot. I have noticed that at a given point of time my preference of watching a movie changes based on what I want at that time. For instance, I might end up watching an inspirational movie when I am feeling bit down.

12 Angry Men ++

3 Idiots ++


A Beautiful Mind ++


American Beauty +++
The story evolves around an American family with a sexually unsatisfied father, professionally unsuccessful mother and a teen daughter with the thought she needs a breast implant. The story includes the daughter's pretty friend on whom the father has a crush, a neighborhood with a house with gay partners, a retired army person with teen aged son on drugs, and a guy who is the business idol of the mother.
Reason of liking: The movie narrates stories of untold desires and thoughts of human mind. Those desires might sound improper, yet those are beautiful in the sense that they are unique and strong among us, we cannot deny their existence.

Andaj Apna Apna 
Hilarious journey of two good looking guy's to marry a rich man's daughter.

Back to the Future ++

Boyhood +++
A boy grows up with typical incidents in a typical family of developed country.
Reason of liking: The movie gave me an idea of how a family of privileged race works in a developed country. How dads are, how moms are, how kids are brought up.

Brave heart +++
A Scot man fights the mighty British empire to take avenge his wife's death.

Cars +++

Cast Away +
A FedEx plane crashes in midst of the sea, only one person survives and find himself in a lonely island.

Catch Me if You Can ++
A good looking young man gets himself into fake currency business.

Children of Heaven +++

Cinderella Man +++
A formerly famous wrestler struggles to survive with a family depending on him and with a injury on his wrist during the great depression period of USA prior to the Second World War.

Coco ++
A music loving little kid goes to land of the dead and find surprising history of his ancestors.

Dark Knight +++
Batman fights the most brilliant psycho opponent, Joker, in order to sustain people's belief in goodness.

Dead Poets Society +++

Definitely, May be ++
A single dad tells stories of the women he dated to his little daughter. The father changes names of the women and the daughter has to find out who is her mom and which lady the father still loves.

Flipped ++
In a neighborhood a little girl has a crush on little boy. Growing upto teen-age the boy keeps ignoring the girl. Things starts getting interested when at some point the girl starts ignoring the boy.

Fight Club +++++
A regular office worker meets a rebellious person with an interesting philosophy that changes his way of life.

Finding Neverland +++
The story behind how the author of  "Peter Pan" collected the ingredients of the story from his family and surroundings.

Forrest Gump ++
An autistic person narrates story of his simple life.

Freedom Writers +++ (True Story)
During Early 90s, there was an extreme racial tension in California. One race would attack, beat, murder people from other race. Schools are full of teens who hate people from other race and education is not their primary concern when they are always in fear of death. One of those schools comes a new young female teacher, Erin. She quickly finds out her students have a difficult past. She attempts to teach them in different way. She goes through a lot of opposition, troubles and hatred from students and from fellow teachers. She does not loose hope on her students.
Reason of liking: After I watched the movie, I instantly fell in love with Erin's spirit. I like that she said, when we are young that is the time for us to go for our dreams. She is a strong woman, she is not a feminist. She treated men and women the same. This is type of women I would definitely love to stand by and walk with on the way of life.

Gladiator ++
A roman general falls into victim of a conspiracy and eventually sold as a gladiator.

Good Will Hunting
A teen janitor of MIT turns out to be a mathematical genius. He had a difficult childhood, now he is confused and insecured, he does not know what to do with his life. A MIT professor wants him to contribute in the field of mathematics while a counselor from a Community College thinks the boy should decide what the boy wants to do by himself without any external rush or push.
Reason of liking: I kind of felt the movie tells the story what would have happened if Ramanujan, the great mathematician, was born in modern day USA.

Hirok Rajar Deshe +++
A group of two singers and a teacher tries to fight against tyranny.

In to the Wild ++++ (true story)
An american recent college graduate finds that this society is full of falseness, fake smiles and unnecessary works. He decides to leave home for good and live in a place where no people lives. He sets out his journey and meets many people on his way.
Reason of liking: This movie reflects my dream and agrees with my philosophy. I felt deeply connected with the boy.

Indiana Jones series +
A history professor becomes treasure-hunter due to circumstances.

Inside Out ++

Interstellar ++
Human beings tries to survive as the earth starts becoming inhabitable.

It's a wonderful life +++
A man wants to commit suicide. Eventually, his friends and good deeds helps him get out of the situation.

Kingdom of Heaven ++
A crusader fights back Saladin's seize to rescue people in Jerusalem.

Krishna Kanter Will +++
A young rich honest man's fate slowly gets altered by actions of people in surrounding and his impulsive acts.

Kungfu Panda ++
A big fat panda wants to be a master of kung fu. Eventually, he has to fight against the greatest masters.

Lawless ++

Les Miserable +++

Life is Beautiful +++
A jew father in a Italian concentration camp tries to cheer up his little boy with his imagination.

Little Manhattan ++
A 10 year old boy falls in love with a 11 year old girl and expresses his emotions and feelings in soliloquy.

Little Miss Sunshine +++
A family has an eventful journey to California that would change perception of the family members.

Maha Nagar +
Life of a middle class family during 1960 in Kalkata.

Malena +

Matrix +++
A man wakes and finds he has been inside a simulation all his life.

Match Point +++
A poor tennis trainer gets married to a rich man's daughter. Things starts changing when he gets attracted by a poor but extremely beautiful actress.

Meet the Robinsons +++
A geeky-orphan boy attempts to build a time machine to find his mother.

Mongol +++
Life of the legendary warrior, Genghis Khan.

Mr. India(1987) +

National Treasure 
A treasure hunter's journey to the treasure of the knight-templer through clues hidden by the founding fathers of America.

Nayak(1966) +++
A famous actor reveals his secret to good looking reporter during a train journey.

Nayak(2001) 
A courageous tv anchor gets chance to be the head of India for one day.

Nightcrawler ++
An ambitious man gets involved in collecting breaking news for tv channels.

Now You See Me ++
During a show in US, a group of magicians robs a bank in Paris.

Pirates of the Caribbean
Fantasy journey of pirates during British period.

Prisoners ++
A father tries to find his lost 6 year old daughter.

Rang De Basanti +++

Ratatouille ++

Real Steel 
A single dad agrees to take care of his son for money.

Remember the Titans +
An African-American football couch comes in city of USA where there is a racial tension.

Rudy ++
A short and weak guy wants to be an American football player.

Taare Zameen Par +++

The Danish Girl

The Devil's Advocate +++

The Godfather +++

The Patriot +++
An American father of a big family eventually gets involved with the liberation war against Britain.

The Reader +

The Sixth Sense +
A doctor tries to help a kid who can see ghosts.

The Wolf of Wall Street

Titanic +++

Seven Samurai ++

Shawshank Redemption +++
A young bright banker gets life sentence for the false charge of murdering his wife.

Shutter Island ++
A US detective comes to visit a dangerous asylum and finds there is a conspiracy going on.

Speak +
A teen-aged girl suddenly starts behaving abnormally after her summer camp.

Stand and Deliver +++

Terminal ++
A foreigner gets stucked in a US airport and has to stay there for legal causes.

Theory of Everything +++
Life of Stephen Hawking.

The Butterfly Effect +++
A young man tries to prevent unfortunate events of life of his loved ones by frequently going to past.

The Curious Case of Benjamin Button ++
Benjamin Button is born as an old, he gets younger as time passes by.

The Hunt ++
A kindergarten school teacher gets accused for sexual harassment on little kindergarten girl. His and his family's life becomes very difficult with the hatred of surrounding people.

The Man from the Earth +++
A history middle-aged professor claims he is thousands of years of age in his farewell party.

The Prestige +
Two gifted magicians become rivals willing to harm each other after unwanted events.

The Professional ++
A hitman's life changes once he saves life of a little girl.

The Pursuit of Happyness +++

The Secret Life of Walter Mitty

The Wrestler +++

To Kill a Mockingbird

Toy Story +++
Life of a group toys that belong to a young boy.

Up ++

Wall-e ++




Watch List:
Full Metal Jacket
Amelie
Singing in the Rain
Some like it hot
Bicycle theives
The boy in stripped pajama
Graveyard of the fire flies
3:10 to yuma

Sunday, February 4, 2018

[Compiler] Inside My Implementation of the Jack Compiler

A compiler would take a program written in a high level language and translate the instruction to the instruction for a Virtual Machine(VM).

#Modules
A compiler has the following modules
0. Tokenizer
1. Parser
2. Symbol table generator
3. VM code generator

# Implementation Detail
0. Tokenizer
A language defines its basic building blocks (tokens). These basic building blocks contains comment structure, keywords, symbols, identifier logic,  literals(integer, string etc). A parser would take a program and generate stream of tokens as defined by a given language.

The tokenizer gives the following apis
i. hasMoreTokens()
ii. advance()
iii. getCurrentToken()

Algorithm:
i. Load given program in to memory
ii. Remove all the comments
iii. Add a space where 2 tokens are joined (eg. convert 5; to 5 <Space> ;)
iv. Look for a character
             if (character != ")
                curIndex = i
                j = index of next space
                subString = extract(i, j)
                using regular expression, check if the subString is a proper token, if so store in a list raise                     an exception otherwise
            if (character == ")
                look for next " and extract the substring and store in the token list


1. Parser
Uses a tokenizer and checks if the token sequence is in harmony with the grammar of the language.

Implementation:
Lets consider rule for class
class   >>>
    'class' identifer '{'
              classVarDec*
              subroutineDec*
     '}'

A set of function related to each grammar construct and bunch of helper function helped me writing the parser module.
Example:
For the rule above, I wrote the following method in the parser

void CompileClass(){
  keyword(CLASS);
  identifier();
  symbol('{');
  classVarDec_zero_more();
  subroutineDec_zero_more();
  symbol('}');  
}
Here compileClass is the method related to the grammar rule and classVarDec_zero_more() is a helper function.
the method symbol(char) was implemented as follows

void symbol(s)
  curTok = tokenizer.getCurrentToken();
  if(curTok.type != SYMBOL)
    raise exception
   
  if(curTok.val != s)
    raise exception
 
  tokenizer.advance()
end

2. Symbol Table
A symbol table is used to translate a variable declared in a high level language to a (segment, offset) pair of a virtual machine. Symbol table is divided in two spaces, one for variables declared in class level another one for subroutine level

API
i. define(varName, kind(static, field etc), type(String, CustomClass etc))
  called when a variable declaratrion is encountered
ii. startMethodLevelSymbolTable()
  called when a method declaration is encountered
iii. getInfo(varName)
  used to resolve a variable name encountered

An instance of symbol table needs to be added on the parser. As the parser process any variable declaration, an entry needs to be created in the symbol table.

3. VM Code generator
A VM Code generator needs to be augmented within the parser. As the parser parses through a given source code, a VM Code Generator would gather context and when appropriate will generate proper VM Code. This module keeps an eye on the following cases
i. Function declartion handling
A VM only cares about subroutines. A compiler needs to translate and write all the subroutines in a output file. To resolve name collision, when a subroutine subRot is encountered in class MyClass, the VM code generator would give the subroutine the name "MyClass.subRot". Here is how it deals with different subroutine types.

#Static
Observes how many local variables declared in the static method (lets call it n) and then generate the following code
function  MyClass.subRot n

#Construtor
Observes how many local variables are declared(localCount) from parser and how many fields the MyClass has(fieldCount) from symbol table. Then generates the following code-

function MyClass.new localCount
  call Memory.Alloc(fieldCount)
  pop pointer 0 //pointer 0 is used for current object also known as this
 
#Method
A method implicitly expects the object to act on as its first argument. It observes how many local variables are declared(localCount) from parser then generates the following code-

function MyClass.subRot localCount+1
  push arg 0
  pop pointer 0 //sets this pointer
 
The above code essentially sets this pointer to the object on which the method is called.

# Variable declaration
Create an entry in appropraite space of the symbol table with proper context.

# Expressions
An expression is evaluated and is value is pushed to stack.
## for an int value, generate -> push value.
## True -> generate -> push -1 //notice -1 means 11111... in memory
## False, null -> push 0
## "ab" generate the following
  call Memory.alloc 3 //3 characters in the string
  call String.appendChar 97 //ascii value of 'a'
  call String.appendChar 98
## variable, a variable is resolved using the symbol table. For example, lets say a variable var was declared in a method and it was the 2nd variable. So this variable exists in local segemnt's 2nd index. it will be resolved as following
  push local 2
A variable declared in a class, or declared as staic or declared as an argument would resolved with a proper segment and a proper offset.
 
## binary operator, an expression with the form (exp1 op exp1) is translated in the following way
  evaluate exp1 //exp1's value would be on top of the stack
  evaluate exp2
  op //apply the operator on the top two values of the stack
 
## unary operator, op exp
  evalute exp
  op
 
## for a method call myMethod(1, 2) of class MyClass, the following code will be generated
  push pointer 0 //push this object's reference to stack
  push constant 1
  push constant 2
  call MyClass.myMethod 3 //3 is the number of arguments the caller has passed to the function
 
## To translate a call of myObj.myMethod(1,2) using the symbol table, Code generator module finds its type lets say MyClass. Then it generates the following-
  push myObj //pseudo code, myObj needs to be resolved using symbol table as described earlier.
  push 1
  push 2
  call MyClass.myMethod 3
 
## To translate a static method MyClass.myStatic(1,2) the Code generator does not have to push any objects reference as the first argument. Other than that, it produces VM code as following the logic earlier.

## Access array index, myObj[exp] is translated as follows
  evaluate exp
  push segment offset //myObj variable's segment offset
  add
  pop pointer 1 //set that pointer (pointer 1), with the address of myObj[exp]
  push that 0 // push the value of myObj[exp] to stack

# Statements
## return : Every subroutine is expected to produce a result and push it to the stack.
return; is translated as
  push constant 0
  return
 
return exp; is translated as -
  evaluate exp //the value of exp will be on top of the stack
  return
 
## method call. A method call statement, do myMethodCall(1,2); would be translated as follows
  generate code for myMethodCall(1,2) discussed in the Expression section
  pop temp 0 // discard the value generated by the function as it will not be used
 
## assignment
  ### let var = exp; using symbol table first finds segment, offset of the var. Then the following code gets generated
    evaluate exp
    pop segment offset
   
  ### let var[exp1] = exp2
    evaluate exp2
    evaluate exp1
    push segment offset
    add
    pop pointer 1
    pop that 0
   
## If else
  if(cond) { stmts1 } else { stmts2 } is translated as follows
 
  if-goto IF-TRUE
  goto IF-FALSE
  label IF-TRUE
 
  generate code for stmts1
 
  goto IF-END
  label IF-FALSE
 
  generate code for stmts2
 
  label IF-END

## while
  while(cond) { stmts } is translated as follows
 
  label WHILE-BEGIN
  evaluate cond
  not
  if-goto WHILE-END
 
  generate code for stmts
 
  goto WHILE-BEGIN
  label END

Conclusion:
To build a compiler from the scratch, start with the tokenizer module. Build parser module with the the help of tokenizer. Write symbol table module and integrate it to the Parser. Write VM Code generator module, then augment it to the Parser module; also add proper context capturing code in the Parser module.

Reference:
0. My compiler implementation
1. Elements of computing: Building a modern computer from the first principle
2. A modern compiler generation in Java
3. Stanford Compiler course by Dr. Alex Aiken
4. GATE lecture on Compilers by RavindraBabu