title: Does a line profiler for code require a parse tree and is that sufficient?
body: <p>I am trying to determine what is necessary to write a line profiler for a language, like those available for Python and Matlab.</p>
  
  <p>A naive way to interpret "line profiler" is to assume that one can insert time logging around every line, but the definition of a line is dependent on how a parser handles whitespace, which is only the first problem.  It seems that one needs to use the parse tree and insert timings around individual nodes.</p>
  
  <p>Is this conclusion correct?  Does a line profiler require the parse tree, and is that all that is needed (beyond time logging)?</p>
  
  <hr>
  
  <p>Update 1: Offering a bounty on this because the question is still unresolved.</p>
  
  <p>Update 2: Here is a link for a well known <a href="http://packages.python.org/line_profiler/" rel="nofollow noreferrer">Python line profiler</a> in case it is helpful for answering this question.  I've not yet been able to make heads or tails of it's behavior relative to parsing.  I'm afraid that the code for the Matlab profiler is not accessible.</p>
  
  <p>Also note that one could say that manually decorating the input code would eliminate a need for a parse tree, but that's not an automatic profiler.</p>
  
  <p>Update 3: Although this question is language agnostic, this arose because I am thinking of creating such a tool for R (unless it exists and I haven't found it).  </p>
  
  <p>Update 4: Regarding use of a line profiler versus a call stack profiler - <a href="https://stackoverflow.com/questions/7252602/digging-into-r-profiling-information/7297445#7297445">this post</a> relating to using a call stack profiler (<code>Rprof()</code> in this case) exemplifies why it can be painful to work with the call stack rather than directly analyze things via a line profiler.</p>