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::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();
}
%%
Re: trouble with assigning values to tokens flex++ bison++
Quote:
Originally Posted by
eris_86
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.
Quote:
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.
Re: trouble with assigning values to tokens flex++ bison++
Quote:
Originally Posted by
D_Drmmr
In principal, you can compile C code as C++ code without problems.
I've manage to compile it but it's not working.
Quote:
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++...
Re: trouble with assigning values to tokens flex++ bison++
Quote:
Originally Posted by
eris_86
... It is c++...
It looks more like lex/yacc code to me rather than C++