Git Product home page Git Product logo

pegged's Introduction

Pegged

Pegged is a tool for generating Objective-C recursive-decent parsers from Parsing Expression Grammars (PEGs).

This code, along with the code it generates, is in the public domain.

SYNOPSIS

pegged [--version] [--help] [--output-dir directory] file

DESCRIPTION

Pegged generates Objective-C parsers from PEG grammars. The parsers it generates are re-entrant, thread-safe, and do not leak memory: they are suitable for inclusion in other programs.

Pegged reads the grammar specified in file and will create a class of the same name. A .h and a .m file will then be created in the output directory, which may be specified from the command line and defaults to the directory containing the PEG grammar. The parser class adheres to a simple interface:

@protocol ParserDataSource;
typedef NSObject<ParserDataSource> ParserDataSource;
@interface Parser : NSObject
{
}
@property (retain) ParserDataSource *dataSource;
- (BOOL) parse;
- (BOOL) parseString:(NSString *)string;
@end

@protocol ParserDataSource
- (NSString *) nextString;
@end

The data to be parsed may either be provided via a data source (which responds to a single selector, -nextString) or via the -parseString: selector.

OPTIONS

--version    Print version information and exit
--help        Print help and exit
--output-dir    Write generated files to the specified directory

AN EXAMPLE

Provided here is a simple example: a basic calculator.

@property (retain) Calculator *calculator;

Equation <- Sum EndOfFile

Sum <- Product ( PLUS  Product { [self.calculator add]; }
               / MINUS Product { [self.calculator subtract]; }
               )*

Product <- Terminal ( MUL Terminal { [self.calculator multiply]; }
                    / DIV Terminal { [self.calculator divide];   }
                    )*

Terminal <- OPEN Primary CLOSE
          / Number { [self.calculator pushNumber:text]; }

Number <- < [0-9+] > _

OPEN      <- '(' _
CLOSE     <- ')' _

MUL       <- '*' _
DIV       <- '/' _
PLUS      <- '+' _
MINUS     <- '-' _
_         <- ' '*
EndOfFile <- !.

The grammar begins by specifying an options and any desired Objective-C properties. These properties are added to the generated parser class, along with the requisite member variables, @class declarations, and #imports.

It continues with a series of rules, some of which actions. These actions are performed after parsing has completed. In the case of the calculator, numbers are added to the stack and basic operations are performed through the Calculator object that was added as a property.

pegged's People

Contributors

dparnell avatar goetzf avatar mdiep avatar monyschuk avatar

Watchers

 avatar

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.