title: (f)lex in c and bison/yacc in C++
body: <p>I am trying to build a mini programming languages like ruby with flex/bison. Flex and bison work fine together if it's all writin in C. The problem starts when I need c++ for building classes for my nonterminals (expr, statements etc.).</p>
  <p>mRuby.l:</p>
  <pre><code>%option yylineno
  
  %{
  #include &quot;absyn.h&quot;
  #include &quot;mRuby.tab.h&quot;
  
  int line_nr = 1;
  int col_nr = 1; 
  
  /*
  col_nr += strlen(atoi(yytext));
  */
  
  %}
  
  identifier  [A-z]([A-z]|[0-9])*
  integer  -?([0-9])+(_?[0-9])*
  comment  (&quot;#&quot;.*&quot;\n&quot;)
  whitespace (&quot; &quot;|&quot;\t&quot;)+
  boolean (true|false)
  CR (\r\n)*|(\n)*|(\r)*
  
  
  %%
  &quot;;&quot;                     { return SEMICOLON; }
  &quot;undef&quot;                 { return UNDEF; }
  &quot;def&quot;                   { return DEF; }
  &quot;(&quot;                     { return LPAREN; }
  &quot;)&quot;                     { return RPAREN; }
  &quot;end&quot;                   { return END;}
  &quot;return&quot;                { return RETURN;}
  &quot;if&quot;                    { return IF; }
  &quot;then&quot;                  { return THEN; }
  &quot;elsif&quot;                 { return ELSIF;}
  &quot;else&quot;                  { return ELSE; }
  &quot;unless&quot;                { return UNLESS; }
  &quot;while&quot;                 { return WHILE; }
  &quot;do&quot;                    { return DO; }
  &quot;until&quot;                 { return UNTIL; }
  &quot;case&quot;                  { return CASE; }
  &quot;when&quot;                  { return WHEN; }
  &quot;,&quot;                     { return COMMA; }
  &quot;=&quot;                     { return ASSIGN; }
  &quot;+=&quot;                    { return PLUSASSIGN; }
  &quot;-=&quot;                    { return MINUSASSIGN; }
  &quot;*=&quot;                    { return MULASSIGN; }
  &quot;/=&quot;                    { return DIVASSIGN; }
  &quot;&amp;&amp;=&quot;                   { return ANDASSIGN; }
  &quot;||=&quot;                   { return ORASSIGN; }
  &quot;+&quot;                     { return PLUS; }
  &quot;-&quot;                     { return MINUS; }
  &quot;*&quot;                     { return MUL; }
  &quot;/&quot;                     { return DIV; }
  &quot;&gt;&quot;                     { return GT; }
  &quot;&gt;=&quot;                    { return GE; }
  &quot;&lt;&quot;                     { return LT; }
  &quot;&lt;=&quot;                    { return LE; }
  &quot;==&quot;                    { return EQ; }
  &quot;!=&quot;                    { return NE; }
  &quot;&amp;&amp;&quot;                    { return AND; }
  &quot;||&quot;                    { return OR; }
  &quot;!&quot;                     { return NOT; }
  &quot;\n&quot;                    { col_nr = 1; return SEMICOLON; }
  {boolean}               { return BOOLEAN; }  
  {comment}|{whitespace}  { /* doe niets */ }
  {integer}               {
  
                          return INTEGER; 
                          }
  {identifier}            { 
                          char* s = (char*) malloc(yyleng+1);
                          strcpy(s, yytext);
                          
                          return IDENTIFIER;
                          }
  
  .      {
    if (yytext[0] &lt; ' '){ 
      /* non-printable char */
      /*yyerror*/ 
      fprintf(stderr,&quot;illegal character: ^%c&quot;,yytext[0] + '@'); 
    }
    else {
      if (yytext[0] &gt; '~') {
        /* non-printable char printed as octal int padded with zeros, eg \012*/
        /*yyerror(*/
        fprintf(stderr,&quot;illegal character: \\%03o&quot;, (int) yytext[0]);
      }
      else {
        /*dit is een functie verwijder enter om te gebruiken*/
        /*yyerror(*/
        fprintf(stderr,&quot;illegal character: %s&quot;,yytext);
      }
    }
    /* lex read exactly one char; the illegal one */
    //fprintf(stderr,&quot; at line %d column %d\n&quot;, line_nr, (col_nr-1));
    fprintf(stderr,&quot; at line %d column %d\n&quot;, yylineno, (col_nr-1));
                     }
  %%
  
  /* Function called by (f)lex when EOF is read. If yywrap returns a
     true (non-zero) (f)lex will terminate and continue otherwise.*/
  int yywrap(){
    return (1);
  }
  </code></pre>
  <p>My bison file:</p>
  <pre><code>%{
  #include &quot;lexer.h&quot;
  #include &quot;absyn.h&quot;
  #include &lt;iostream&gt;
  
  
  void yyerror(const char* str);
  
  int main(int argc, char* argv[]){
    int tokenid;
    std::cout &lt;&lt; &quot;Hello world! \n&quot;;
  
    //return yyparse();
    yyparse();
  
    std::cout &lt;&lt; &quot;TEST \n&quot;;
    return 0;
  }
  
  %}
  
  %union {
    int g;
    char* id;
    char* b;
    Stmts stmts;
    Stmt stmt;
    CaseStmt casestmt;
    WhenStmt whenstmt;
    IfStmt ifstmt; 
    ElifStmt elifstmt;
    Expr expr;
    Exprs exprs;
    ArgList arglist;
    ArgLists arglists;
    Ids ids;
    T t;
    Assignop assignop;
    Binop binop;
  }
  // vul aan met tokendeclaraties
  
  %token
   SEMICOLON UNDEF DEF LPAREN RPAREN END RETURN INTEGER
   IF THEN ELSIF ELSE UNLESS WHILE DO UNTIL CASE WHEN COMMA
   ASSIGN PLUSASSIGN MINUSASSIGN MULASSIGN DIVASSIGN ANDASSIGN ORASSIGN
   PLUS MINUS MUL DIV GT GE LT LE EQ NE AND OR NOT
  
  %token &lt;id&gt; IDENTIFIER
  %token &lt;b&gt; BOOLEAN
  
  %type &lt;stmts&gt; stmts
  %type &lt;stmt&gt; stmt
  %type &lt;casestmt&gt; casestmt
  %type &lt;whenstm&gt; whenstmt
  %type &lt;ifstmt&gt; ifstmt
  %type &lt;elifstm&gt; elifstmt
  %type &lt;expr&gt; expr
  %type &lt;exprs&gt; exprs
  %type &lt;arglist&gt; arglist
  %type &lt;arglists&gt; arglists
  %type &lt;ids&gt; ids
  %type &lt;t&gt; t 
  %type &lt;assignop&gt; assignop
  
  
  %type &lt;binop&gt; binop
  %type &lt;binop&gt; PLUS
  
   // vul aan met voorrangdeclaraties
  %nonassoc operation
  %nonassoc expression
  
  %nonassoc OR NE EQ LT LE GT GE AND
  
  %left PLUS MINUS
  %left TIMES DIVIDES
  %right ASSOP
  %right UNOT
  %right UMINUS
  //%defines
  
  %%
  
  // vul aan met producties
  program   : compstmt            { std::cout &lt;&lt; &quot;program 0&quot;; }
  ;
  
  compstmt  : stmts              { std::cout &lt;&lt; &quot; compstmt 1&quot;; }
            | stmts t            { std::cout &lt;&lt; &quot; compstmt 2&quot;; }
  ;
  
  stmts     : stmt               { std::cout &lt;&lt; &quot; stmts 1&quot;; }
            | stmts t stmt       { std::cout &lt;&lt; &quot; stmts 2 &quot;; }
  ;
  
  stmt      : undefstmt     { std::cout &lt;&lt; &quot; stmt 1&quot;; }
            | expr          { std::cout &lt;&lt; &quot; stmt 2&quot;; }
            | defstmt       { std::cout &lt;&lt; &quot; stmt 3&quot;; }
            | returnstmt    { std::cout &lt;&lt; &quot;stmt 4&quot;; }
            | ifstmt        { std::cout &lt;&lt; &quot;stmt 5&quot;; }
            | whilestmt     { std::cout &lt;&lt; &quot; stmt 6&quot;; }
            | untilstmt     { std::cout &lt;&lt; &quot; stmt 7&quot;; }
            | unlessstmt    { std::cout &lt;&lt; &quot; stmt 8&quot;; }
            | casestmt      { std::cout &lt;&lt; &quot; stmt 9&quot;; }
            | error         { std::cout &lt;&lt; &quot; error&quot;; }
  ;
  
  undefstmt : UNDEF IDENTIFIER                                      { std::cout &lt;&lt; &quot; undefstmt&quot;; }
  ;
  
  defstmt   : DEF IDENTIFIER LPAREN arglists RPAREN compstmt END    { std::cout &lt;&lt; &quot; defstmt&quot;; }
  ;
  
  returnstmt : RETURN expr                                          { std::cout &lt;&lt; &quot; returnstmt&quot;; }
  ;
  
  whilestmt : WHILE expr DO compstmt END                            { std::cout &lt;&lt; &quot; whilestmt&quot;; }
  ;
  
  untilstmt : UNTIL expr DO compstmt END                            { std::cout &lt;&lt; &quot; untilstmt&quot;; }
  ;
  
  unlessstmt  : UNLESS expr THEN compstmt END                         { std::cout &lt;&lt; &quot; unless 1&quot;; }
              | UNLESS expr THEN compstmt ELSE compstmt END           { std::cout &lt;&lt; &quot; unless 2&quot;; }
  ;
  
  casestmt  : CASE expr WHEN expr THEN compstmt END                         { std::cout &lt;&lt; &quot; casestmt &quot;; }
            | CASE expr WHEN expr THEN compstmt ELSE compstmt END           { std::cout &lt;&lt; &quot; casestmt &quot;; }
            | CASE expr WHEN expr THEN compstmt whenstmt END                { std::cout &lt;&lt; &quot; casestmt &quot;; }
            | CASE expr WHEN expr THEN compstmt whenstmt ELSE compstmt END  { std::cout &lt;&lt; &quot; casestmt &quot;; }
  ;
  whenstmt  : WHEN expr THEN compstmt                           { std::cout &lt;&lt; &quot; whenstmt &quot;; }
            | whenstmt WHEN expr THEN compstmt                  { std::cout &lt;&lt; &quot; whenstmt &quot;; }
  ;
  ifstmt    : IF expr THEN compstmt END                         { std::cout &lt;&lt; &quot;ifstmt&quot;; }
            | IF expr THEN compstmt ELSE compstmt END           { std::cout &lt;&lt; &quot;ifstmt&quot;; }
            | IF expr THEN compstmt elifstmt END                { std::cout &lt;&lt; &quot;ifstmt&quot;; }
            | IF expr THEN compstmt elifstmt ELSE compstmt END  { std::cout &lt;&lt; &quot;ifstmt&quot;; }
  ;
  
  elifstmt  : ELSIF expr THEN compstmt                { std::cout &lt;&lt; &quot; elifstmt &quot;; }
            | elifstmt ELSIF expr THEN compstmt       { std::cout &lt;&lt; &quot; elifstmt &quot;; }
  ;
  
  expr      : IDENTIFIER                              { std::cout &lt;&lt; &quot; expr 1&quot;; }
            | IDENTIFIER assignop expr %prec ASSOP    { std::cout &lt;&lt; &quot; expr 2&quot;; }
            | NOT expr %prec UNOT                     { std::cout &lt;&lt; &quot; expr 3&quot;; }
            | BOOLEAN                                 { std::cout &lt;&lt; &quot; expr 4&quot;; }
            | MINUS expr %prec UMINUS                 { std::cout &lt;&lt; &quot; expr 5&quot;; }
            | IDENTIFIER LPAREN RPAREN                { std::cout &lt;&lt; &quot; expr 6&quot;; }
            | IDENTIFIER LPAREN exprs RPAREN          { std::cout &lt;&lt; &quot; expr 7&quot;; }
            | expr binop expr                         { std::cout &lt;&lt; &quot; expr 8&quot;; }
  ;
  
  exprs     : expr                      { std::cout &lt;&lt; &quot; exprs &quot;; }
            | exprs COMMA expr          { std::cout &lt;&lt; &quot; exprs &quot;; }
  ;
  
  arglists  : arglist           { std::cout &lt;&lt; &quot; arglists &quot;; }
            | arglists arglist  { std::cout &lt;&lt; &quot; arglists &quot;; }
  ;
  
  arglist   : IDENTIFIER        { std::cout &lt;&lt; &quot; arglist &quot;; }
            | IDENTIFIER ids    { std::cout &lt;&lt; &quot; arglist &quot;; }
  ;
  
  ids       : SEMICOLON IDENTIFIER       { std::cout &lt;&lt; &quot; ids &quot;; }
            | SEMICOLON IDENTIFIER ids   { std::cout &lt;&lt; &quot; ids &quot;; }
  ;
  
  t         : SEMICOLON     { std::cout &lt;&lt; &quot; t &quot;; }
  ;
  
  assignop  : ASSIGN        { std::cout &lt;&lt; &quot;assop&quot; ; }
            | PLUSASSIGN    { std::cout &lt;&lt; &quot;assop&quot; ; }
            | MINUSASSIGN   { std::cout &lt;&lt; &quot;assop&quot; ; }
            | MULASSIGN     { std::cout &lt;&lt; &quot;assop&quot; ; }
            | DIVASSIGN     { std::cout &lt;&lt; &quot;assop&quot; ; }
            | ANDASSIGN     { }
            | ORASSIGN      { }
  ;
  
  binop     : PLUS  { Binop op = $1; $$ = op;  }
            | MINUS { std::cout &lt;&lt; &quot;expr MINUS expr\n&quot;; }
            | MUL   { std::cout &lt;&lt; &quot;expr MUL expr\n&quot;; }
            | DIV   { std::cout &lt;&lt; &quot;expr DIV expr\n&quot;; }
            | LE    { std::cout &lt;&lt; &quot;expr LE expr\n&quot;; }
            | LT    { std::cout &lt;&lt; &quot;expr LT expr\n&quot;; }
            | GE    { std::cout &lt;&lt; &quot;expr GE expr\n&quot;; }
            | GT    { std::cout &lt;&lt; &quot;expr GT expr\n&quot;; }
            | EQ    { std::cout &lt;&lt; &quot;expr EQ expr\n&quot;; }
            | NE    { std::cout &lt;&lt; &quot;expr NE expr\n&quot;; }
            | AND   { std::cout &lt;&lt; &quot;expr AND expr\n&quot;; }
            | OR    { std::cout &lt;&lt; &quot;expr OR expr\n&quot;; }
  ;
  
  %%
  
  void yyerror (const char *s)
  {
    // $$ = new OpExpr($1, $2, $3);
    //std::cout &lt;&lt; &quot;%f\n&quot;,($1+$3));
  }
  </code></pre>
  <p>And I try multiple ways of mixing my includes and compiling order. The latest way I try to compile everything is like this:</p>
  <pre><code>bison mRuby.yy
  cp -R mRuby.yy mRuby.y
  bison -d mRuby.y
  flex mRuby.l
  gcc -c lex.yy.c mRuby.tab.c  -ll -ly
  g++ lex.yy.o -c
  g++ mRuby.tab.cc -o parser
  </code></pre>
  <p>My goal is to replace the c++ print statements with class construction for building a parse tree and interpreter in c++.</p>