help-bison
[Top][All Lists]
Advanced

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

%token_table and stuff


From: Michael (Micksa) Slade
Subject: %token_table and stuff
Date: Thu, 14 Nov 2002 23:03:03 +1100
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.0) Gecko/20020623 Debian/1.0.0-0.woody.1

This is more of a bug report than anything but hey

I managed to get bison and flex to cooperate so I could put tokens in the grammar in the style of '"foo"' without having to define macros for token numbers and all that. The extra code required to do this isn't documented, so I thought I'd show what I did:

int token_value(char * token_buffer) {
  int i;
  int token_len = strlen(token_buffer);

  for (i = 0; i < YYNTOKENS; i++) {
     if (yytname[i] != 0
     && yytname[i][0] == '"'
    && strncmp (yytname[i] + 1, token_buffer,token_len) == 0
        && yytname[i][token_len + 1] == '"'
        && yytname[i][token_len + 2] == 0)
   break;
  }
  if(i == YYNTOKENS) return 2;
  return yytoknum[i];
}

Then in the lexer, I just went:

{WORD} {
  return token_value(yytext);
}

(With the prototype for token_value up the top as well)

Hope this helps,

Mick.






reply via email to

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