---
name: "sglr-api"
section: "3"
platform: "linux"
package: "xt-sdf2"
version: "2.0.1-3"
release: "Debian 3.1 (Sarge)"
platforms: ["linux"]
url: "https://new.carta.tech/sglr-api"
---

# sglr-api(3)

**SGinitParser**, **SGopenLanguageFromTerm**, **SGopenLanguage**, **SGcloseLanguage**, **SGreOpenLanguage**, **SGparseString**, **SGparseStringAsAsFix2**, **SGparseStringAsAsFix1**, **SGparseFile**, **SGtermToFile**, **SGparseFileUsingTable**, **SGisParseTree**, **SGisParseError** - an interface to the Scannerless Generalized LR parser SGLR library (libsglr, -lsglr)\
 **Definitions**\
\
 **API Functions**\
\
\
\
\
\
\
\
\
\
\
\
\
\
\
 **Operating Mode Macros**\
\
\
\
\
\
\
\
\
\
\
\
\
 The **SGLR** API offers high-level support for parsing functionality. A few key concepts: in order to parse an input term, a parse table for the language in which the term is supposed to be written must be present.  A database of parse tables is maintained for that purpose. In general, the user first makes sure the appropriate parse table for some language L is opened; after that, terms can be parsed over that language L. As a result, either a parse error or a parse forest will be returned. Parse forests can be written to a file. The functions in the API that return an `ATerm` will return `NULL` on an operational error; bear in mind that a successful parse that stumbles upon a syntax error in the input term will produce an error term rather than `NULL`.  Functions for checking parse results are therefore also provided. Throughout the interface, a  `errpre` argument specifies a prefix that is used in contexts where an error message is returned to the user.  It is typically instantiated with the toolname, making it possible to identify the source of any occurring error message in a possibly complex toolchain.\
 **API Functions**\
\
 If a parse table of the indicated name is already present, the existing table will be used.  The **SGreOpenlanguage**, used identically to **SGopenlanguage**, replaces eventually existing parse tables by the table contained in the named file.\
\
\
 For **SGparseFile**, the term contained in named input file `infil` is parsed over the parse table as previously opened and registered by the name `lang`.

**SGparseFileUsingTable** uses a parse table contained in `tblfil` to parse the term contained in the named file `infil`, and writes output to the named file `outfil`.\
\
\
 **Operating Mode Macros** A number of macros influence the parsing process.  The following naming convention is used:  use `SG_XXX` as a variable to check whether option `XXX` is enabled; use `SG_XXX_ON()` to enable and `SG_XXX_OFF()` to enable or disable option XXX, respectively.\
\
\
\
\
\
\
 These specific toggles are supposed to be used for debugging purposes only. So, do not use these options to tweak your language implementations! It is highly probable that there will be no backward compatibility for them in  future versions.

```
  #include <sglr.h>
```

```
  #define ME "MyParser"
  #define L  "MyLanguageReferenceName"
```

```
  int main(int argc, char **argv)
  {
    ATerm bottomOfStack;
    ATerm forest;
```

```
    if(argc != 4) exit(1);
```

```
    ATinit(0, NULL, &bottomOfStack);
```

```
    SGinitParser(ATfalse);
```

```
    if(!SGopenLanguage(ME, L, argv[1])) {
      fprintf(stderr, "error opening language %s (in %s)\n", L, argv[1]);
      exit(2);
    }
    if(!(forest = SGparseFile(ME, L, NULL, argv[2]))) {
      fprintf(stderr, "error parsing %s (over %s)\n", argv[2], L);
      exit(3);
    }
```

```
    fprintf(stdout, "Parse successful, writing %s\n", argv[3]);
    SG_BINARY_OFF();  /*  Make output to `-' (stdout) safe  */
    SGtermToFile(ME, forest, argv[3]);
    return 0;
  }
```

the *sglr* manpage, the *tree2dot* manpage None. Jeroen Scheerder \<js@cwi.nl\>
