title: Parsing: how can I generate a more concise AST?
body: <p>I'm developing a parser for learning purposes that follows some user-defined rules to create ASTs. During AST generation, however, I can't find a way to abstract the tree - all tokens/intermediate node types are maintaned in the final tree structure. </p>
  
  <p>It's easier to explain with an example:</p>
  
  <p>User-defined structures:</p>
  
  <pre><code>struct ASTExpr { };
  struct ASTNumber : ASTExpr  { int value; };
  struct ASTAddition : ASTExpr { ASTExpr* rhs, lhs; };
  </code></pre>
  
  <p>User-defined rules:</p>
  
  <pre><code>[0..9]              -&gt; ASTNumber
  ASTExpr [+] ASTExpr -&gt; ASTAddition
  ASTNumber           -&gt; ASTExpr
  ASTAddition         -&gt; ASTExpr
  </code></pre>
  
  <p>Example input:</p>
  
  <pre><code>1 + 5
  </code></pre>
  
  <p>Example parsing process/output:</p>
  
  <pre><code>          (ASTExpr)
                |
          (ASTAddition)
                |
        /----------------\
        |       |        |
    (ASTExpr)  [+]   (ASTExpr)
        |                |
   (ASTNumber)      (ASTNumber)
        |                |
       [1]              [5]
  </code></pre>
  
  <p>As you can see the tree is very complex and not very "abstract". The <code>(...)</code> nodes are classes, the <code>[...]</code> nodes are tokens. </p>
  
  <p>This is what I would prefer:</p>
  
  <pre><code>        (ASTAddition)
                |
        /----------------\
        |                |
   (ASTNumber)      (ASTNumber)
  </code></pre>
  
  <p>In the desired version of the tree, the tokens and the intermediate <code>ASTExpr</code> nodes are not present.</p>
  
  <hr>
  
  <p><strong>How can I make my AST tree more "abstract"?</strong> Do I have to change the ruleset? What is usually done to ensure that the final AST contains less noise?</p>