Skip to content

Instantly share code, notes, and snippets.

@mlcollard
Last active February 4, 2020 12:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mlcollard/25f36564328fef007a3341914c1d8892 to your computer and use it in GitHub Desktop.
Save mlcollard/25f36564328fef007a3341914c1d8892 to your computer and use it in GitHub Desktop.

Installing and Running srcML for srcFacts

To download srcML, go to srcML.org and download the installer for your platform. For example, on Ubuntu (and this includes WSL in Windows):

  1. Download the debian package. Note: curl may not be installed
curl -L -O http://131.123.42.38/lmcrs/v1.0.0/srcml_1.0.0-1_ubuntu18.04.deb
  1. Install srcml
apt-get install ./srcml_1.0.0-1_ubuntu18.04.deb
  1. Verify install
srcml --version

with the output:

srcml 1.0.0
libsrcml 1.0.0
libarchive 3.4.0

Directly Creating and Running An Example

  • Directly create srcML for a statement:
srcml --text='\nstr = "Hello World!";\n' --language C++ --archive -o str.xml

To view the srcML:

cat str.xml

with the output (may have to scroll right):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<unit xmlns="http://www.srcML.org/srcML/src" revision="1.0.0">

<unit revision="1.0.0" language="C++" hash="ffa860933a5dca89d01a331e62b47431bb3ed325">
<expr_stmt><expr><name>str</name> <operator>=</operator> <literal type="string">"Hello World!"</literal></expr>;</expr_stmt>
</unit>

</unit>

About this example:

  • Escaped newlines in the text, i.e., \n, are converted to newlines in the srcML file.
  • The option --archive, necessary for a single input file or direct text, creates a srcML archive. The srcFacts program is designed to run on srcML archives
  • The option --language is required for direct text input so srcML knows the programming language of the code. srcML supports C++, C, Java, and C#

To run your program with a srcML file you created

./srcFacts < str.xml

To run your program directly from an example

srcml --text='\nstr = "Hello World!";\n' --language C++ --archive | ./srcFacts

Creating srcML from Files

  • Create a srcML file from an individual source code file:
srcml srcFacts.cpp --archive -o srcFacts.cpp.xml
  • Create srcML file for a project in a directory:
srcml . -o srcFacts.cpp.xml
  • Convert srcML back to source code (for a single file)
srcml srcFacts.cpp.xml -o srcFacts2.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment