Path: csiph.com!weretis.net!feeder6.news.weretis.net!feeder.usenetexpress.com!feeder1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news.iecc.com!.POSTED.news.iecc.com!nerds-end From: Hans-Peter Diettrich Newsgroups: comp.compilers Subject: Re: Parsing partial sentences Date: Fri, 7 Apr 2017 22:24:04 +0200 Organization: Compilers Central Lines: 43 Sender: news@iecc.com Approved: comp.compilers@iecc.com Message-ID: <17-04-006@comp.compilers> References: <17-04-001@comp.compilers> <17-04-002@comp.compilers> <17-04-003@comp.compilers> <17-04-004@comp.compilers> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit Injection-Info: miucha.iecc.com; posting-host="news.iecc.com:2001:470:1f07:1126:0:676f:7373:6970"; logging-data="30894"; mail-complaints-to="abuse@iecc.com" Keywords: C, parse, comment Posted-Date: 10 Apr 2017 10:26:16 EDT X-submission-address: compilers@iecc.com X-moderator-address: compilers-request@iecc.com X-FAQ-and-archives: http://compilers.iecc.com Xref: csiph.com comp.compilers:1845 Am 07.04.2017 um 18:59 schrieb George Neuner: > On Fri, 7 Apr 2017 11:16:18 -0400 (EDT), Hans-Peter Diettrich > wrote: >> The purpose is just what I wrote: parse #defines into constructs which >> can be translated into some other programming language. It shall become >> part of my C-to-Pascal converter, written in Pascal (OPL). > > Unfortunately, #define is a textual substition ... the preprocessor > doesn't care whether the body is legal C, and the compiler itself > doesn't look at the raw #define bodies. Right, but in a language translation I want to retain as much of the original code as possible. Then a Pascal compiler can find bugs which escape the C compiler. > It might be easiest to just run the C preprocessor as a 1st pass and > then attempt to convert the result. That would obfuscate the source code widely :-( > Since a #define body is just text, it can be anything - people have > created whole DSLs using #define. If you really need to figure them > out, I'm afraid you'll need (almost) the whole C language parser to do > it. I already wrote the parser, but it's LL. For code snippets a LR parser looks like the only solution? DoDi [Since preprocessor macros are text macros, there's no reason to expect a macro's expansion can be parsed at all. Consider this: # define FOO 42 + int a, b; ... a = FOO b; A notorious example of preprocessor abuse was the Bourne shell, where Steve Bourne wrote preprocessor macros to make C look like Algol 68. -John]