3 ms·
> You simply can’t produce an AST without expanding macros first. False; the syntax before expanding macros is an AST, so is the one after. It's an AST-AST tra
by kazinator 9y ago
> You simply can’t produce an AST without expanding macros first.
False; the syntax before expanding macros is an AST, so is the one after. It's an AST-AST transformation.
If anything, the one with macros is more abstract: because it, like, has the abstractions in their original abstract form!
Also, non-Lisp languages perform AST-AST transformations; just usually not with Lisp-like macros. For instance, the AST node for a while loop in a C compiler might be replaced with a combination of if, goto and statement label nodes (with generated labels: analogous to a Lisp macro's gensyms). That's a form of expansion. The input is an AST with while nodes; the output is one without.
So, no the advantage really is that with Lisp we are reading rather than parsing. Or, alternatively, that the parsing is very simple and uniform, and that the language of the parser over-generates: it produces a large space of forms which do not have a meaning, but serve as arbitrary data or can be given a meaning with new abstractions.
In, say C, we have a syntax in which there are numerous lists: lists of declaration specifiers in a declaration, lists of parameters, lists of structure members in a struct declaration, lists of global definition, lists of statements in a statement body. These all have their own grammar productions with their own quirks. And none of them have an object model to which they correspond.
In Lisp, the analogous things are all the same list type with the same syntactic representation. It corresponds to an object, and is operated upon by the same access and construction methods.
Homoiconicity isn't the point, because that just refers to storing procedures in the form in which they were entered ("code is characters, and nothing but"). Code is structured data is code is the point, with a nice, straightforward printed representation for working with the data textually.
- derefr 9y ago> False; the syntax before expanding macros is an AST, so is the one after. It's an AST-AST transformation. I mean, yes, in the same vacuous sense that the flat stream of tokens output by a lexer technically qualifies as an AST. If you like, your lexer could output a "tree" of 1+N nodes: a Parse node, and within it, a list of N arguments (the lexer-tokens.) You would then apply an AST-AST transformation that responds to the "parse" node by parsing its contents. When we talk about an AST in the context of programming, we usually mean to refer not to any airy CS concept, but specifically to the output of an LR(k) parser—that is, a bottom-up, context-free parser. To parse the lexer-token stream in an LR(k) parser, you need to be able to output ("produce") a structure given a sequence of tokens, without having any context of the greater rule you're executing "within" (i.e. above on the call-stack) other than the fact of what the current rule is. Homoiconicity is exactly the property of a programming-language grammar that allows code containing macros to pass cleanly through this initial "lexical parsing" step. Usually, this requires a separation of the grammar from the syntax of the language, such that the "lexical parsing" grammar will no longer directly produce any of the "special forms" of the language itself, but these will rather be later handled by a similar (or the same!) process as macro-expansion—consuming AST subtrees to produce other, more specialized AST subtrees. Or, to put that another way: macros require top-down parsing. If you want to avoid using a full-on top-down parser, you can instead apply a traditional bottom-up context-free parser followed by applying a folding transformation to the generated tree to do "the rest" of the parsing. But, to achieve this parsing strategy, the property you have to imbue your language grammar with is called "homoiconicity."
- benlorenzetti 9y agoI'm a little rusty remembering my compilers course, but do you mean separating syntax from inflection? So that you can parse just based on a "syntax grammar" and then you can trivially assume the inflection based on where things fell in the AST? Can you explain more why this makes macros AST -> AST transformation only vacuously true? I didn't follow the argument. Also why macros require top down parsing? Perhaps more interesting, would the property you described still be called homoiconic if the the parser were context sensitive?
- kazinator 9y agoLisp macros (that the article is talking about) work with the same input and output language, not with two different languages/representations as is written. Macros can expand to other macros and even to themselves. It's an iterative process in which the intermediate pieces of tree have fewer and fewer macros until none are left. Other than not using macros, the target code is in the same language. (In C, the preprocessing phase also works with the same input and output language. It's not a tree data structure, but rather a sequence of preprocessor tokens. Tokens in, tokens out. Parsing is then done in the next phases of translation. Macros have to do some light parsing in order to delimit the argument lists, of course, and to identify the operators like the token pasting ##, stringification # and whatever, plus to handle the preprocessing directives and #if expressions with arithmetic.)
- kd0amg 9y agoFalse; the syntax before expanding macros is an AST, so is the one after. It's an AST-AST transformation. In context, this is about producing an AST in a "base" language without all the language extensions the actual user program uses.
- kazinator 9y agoIn context, the article says exactly this What’s this intermediate syntax tree? It’s an almost entirely superficial understanding of your program: it basically does paren-matching to create a tree representing the surface nesting structure of the text. This is nowhere near an AST, but it’s just enough for the macro expansion system to do its job.. That is completely false. If the code happens not to use any macros, they are the same. One is exactly as "superficial" as the other. Macros write code in the same language that the human user; they use other macros and even recursively themselves sometimes.
- kd0amg 9y agoWhat systems have you been using where reading involves a more than superficial understanding of the program? Normally, a reader doesn't even know how many arguments ought to be given to each function or syntactic form, nor does it know the difference between macro invocation and a function call. There is no need for the reader to know what the syntactic forms in the base or extended language are. The value of s-expressions (or any notation that explicitly encodes the tree shape) is that the reader can function without that knowledge.