title: Boost: parsing only variables that were previously declared
body: <p>I have pieced together a parser using boost libraries from various sources on the net. It works (although is not as clean as I would have liked) but I ran into a particular problem. In the first part of the parser I first parse the function name, then a set of arguments enclosed in parenthesis. Later on, when parsing the actual expression, in the parse <code>factor</code> I allow for numbers and variables to be parsed. However, I would like to only parse those variables that have been previously declared in the <code>vars</code> parser. Here is my grammar:</p>
  
  <pre><code>template&lt;typename Iterator&gt;
    struct exp_parser : qi::grammar&lt;Iterator, expression(), ascii::space_type&gt;
    {
      exp_parser() : exp_parser::base_type(all)
      {
        using qi::_val;
        using qi::_1;
        using qi::char_;
        using qi::double_;
        using qi::lit;
        using phoenix::at_c;
        using phoenix::push_back;
        using phoenix::bind;
  
        all =
          name [at_c&lt;0&gt;(_val) = _1] &gt;&gt; '(' &gt;&gt; vars [at_c&lt;1&gt;(_val) = _1] &gt;&gt; ')' &gt;&gt; '='
          &gt;&gt; expr [at_c&lt;2&gt;(_val) = _1];
  
        // Parsing of actual expression
        expr =
            term                            [_val = _1]
            &gt;&gt; *(   ('+' &gt;&gt; term            [_val += _1])
                |   ('-' &gt;&gt; term            [_val -= _1])
              );
  
        term =
            factor                          [_val = _1]
            &gt;&gt; *(   ('*' &gt;&gt; factor          [_val *= _1])
                |   ('/' &gt;&gt; factor          [_val /= _1])
              );
  
        factor =
            simple                          [_val = _1]
            |   '(' &gt;&gt; expr                 [_val = _1] &gt;&gt; ')'
            |   ('-' &gt;&gt; factor              [_val = bind(make_unary, UN_OP::MIN, _1)])
            |   ("sin" &gt;&gt; factor            [_val = bind(make_unary, UN_OP::SIN, _1)])
            |   ("cos" &gt;&gt; factor            [_val = bind(make_unary, UN_OP::COS, _1)])
            |   ("tan" &gt;&gt; factor            [_val = bind(make_unary, UN_OP::TAN, _1)])
            |   ('+' &gt;&gt; factor              [_val = _1]);
  
        // Prototyping of expression
        prtctd %= lit("sin") | lit("cos") | lit("tan");
        var    %= !prtctd &gt;&gt; char_('a','z');
        num    %= double_;
        simple %= var | num | ('(' &gt;&gt; expr &gt;&gt; ')');
        name   %= ((char_('a','z') | char_('A','Z') ) &gt;&gt; *(char_('a','z') | char_('A','Z') | char_('0','9') ));
        vars   %= (char_('a','z') &gt;&gt; *(',' &gt;&gt; char_('a','z')));
      }
      qi::rule&lt;Iterator, ast(), ascii::space_type&gt; expr, term, factor, simple;
  
      qi::rule&lt;Iterator, expression(), ascii::space_type&gt; all;
      qi::rule&lt;Iterator, std::string(), ascii::space_type&gt; name, prtctd;
      qi::rule&lt;Iterator, std::vector&lt;char&gt;(), ascii::space_type&gt; vars;
      qi::rule&lt;Iterator, char(), ascii::space_type&gt; var;
      qi::rule&lt;Iterator, double(), ascii::space_type&gt; num;
    };
  </code></pre>
  
  <p>And this is the struct that I use to store it all:</p>
  
  <pre><code>  struct expression {
      std::string name;
      std::vector&lt;char&gt; arguments;
      ast syntax_tree;
    };
  </code></pre>
  
  <p>Now, how can I access the <code>std::vector&lt;char&gt;</code> in the <code>factor</code> parser so that I only parse the right variables. </p>
  
  <p>Also, I am new to using boost and using this as an exercise to myself to start learning a little bit about. If anyone has any advice, please let me know on how I can clean up this code.</p>
  
  <p>Thanks in advance!</p>