very simple preprocessor using flex

viniciuspetrucci@gmail.com (vpetrucci)
2 Apr 2005 19:36:02 -0500

          From comp.compilers

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

From: viniciuspetrucci@gmail.com (vpetrucci)
Newsgroups: comp.compilers
Date: 2 Apr 2005 19:36:02 -0500
Organization: http://groups.google.com
Keywords: interpreter, design, comment
Posted-Date: 02 Apr 2005 19:36:02 EST

Hi,


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 (gnu.org). 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; }


<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);


                free(tmp);
                BEGIN INITIAL;
        }
}


{DEFNAME} {
        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--) {
                        unput(newtext[i]);
                }
                //yytext[yyleng] = '\0';
                printf("'%s'\n", yytext);
                printf("'%d'\n", yyleng);


                free(newtext);
                BEGIN bah;
        } else {
                REJECT;
        }
}


<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.