Re: [Flex-help] input() function with preserver yytext at end
flex is a tool for generating scanners
Brought to you by:
wlestes
From: Marcel L. <ma...@la...> - 2009-09-27 15:38:46
|
C-style strings are null (\0) terminated. If you have a string that's like: const char *str = "foo"; Internally it actually looks something like this: const char foo[4] = {'f', 'o', 'o', '\0'}; The '\0' character lets whatever function you're using know that you've reached the end of the string. Flex, in order to run much quicker, will use this to its advantage. Consider the following code: char *foo = "foo bar"; // don't assign static memory to non-const; only for sake of example char *bar = foo + 4; The string `foo` right now is "foo bar", and `bar` is "bar". What's interesting is that only one memory allocation happened here. What you can then do is this: char hold_char = foo[3]; char foo[3] = 0; And now `foo` is "foo", and `bar` is "bar". `hold_char` here is " ". If you assign `hold_char` back to `foo[3]` then `foo` will become "foo" again. Flex uses this to give your scanner a value for yytext (which is just a substring of the whole buffer) without having to strdup it. hth On Sun, 27 Sep 2009 03:09:57 -0300, "Fabio Y." <fya...@gm...> wrote: > Hi, > > This is my first time on this group. I'm sorry if the subject wasn't so > clear, but I'll try to explain... > > Currently I'm doing a compiler course, and guided by my personal interests > I'm currently reading a little about the flex code. Actually, I'm reading a > lot about the scan.c code. > > One part that I don't get is the instruction at end of input() function, > which do this follow command: > > c = *(unsigned char *) (yy_c_buf_p); /* cast for 8-bit char's */ >> > **(yy_c_buf_p) = '\0'; /* preserve yytext */* >> > (yy_hold_char) = *++(yy_c_buf_p); >> > > I didn't get why it is setting to a '\0' char. > > I'm not a experienced C programmer. I know some concepts but it's not the > kind of language that I deal normally. > > Thanks for any help. |