trouble with assigning values to tokens

Una Radovanovic <[email protected]> Mon, 4 Jan 2010 00:05:52 +0100
Newsgroups gmane.comp.lex.flex.general
Message-ID <[email protected]>
--===============1766215664==
Content-Type: multipart/alternative; boundary=001485f4b246756f1e047c4aa591

--001485f4b246756f1e047c4aa591
Content-Type: text/plain; charset=ISO-8859-1

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::out);
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();
}
%%



-- 
-- 
Kindest regards,
Una

--001485f4b246756f1e047c4aa591
Content-Type: text/html; charset=ISO-8859-1


	




<pre>Hello everybody!<br><br>I&#39;m writting a HTML parser using flex++ and bison++. The trouble is that I cannot assign values to<br>the tokens. I know how this works in C, but is there some other way to do this in C++? <br>
I would really appreciate if you could take a look at this code<br>(it&#39;s just a small example of the original code).<br><br>parser.y<br>-----------------<br>%name Parser<br>%define MEMBERS \<br>~Parser() {} \<br>private: \<br>
yyFlexLexer lexer;<br>%define LEX_BODY {return lexer.yylex();}<br>%define ERROR_BODY {}<br>%header{<br>#include &lt;iostream&gt;<br>#include &lt;fstream&gt;<br>#include &lt;FlexLexer.h&gt;<br>#include &lt;cstdlib&gt;<br>#include &lt;stdio.h&gt;<br>
#include &lt;string&gt;<br>#include &lt;string.h&gt;<br>#define YYSTYPE char *<br>using namespace std;<br>%}<br><br>%union {<br><br>      const char *str;<br>}<br><br>%{<br>	ofstream out;<br>%}<br>%token &lt;str&gt; STRING<br>
%start html_start<br>%%<br>html_start<br><br>:  text  {<br>   <br>	$&lt;str&gt;$=$&lt;str&gt;1;<br>    out&lt;&lt;&quot; text2 &quot;&lt;&lt;$&lt;str&gt;$;<br>    } <br>|{out&lt;&lt;&quot; text1 &quot;;}<br><br>;<br><br>text<br>
: STRING {$&lt;str&gt;$ = $&lt;str&gt;1;<br>}<br>;<br>%%<br>int main(int argc, char ** argv	)<br>{<br>Parser parser;<br>ifstream inp;<br>string myFileName;<br>myFileName = &quot;mydata.txt&quot;;<br>inp.open(myFileName.c_str(), ifstream::in);<br>
inp.close();<br>inp.clear(ios::failbit);<br>out.open(&quot;mydata.txt&quot;, ofstream::out);<br>parser.yyparse();<br>return 0;<br>out.close();<br>}<br><br><br>scanner.l<br>------------------<br>%option c++<br>%option noyywrap<br>
%{<br>#include&lt;sstream&gt;<br>#include &lt;iostream&gt;<br>#include &quot;parser.h&quot;<br>#include &lt;cstdlib&gt;  <br>#include &lt;fstream&gt;<br>#include &lt;malloc.h&gt;<br>#include &lt;string.h&gt;<br>yy_Parser_stype yyval;<br>
%}<br><br>letter                      [_a-zA-Z]<br>DIGIT [0-9]<br>%%<br><br>{letter}({letter}|{DIGIT})*  {<br>		yyval.str=(yytext);<br>		return ( Parser::STRING);<br>}<br><br>&lt;&lt;EOF&gt;&gt; {<br>yyterminate();<br>}<br>
%%<br><br></pre><br clear="all"><br>-- <br>-- <br>Kindest regards,<br>Una<br><br>

--001485f4b246756f1e047c4aa591--



--===============1766215664==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
help-flex mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/help-flex

--===============1766215664==--