help-bison
[Top][All Lists]
Advanced

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

Cookie replacement function


From: Jonathan Selander
Subject: Cookie replacement function
Date: Tue, 08 Nov 2005 15:44:44 +0100
User-agent: Mozilla Thunderbird 1.0.2 (Windows/20050317)

Hi, I'm trying to do a very trivial thing using flex/bison. I want to make a sort of cookie replacement function, that finds a pattern in a string and replaces it with something else.

The string to look for is in the format "%{text}" and later on perhaps "%{text:moretext}" ... I know this should be a very simple thing to do, but my brain doesn't seem to think the same. I've been reading some howtos and so on but I'm still not sure enough to know exactly what to do. My flex file looks like this:

------------------ main.lex ------------------

%{

#include "y.tab.h"

%}

%%

[[:alnum:]]+            yylval = yytext; return WORD;
^.*$                    yylval = yytext; return STRING;

----------------------------------------------

And the bison file like this:

------------------ main.y --------------------

%{

#include <stdio.h>

#define YYSTYPE char *

int yyparse(void);
int yylex(void);
int yyerror(const char *);

%}

%token STRING COOKIE WORD

%%

basic:
       | basic cookie
       ;

cookie: STRING
       |
       '%' '{' WORD '}'
       {
               printf("COOKIE: %s\n", $3);
       }
       ;

%%

int yyerror(const char *err)
{
       fprintf(stderr, "ERROR: %s\n", err);
       return 0;
}

int main()
{
       yyparse();
       return 0;
}

----------------------------------------------

The program is compiled with

bison -y -d main.y
flex main.lex
cc -W -Wall -O2 -g lex.yy.c y.tab.c  -o main -lfl

Obviously I want it to print "COOKIE: <name of found cookie>" when something simple as %{cookie} is typed. A simple hint on where I'm thinking wrong would be more than helpful. I suppose it's something very easy.

Regards,
Jonathan




reply via email to

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