https://github.com/fnogatz/plammar
A Prolog grammar written in Prolog, for parsing and serialising Prolog code.
https://github.com/fnogatz/plammar
definite-clause-grammar parser prolog serializer swi-prolog
Last synced: about 4 hours ago
JSON representation
A Prolog grammar written in Prolog, for parsing and serialising Prolog code.
- Host: GitHub
- URL: https://github.com/fnogatz/plammar
- Owner: fnogatz
- License: mit
- Created: 2019-05-14T19:25:05.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2021-08-16T08:13:16.000Z (over 4 years ago)
- Last Synced: 2025-08-16T13:53:36.959Z (6 months ago)
- Topics: definite-clause-grammar, parser, prolog, serializer, swi-prolog
- Language: Prolog
- Size: 263 KB
- Stars: 15
- Watchers: 4
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# plammar
A Prolog grammar written in Prolog, for parsing and serialising Prolog code.
## Installation
First, you need [SWI-Prolog](http://www.swi-prolog.org/). See there for installation instructions.
We make use of the following packages:
- [`library(tap)`](https://github.com/fnogatz/tap)
- [`library(dcg4pt)`](https://github.com/fnogatz/dcg4pt)
- [`library(cli_table)`](https://github.com/fnogatz/cli_table)
All can be installed by calling the following query in SWI-Prolog:
```prolog
?- pack_install(tap), pack_install(dcg4pt), pack_install(cli_table).
```
### Development Version
To get the latest development version, clone it via git and link it to the package directory of SWI-Prolog:
```sh
git clone https://github.com/fnogatz/plammar.git
ln -s $PWD/plammar $(swipl -q -g "absolute_file_name(pack(.),D,[file_type(directory)]), write(D), halt")
```
### Pre-Compilation
It is possible to create a pre-compiled file which increases the tool's performance significantly. The command line interface is compiled using swipl's [`-c` option](https://www.swi-prolog.org/pldoc/man?section=compilation):
```sh
swipl -g main -o cli.exe -c cli.pl
```
The `.exe` suffix is chosen for compatibility with Windows systems. You can also use `make cli` to generate the pre-compiled CLI.
## Usage with SWI-Prolog
First, load the package:
```prolog
?- use_module(library(plammar)).
```
Examples:
```
?- prolog_tokens(string("a(1)."), Tokens).
?- prolog_parsetree(string("a(1)."), PT).
?- prolog_ast(string("a(1)."), AST).
```
All three predicates can also take an additional `Options` list. The first argument accepts several data formats, including `string(_)`, `file(_)`, `stream(_)`, `chars(_)` and `tokens(_)`. The predicates can be used to parse and serialise Prolog source code.
For more examples, have a look at the `/test` directory.
## Usage as CLI
plammar comes with a command line interface to parse given source code. You can directly execute it via
```sh
swipl -g main cli.pl -- [options] []
```
Call with `--help` instead of the filenames to get more options. The CLI accepts a filename as the first argument. If called without this filename, the source is read from stdin.
After the pre-compilation step mentioned before, the created executable can be called via:
```sh
./cli.exe [options] []
```