help-flex
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

trouble with assigning values to tokens


From: Una Radovanovic
Subject: trouble with assigning values to tokens
Date: Mon, 4 Jan 2010 00:05:52 +0100

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]