-
January 3rd, 2010, 06:10 PM
#1
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();
}
%%
-
January 4th, 2010, 11:07 AM
#2
Re: trouble with assigning values to tokens flex++ bison++
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.
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
-
January 4th, 2010, 11:26 AM
#3
Re: trouble with assigning values to tokens flex++ bison++
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.
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++...
-
January 4th, 2010, 12:34 PM
#4
Re: trouble with assigning values to tokens flex++ bison++
Originally Posted by eris_86
... 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|