views:

62

answers:

1

How would i implement #define's with yacc/bison?

I was thinking all define characters much match a regular varaible. Variables are defined as [a-zA-Z_][a-zA-Z0-9_]* so i figure i can put a check there to see if the variable is a define'd worked or not. Then replace the text with what it should be.

How can i do that? Right now i want to completely ignore the word BAD as if i defined it as #define BAD in c. Below is the code for that lex rule but i am doing it wrong. Also lex complains about "BA" being in the stream. I know the below is completely wrong and illogic so how do i ignore BAD and then how do i replace it with something like float

 if(strcmp(yytext, "BAD")==0) {
  int i, l = strlen(yytext);
  for(i=0; i<l; i++) { REJECT }
  return;
 }
 return VAR; }

i know the major steps are 1) define the define, 2) detect it in source 3) make lex forget the define characters 4) insert the new correct characters

A: 

Put a rule in lex to find the define. Then use unput to insert the replacement text. Note the text is to be inserted backwards

[a-zA-Z0-9_]* {
     if(strcmp(yytext, "HARDCODED_DEFINE")==0) {
      const char s[]="int replacement_text";
      int z;
      for(z=strlen(s)-1; z>=0; z--)
       unput(s[z]);
     }
     else
      return VAR_TOK; 
     }
acidzombie24