CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    2

    Unhappy trouble with assigning values to tokens flex++ bison++

    Hello everybody!

    I'm writting a HTML parser using flex++ and bison++. The trouble is that I cannot assign values to
    the tokens. I know how this works in C, but is there some other way to do this in C++?

    I would really appreciate if you could take a look at this code
    (it's just a small example of the original code).

    parser.y
    -----------------
    %name Parser
    %define MEMBERS \
    ~Parser() {} \
    private: \

    yyFlexLexer lexer;
    %define LEX_BODY {return lexer.yylex();}
    %define ERROR_BODY {}
    %header{
    #include <iostream>
    #include <fstream>
    #include <FlexLexer.h>
    #include <cstdlib>
    #include <stdio.h>

    #include <string>
    #include <string.h>
    #define YYSTYPE char *
    using namespace std;
    %}

    %union {

    const char *str;
    }

    %{
    ofstream out;
    %}
    %token <str> STRING

    %start html_start
    %%
    html_start

    : text {

    $<str>$=$<str>1;
    out<<" text2 "<<$<str>$;
    }
    |{out<<" text1 ";}

    ;

    text

    : STRING {$<str>$ = $<str>1;
    }
    ;
    %%
    int main(int argc, char ** argv )
    {
    Parser parser;
    ifstream inp;
    string myFileName;
    myFileName = "mydata.txt";
    inp.open(myFileName.c_str(), ifstream::in);

    inp.close();
    inp.clear(ios::failbit);
    out.open("mydata.txt", ofstream:ut);
    parser.yyparse();
    return 0;
    out.close();
    }


    scanner.l
    ------------------
    %option c++
    %option noyywrap

    %{
    #include<sstream>
    #include <iostream>
    #include "parser.h"
    #include <cstdlib>
    #include <fstream>
    #include <malloc.h>
    #include <string.h>
    yy_Parser_stype yyval;

    %}

    letter [_a-zA-Z]
    DIGIT [0-9]
    %%

    {letter}({letter}|{DIGIT})* {
    yyval.str=(yytext);
    return ( Parser::STRING);
    }

    <<EOF>> {
    yyterminate();
    }

    %%

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: trouble with assigning values to tokens flex++ bison++

    Quote Originally Posted by eris_86 View Post
    I'm writting a HTML parser using flex++ and bison++. The trouble is that I cannot assign values to the tokens. I know how this works in C, but is there some other way to do this in C++?
    In principal, you can compile C code as C++ code without problems.
    I would really appreciate if you could take a look at this code
    (it's just a small example of the original code).
    Please use code tags when you post code. What you've posted doesn't look like C++ code to me.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Jan 2010
    Posts
    2

    Re: trouble with assigning values to tokens flex++ bison++

    Quote Originally Posted by D_Drmmr View Post
    In principal, you can compile C code as C++ code without problems.
    I've manage to compile it but it's not working.

    Please use code tags when you post code. What you've posted doesn't look like C++ code to me.
    I will use it in the future. It is c++...

  4. #4
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: trouble with assigning values to tokens flex++ bison++

    Quote Originally Posted by eris_86 View Post
    ... It is c++...
    It looks more like lex/yacc code to me rather than C++

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured