very simple preprocessor using flex (vpetrucci)
2 Apr 2005 19:36:02 -0500

          From comp.compilers

Related articles
very simple preprocessor using flex (2005-04-02)
Re: very simple preprocessor using flex (2005-04-11)
| List of all articles for this month |

From: (vpetrucci)
Newsgroups: comp.compilers
Date: 2 Apr 2005 19:36:02 -0500
Keywords: interpreter, design, comment
Posted-Date: 02 Apr 2005 19:36:02 EST


im new using flex, but ive already read its manual.

im trying to write a VERY simple preprocessor. the "include" problem
is solved at flex manual ( but i got a problem at "define"
directive when i have to "replace" the token matched by the "text" on
the "define table".

i tried unput(), start conditions, replacing yytext... but its not
working. here is what ive done (kind of imcomplete), i think i need
flush input first (yytext)... wow... im confused.


"define" { BEGIN pr_define; }

        [ ]+{DEFNAME}[ ]+{DEFNAME}\n {
                char *tmp = strdup(yytext);
                char *d1, *d2;
                p_def p;

                tmp[yyleng-1] = '\0';
                d1 = strtok(tmp, " ");
                d2 = strtok(NULL, " ");
                //printf("def1: '%s'\n", d1);
                //printf("def2: '%s'\n", d2);
                do_define(d1, d2);

                BEGIN INITIAL;

        p_def p = def_find(yytext);
        char *newtext;
        int i;
        if (p) {
                printf("def1: '%s'\n", yytext);
                printf("def2: '%s'\n", p->s2);
                /* rescan replaced input */
                newtext = strdup(p->s2);
                for (i = strlen(newtext)-1; i >= 0; i--) {
                //yytext[yyleng] = '\0';
                printf("'%s'\n", yytext);
                printf("'%d'\n", yyleng);

                BEGIN bah;
        } else {

<INITIAL,bah>{ID} {
        yylval.s = (char *) strdup(yytext);
        return NAME;
<INITIAL,bah>[0-9]+ {
        yylval.i = atoi(yytext);
        return NUMBER;
<INITIAL,bah>[-()<>=+*/:] {
        return *yytext;
[Macro expansion is tricky. Either you need to build your own pushback
system, or you have to treat each macro that's being expanded like a
little nested include file. People have known how to do this since
the 1960s, but it's not written down in many places. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.