SAS Parser

This is a parser for the SAS language.

It is intended to be used mainly as a JVM library. It can also be used as a command line tool.

The parser works in three different stages:

  • First a list of tokens are created from some input stream (typically a File)
  • Then a parse-tree is obtained by grouping the tokens and recognizing structures
  • Finally an Abstract Syntax Tree (AST) is derived from the parse-tree. While the AST resembles closely the parse-tree it contains re-arrangements and simplifications that makes easier working with it.

The AST is built using the Kolasu library. This open-source library has been built by Strumenta. This tutorial on Kolasu explains how the library can be used: Building advanced parsers using Kolasu.

The AST could be used to perform analysis on the code or transformations of the code.

An example of transformations that can be created is transpilation.

Contents of the package

Full JAR file with all dependencies.

How to use the parser

You can use the parser as a library (i.e., from code) or as a standalone program, from the command line.

How to use from the command line

You can use the full JAR from the commandline. Executing the program without commands will display the help information.

java -jar sas-parser-with-dependencies-<version>-all.jar

The main commands are parse and jsonast. The first one will parse an input file and output the AST on stdout. The second one will parse an input file (or directory) and output JSON files.

How to generate parsing results on stdout

The parse command requires to provide the path to a valid license with the --license option. The argument is the input file.

java -jar sas-parser-with-dependencies-<version>-all.jar parse --license=strumenta.license examples.sas 

How to generate parsing results as JSON

The jsonast command requires to provide the path to a valid license with the --license option. The first argument is the input (file or directory). The second argument is the output directory where to save the JSON files.

java -jar sas-parser-with-dependencies-<version>-all.jar jsonAst --license=strumenta.license examples.sas .\ 

How to download an updated license

You can use downloadLicense command to download an updated license from the license service. The license will expire around 31 days from the moment of execution. The exact date will vary because of timezone discrepancies between the server and the local timezone. You need to provide a username and password and the path where to save the license.

java -jar sas-parser-with-dependencies-<version>-all.jar downloadLicense username password sesameholder.license 

If you are successful in downloading the license, you will see something similar to this output.

License successfully downloaded at sesameholder.license
StartDate: Mon Nov 04 01:00:00 CET 2024
EndDate: Sat Dec 07 01:00:00 CET 2024

How to use as a library

You can use this parser as any other library, directly from your code.

Using a license

This parser requires a license to be used. You can use the static method registerLicense to register the license by providing the license file as argument. You need to register the license once in the program.

import com.strumenta.sas.parser.SASLanguage;
[..]
SASLanguage.Companion.registerLicense(new File("data/example.license"));

You can then instantiate the parser to parse your SAS code (either as a string or as a file).

SASLanguage sasLanguage = new SASLanguage();
sasLanguage.parse(input);

Download a license

You can also download an updated license using the method download license in the LicenseValidation class.

LicenseValidation lv = new LicenseValidation();
lv.downloadLicense("user", "pass", "location.license");