Reading this and that gives me some ideas about the differences between a parse tree and an abstract syntax tree.

However, I'm still a bit puzzled about what this means concretely in R and the outputs from pryr::ast, quote and substitute.

Consider the following example:

pryr::ast() returns an abstract syntax tree (AST) (according to the docs: ?pryr::ast):

pryr::ast(z <- x + y)
\- ()
  \- `<-
  \- `z
  \- ()
    \- `+
    \- `x
    \- `y

When calling as.list(quote() on the same expression I get:

as.list(quote(z <- x + y))
[[1]]
`<-`

[[2]]
z

[[3]]
x + y

Is this output also considered to be an AST? Or is this too vague to be an AST? Or is it a parse tree?


On the other side susbtitute does return a parse tree (according to the docs: ?substitute):

as.list(substitute(z <- x + y, list(x = 1)))
[[1]]
`<-`

[[2]]
z

[[3]]
1 + y

..which is apart from the substituted x the same as the output from quote..

So is this a parse tree? And is the difference between the parse tree and the AST only the substituted x or something else?

Even worse, when considering the example from the accepted answer here I think the parse tree should be more precise than the AST, but when looking at the output from pryr::ast and quote I see quite the opposite, meaning the output from pryr::ast is more precise than the one from quote..

Thanks in advance for the enlightment..

0 Answers