Re: [Flex-help] search and substitute constants in C
flex is a tool for generating scanners
Brought to you by:
wlestes
From: Arthur <asc...@at...> - 2016-02-24 23:17:24
|
Hi Roberto; The real answer is that is a parser issue, not a lexer issue. flex is a lexer. Bison (bYacc,, ...) are parsers. The lex constructs are straight forward, for example: # can be a token define can be a token TRUE can be a token 1 is an integer In the parser you would do something like: define : '#' define_token ident_token int_token { code } ; I am greatly simplifying but the lexer returns tokens, the parser recognizes a particular configuration of the tokens, in this case a define statement, and the code is code you insert to take some action. Your question is fairly vague and I have provided a fairly vague answer. The key point is that flex passes tokens to, e.g., bison and bison does all the heavy lifting. In general, the lexer does not have the power to do much, it is a regular expression processor. Parsers are normally written in BNF. You can look online for C language BNF. If this does not answer your question, ask again. If the terms are unfamiliar to you, ask about the terms. If you have never, ever done something like this before, then get a book on compiler theory and take a look at what it says. I hope that this helps (a little). art On 2/24/2016 1:37 PM, Roberto Valverde Cameselle wrote: > Hello to all, I’m new to flex and i wanted to know how to parse a C example file and when a #define statement is found, substitute all the instances of that constant with its value. For example: > > <input file> > > #define TRUE 1 > … > > … > … > .. > > a = TRUE; > … > > > <output file> > > … > .. > … > ... > > a = 1; > > > is this possible? > > Thank you in advance! > ------------------------------------------------------------------------------ > Site24x7 APM Insight: Get Deep Visibility into Application Performance > APM + Mobile APM + RUM: Monitor 3 App instances at just $35/Month > Monitor end-to-end web transactions and take corrective actions now > Troubleshoot faster and improve end-user experience. Signup Now! > http://pubads.g.doubleclick.net/gampad/clk?id=272487151&iu=/4140 |