expr2list {lazy.symbolic}R Documentation

Display the structure of input expression as recursive list

Description

Display the structure of input expression as recursive list

Usage

expr2list(expr, type = "list", return = 0, print = 1)

Arguments

expr

expression as a string

type

= "str" for str style output, "lst" for list style output

return

= 1 to return the resulting list

print

= 0 not to print the result

Details

 expr <- easy_parse("a-2*B(c)*d")
 expr2list(expr)

returns the following:

length(input expression as list) = 3
 1  [[1]]$call
 2  -
 3  [[2]]$symbol
 4  a
 5  [[3]]
 6  [[3]][[1]]$call
 7  *
 8  [[3]][[2]]
 9  [[3]][[2]][[1]]$call
 10 *
 11 [[3]][[2]][[2]]$const
 12 2
 13 [[3]][[2]][[3]]
 14 [[3]][[2]][[3]][[1]]$call
 15 B
 16 [[3]][[2]][[3]][[2]]$symbol
 17 c
 18 [[3]][[3]]$symbol
 19 d

Above indicates:

 expr[[3]][[1]] == '*'
 expr[[3]][[2]][[2]] == 2
 expr[[3]][[2]][[3]][[1]] == "B"
 expr[[3]][[3]] == "d"

Value

a string vector

Examples

expr2list("a+2*b-C(d)*e%*%f-g/h-i")
expr2list("tr(A%*%inv(t(X1)+D))+B")
expr2list("tr(A%*%inv(t(X1)+D))+B", type="str")

# enclose the first multiplication by parens
ee <- parse(text="F( 1 + a*b )%*%H(X*Y)")[[1]]
lee <- expr2list(ee, return=1)
loc <- which( regexpr("^\\*$",lee) > 0 )[1]
locp <- loc-2
printm(loc,locp,lee[[locp]],lee[loc])
locee <- as.numeric(
           regmatches(lee[[locp]],gregexpr("[1-9]",lee[[locp]]))[[1]] )
ee[[locee]]
ee[[locee]] <- call("(",ee[[locee]])
expr2list(ee)
show_ast(ee)



[Package lazy.symbolic version 1.0.0.20250803 ]