help-flex
[Top][All Lists]
Advanced

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

Re: simple question


From: Tim Van Holder
Subject: Re: simple question
Date: Tue, 17 Jan 2006 08:16:46 +0100
User-agent: Mozilla Thunderbird 1.0.6 (Windows/20050716)

Mili Sandokan wrote:
> Hi everyone
> 
> I'm trying to make a parser (not a compiler) for a language, and I´m
> learning to use flex and bison.... but in this moment I just don´t
> know what to do:
> 
> I know that I can define the token "Keyword" , but .......is it
> useful? I mean, when I make the rules in Bison I need to know which
> keyword is it, and it'll be all the time....(is not the same IF than
> RETURN or END). I's supposed that flex is the one who compares the
> words,,,,,,
> 
> the other option I though was defining each keyword in the flex file
> but that is a lot of work to do and a lot of tokens to recognize.....
> I've read many manuals but I really don't know what to do...
> 
> I hope my question is well-explained and thanks for your help
> 
> : )

Well, there are two typical approaches.  Either you indeed list every
keyword in the flex file; something like:

"IF"    return t_IF;
"THEN"  return t_THEN;
"ELSE"  return t_ELSE;
"END"   return t_END;
...

(possibly setting yylval to some value if the parser needs more info).

An alternate approach is to have a function that looks up a word in
some keyword table and returns info about it (the token for it at
least).  That makes the flex file much simpler, because it can typically
do this in the general "identifier" rule

[A-Za-z][A-Za-z0-9_]* { /* identifier */
keyword_info_t* kwi = lookup_keyword(yytext);
  if (kwi == NULL)
    return t_IDENTIFIER;
  else
    return kwi->token;
}

This latter option has the added bonus of being able to decide which
keywords to recognize at runtime, by modifying the table that
lookup_keyword() uses.





reply via email to

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