title: Generating intermediate code in a compiler. Is an AST or parse tree always necessary when dealing with conditionals?
body: <p>I'm taking a compiler-design class where we have to implement our own compiler (using flex and bison). I have had experience in parsing (writing EBNF's and recursive-descent parsers), but this is my first time writing a compiler.</p>
  
  <p>The language design is pretty open-ended (the professor has left it up to us). In class, the professor went over generating intermediate code. He said that it is not necessary for us to construct an Abstract Syntax Tree or a parse tree while parsing, and that we can generate the intermediate code as we go.</p>
  
  <p>I found this confusing for two reasons:</p>
  
  <ul>
  <li><p>What if you are calling a function <em>before</em> it is defined? How can you resolve the branch target? I guess you would have to make it a rule that you have to define functions before you use them, or maybe pre-define them (like C does?)</p></li>
  <li><p>How would you deal with conditionals? If you have an <code>if-else</code> or even just an <code>if</code>, how can you resolve the branch target for the <code>if</code> when the condition is <code>false</code> (if you're generating code as you go)?</p></li>
  </ul>
  
  <p>I planned on generating an AST and then walking the tree after I create it, to resolve the addresses of functions and branch targets. Is this correct or am I missing something?</p>