# R parser package on CRAN

August 4, 2009
By

(This article was first published on Romain Francois, Professional R Enthusiast, and kindly contributed to R-bloggers)

The parser package has been released to CRAN, the package mainly defines a function parser that is similar to the usual R function parse, with the few following differences:

• The information about the location of each token is structured differently, in a data frame
• location is gathered for all symbols from the source code, including terminal symbols (tokens), comments
• An equal sign is identified to be either an assignment, the declaration of a formal argument or the use of an argument

Here is an example file containing R source code that we are going to parse with parser

#' a roxygen comment
f <- function( x = 3 ){

# a regular comment
rnorm(10 ) + runif( 10 )

}


It is a very simple file, for illustration purpose. Let's look what to do with it with the parser package

The parser generates a list of expressions, just like the regular parse function, but the gain is the data attribute. This is a data frame where each token of the parse tree is a line. The id column identifies each line, and the parent column identifies the parent of the current line.

At the moment, only the forthcoming highlight package uses the parser package (actually the parser package has been factored out of highlight), but some anticipated uses of the package include:

• rework the codetools package so that it tells source location of potential problems
• code coverage in RUnit or svUnit
• rework the roxygen parser

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

Tags: , , , ,