help-bison
[Top][All Lists]
Advanced

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

Re: Cookie replacement function


From: Tim Van Holder
Subject: Re: Cookie replacement function
Date: Tue, 08 Nov 2005 17:21:41 +0100
User-agent: Mozilla Thunderbird 1.0.6 (Windows/20050716)

Jonathan Selander wrote:
> 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;

This last rule always matches the entire line (lex/flex chooses the
longest possible match, and with equal lengths it chooses the topmost
rule).
So this scanner will almost always return a series of STRING tokens
(WORD will only match if it also matches the entire line).  You also
have nothing that matches %/{/} so those tokens never reach bison.

You want something like:

[[:alnum:]]+            yylval = yytext; return WORD;
[%{:}]                  yylval = yytext; return yytext[0];
[\n]                    yylval = yytext; return EOL;
.                       yylval = yytext; return CHARACTER;

With a bison grammar more like:

input
: /* empty */
| input line
;

line
: /* empty */ EOL
| nothing_special '%' '{' WORD maybe_extra_word '}' line
;

nothing_special
: /* empty */
| nothing_special not_a_newline_or_cookie_start
;

not_a_newline_or_cookie_start
: CHARACTER
| WORD
| WHITESPACE
| '{'
| '}'
| ':'
;

maybe_extra_word
: /* empty */
| ':' WORD
;

Do note that setting yylval to yytext is not a good idea when using
bison; by the time you get to the parser action, the buffer that
yytext pointed to may very well contain something completely different.





reply via email to

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