Skip to content

Simple C++ library to parse lisp style symbolic expressions to C++ stl based data structures

Notifications You must be signed in to change notification settings

ajuc/c--_lisp_sexp_parser

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This library is intended as simple replacement for XML parsing libraries for those, that prefer lisp sexp syntax over XML.

To use it you only have to include these 2 files in your project: lispparser.h and lispparser.cpp, and to add #include "lispparser.h" in your code.
Code only depends on STL, so no external libraries are needed.

Example of usage is in main.cpp.

Library is contained in one namespace AjucLispParser. The one function that is intended for end-user is

const Expression* parse(istringstream& input);

This function tries to read from input stream input the first form and parse it to Expression.

Expression is abstract base class - its sub-classes are Float, String, Atom, Identifier and List. List is a Expression that can contain other Expressions - it can be arbitrarily deep nested.

Examples of inputs and resulting Expression trees:

1                     Float   - value == 1.0f
1.0                   Float   - value == 1.0f
"lorem ipsum"         String  - value == "lorem ipsum"
:some-name            Atom    - name is :some-name
some-name             Identifier - name is some-name
()                    List - empty list
(1 2 "lorem-ipsum")   List - has 3 elements - first and second are Floats, the last is String
( () (1 2) ((1)))     List containing: Empty list, List with 2 Floats, and List with one list inside, containing Float.

Every Expression subclass has method toString() which returns string representation of given expression, that can be read back by parse function.

Spaces, Tabs, Line breaks (Enters) and commas (,) are considered whitespace, so (1 2 3) is equivalent to (1 2,,,, 3,,).

There are 3 types of parentheses: (), [], {}. They can be used interchangeably, but if kinds of parentheses doesn't match, parse will report error.

So these forms are equivalent:
[ [1 2 3] [4 5 6] ]
{ (1 2 3) [4 5 6] }
( (1 2 3) {4 5 6} )
( (1 2 3) (4 5 6) )
etc.

But this won't parse:
( [1 2 3) {4 5 6} ]


Parse returns shared_ptr to Expression, possibly containing other expressions, each through shared_ptr. There can be no cycles inside (this arises naturally from the way parser works).

If input stream contains More than one top-level form, the first invocation of parse will return the first form, and the second invocation of parse on the same stream will return second form, and so on.

Hope it will be useful for somebody. If so - would be nice if you let me know :)


Copyright 2010-2016 Sebastian Pidek. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of
conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list
of conditions and the following disclaimer in the documentation and/or other materials
provided with the distribution.

THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

The views and conclusions contained in the software and documentation are those of the
authors and should not be interpreted as representing official policies, either expressed
or implied, of Sebastian Pidek.

About

Simple C++ library to parse lisp style symbolic expressions to C++ stl based data structures

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published