Giter VIP home page Giter VIP logo

plammar's Introduction

plammar

A Prolog grammar written in Prolog, for parsing and serialising Prolog code.

Installation

First, you need SWI-Prolog. See there for installation instructions.

We make use of the following packages:

All can be installed by calling the following query in SWI-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:

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:

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:

?- 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

swipl -g main cli.pl -- [options] [<filename>]

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:

./cli.exe [options] [<filename>]

plammar's People

Contributors

fnogatz avatar koo5 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

plammar's Issues

use_module/1 directives can be simplified

The following directives:

:- use_module(library(plammar/util)).
:- use_module(library(plammar/state)).
:- use_module(library(plammar/format_space)).
:- use_module(library(plammar/format_check)).

can be simplified to:

:- use_module(util).
:- use_module(state).
:- use_module(format_space).
:- use_module(format_check).

It will also improve loading times. Same for other occurrences of this directive in other files.

Expensive call to =../2

The =../2 built-in predicate is computationally expensive. In the following line, it can be replaced by a call to the functor/3 built-in predicate:

Q =.. [Kind|_],

Reported by the Logtalk linter as:

*     Suspicious call: A=..[B|C] instead of functor(A,B,D)
*       while compiling object 'plammar/pt_ast'
*       in file /Users/pmoura/Downloads/plammar-master/prolog/plammar/pt_ast.pl between lines 583-598

stuck

hi, it was:

    prolog_ast(
        file(
            'cli.pl'
        ),
        PT
    ),

I just checked again, it's stuck, probably forever.
also, how do i use the cli?

koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- --help
USAGE: plammar [options] [<file>]

Parse an input as Prolog source code
If no input was given it reads from stdin.

Options:
--help     -h     boolean=false  display this help
--version  -v,-V  boolean=false  display version
--dcg      -d     atom=_         start from this DCG body
--pretty   -p     boolean=false  pretty output
--ops             term=[]        pre-defined operators
--not-ops         term=_         disallow operators

koom@dev ~/plammar (dev7)> swipl -g main cli.pl -- -p cli.pl
ERROR: -g main: process/2: Unknown procedure: ast:prolog/3
ERROR:   However, there are definitions for:
ERROR:         ast:prolog/0
koom@dev ~/plammar (dev7) [2]> kw cli.pl&
koom@dev ~/plammar (dev7) [2]> 
(kw:29064): Gtk-WARNING **: 12:14:30.452: Theme parsing error: gtk-widgets.css:1214:18: Not using units is deprecated. Assuming 'px'.

koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- -p=true cli.pl
ERROR: -g main: Syntax error: disallowed: <shortflag>=<value>
koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl --  cli.pl
ERROR: -g main: process/2: Unknown procedure: ast:prolog/3
ERROR:   However, there are definitions for:
ERROR:         ast:prolog/0
koom@dev ~/plammar (dev7) [2]> git status
On branch dev7
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   README.md

Untracked files:
  (use "git add <file>..." to include in what will be committed)
	mytest.pl
	x

no changes added to commit (use "git add" and/or "git commit -a")
koom@dev ~/plammar (dev7)> swipl -g main cli.pl -- cli.pl
ERROR: -g main: process/2: Unknown procedure: ast:prolog/3
ERROR:   However, there are definitions for:
ERROR:         ast:prolog/0
koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- --help
USAGE: plammar [options] [<file>]

Parse an input as Prolog source code
If no input was given it reads from stdin.

Options:
--help     -h     boolean=false  display this help
--version  -v,-V  boolean=false  display version
--dcg      -d     atom=_         start from this DCG body
--pretty   -p     boolean=false  pretty output
--ops             term=[]        pre-defined operators
--not-ops         term=_         disallow operators

koom@dev ~/plammar (dev7)> swipl -g main cli.pl -- -p=true -d prolog cli.pl
ERROR: -g main: Syntax error: disallowed: <shortflag>=<value>
koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- -p=true -d=prolog cli.pl
ERROR: -g main: Syntax error: disallowed: <shortflag>=<value>
koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- -p=true -d cli.pl
ERROR: -g main: Syntax error: disallowed: <shortflag>=<value>
koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- -p=true --dcg prolog cli.pl
ERROR: -g main: Syntax error: disallowed: <shortflag>=<value>
koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- -p=true --dcg=prolog cli.pl
ERROR: -g main: Syntax error: disallowed: <shortflag>=<value>
koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- -p --dcg=prolog cli.pl
ERROR: -g main: call_dcg/3: Unknown procedure: plammar:prolog/4
ERROR:   However, there are definitions for:
ERROR:         plammar:prolog/0
koom@dev ~/plammar (dev7) [2]> swipl -g main cli.pl -- -p --dcg cli.pl
|: ^C⏎                                                                                         koom@dev ~/plammar (dev7) [SIGINT]> swipl -g main cli.pl -- -p -d cli.pl
|: ^\fish: Job 2, “swipl -g main cli.pl -- -p -d c…” terminated by signal SIGQUIT (Quit request from job control with core dump (^\))
koom@dev ~/plammar (dev7) [SIGQUIT]> swipl -g main cli.pl -- -p -d -- cli.pl
ERROR: -g main: call_dcg/3: Unknown procedure: plammar: -- / 4
koom@dev ~/plammar (dev7) [2]> 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.