Path: csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: Abstract Syntax Tree for C programs Date: Thu, 23 May 2019 13:47:26 -0700 Organization: None to speak of Lines: 50 Message-ID: References: <1727e4f9-2056-4357-8572-599c46db6a88@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: reader02.eternal-september.org; posting-host="136863c131cf0a3ecbbb1d67e7494960"; logging-data="10751"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+zhVWsmEOFAktiJ7A+6Ehr" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:epggWiYog/dri2nLkj/MR7qLmVM= sha1:5cNZfIuAqILaMMG3UFMAKo+3y30= Xref: csiph.com comp.lang.c:143988 Bart writes: > On 23/05/2019 14:03, Thiago Adams wrote: >> On Thursday, May 23, 2019 at 9:58:37 AM UTC-3, Bart wrote: >>> On 23/05/2019 13:15, Bart wrote: >>>> On 23/05/2019 12:31, Thiago Adams wrote: >>>>> Does anyone has some links/ideas of good ways to represent >>>>> AST in text mode or in memory? >>> >>>> (BTW I normally don't translate type declarations to ASTs, which are >>>> used for example code. >>> >>> Executable code. That's close to the exact opposite of what he's looking for. >> Let's say you need to check if the function caller is using >> the correct arguments according with the function signature. >> In this case you need to keep the function signature somewhere >> and compare with the type of the argument. > > Then a format like XML is the last I would think of using. Remember that > a function signature in XML format is just a string. Worse, one XML > string could represent all the functions in your program. Thiago wrote: When I print in XML for instance it is very big. I think the point of the XML is that it's a textual (human-readable, more or less) *representation* of the internal data structure that makes up the AST. JSON could be another way to represent the same data structure. You wouldn't use XML as an internal format. You could use it for debugging (so you can see an unambiguous textual representation) or as an exchange and storage format, so you can share data without worrying about non-portable binary formats and pointers. The AST itself would be a bunch of allocated nodes in memory linked by pointers. For example, an AST for the expression `x + y` would consist of a node for the "+" operator with links to nodes for the `x` and `y` expressions, each of which would refer to the node for the corresponding declaration. [...] -- Keith Thompson (The_Other_Keith) kst-u@mib.org Will write code for food. void Void(void) { Void(); } /* The recursive call of the void */