title: Getting a value from the top most rule in a mid/bottom one
body: <p>I'm doing my little own language and I'm trying to make there be blocks, but I'm quite stuck on how to get track of the current block (as I need to know which one a variable was created in and so-on blah blah).</p>
  
  <p>My grammar file looks something like this (for simplicity's sake, it's not the whole code):</p>
  
  <pre><code>%{
      struct Node *nodest = NULL;
      struct Node *currentblock = NULL;
  %}
  
  %start source
  
  %%
  
  source
      : stmts { nodest = block($1); currentblock = nodest; }
      ;
  
  stmts
      : stmt
      | stmts stmt
      ;
  
  stmt
      : expr_stmt
      | iter_stmt
      | select_stmt
      | comp_stmt
      ;
  
  expr_stmt
      : ';'
      | expr ';'
      ;
  
  expr
      : binary_expr
      | assign_expr
      | call_expr
      | decl_expr
      | init_expr
      | VAR_IDENT
      | INTEGER
      | '(' expr ')'
      ;
  
  comp_stmt
      : '{' '}'
      | '{' stmts '}' { $$ = block($2); currentblock = $$; }
      ;
  
  decl_expr
      : type VAR_IDENT  { $$ = declaration($1, $2, currentblock) }
      ;
  /* ... */
  
  type
      : TYPE_INT
      ;
  </code></pre>
  
  <p>And that won't work, obviously, because <code>nodest</code> (being a block node that holds every other node) is actually given some value at the very end of creating the AST, so it's NULL for the rest of time, so <code>currentblock</code> can't be used like in <code>decl_expr</code>, because it's NULL at the time.</p>
  
  <p>So my question is: how could I get the value (to where it points to or whatever) of <code>nodest</code> later on in the code?</p>
  
  <p>Or, if it's not really possible, could you give me some notes/hints on how could I get this going?</p>