Angel \”Java\” Lopez on Blog

October 18, 2013

GrammGen in C# (1) First Concepts

Filed under: C Sharp, GrammGen, Open Source Projects — ajlopez @ 4:30 pm

Next Post

I worked a lot writing lexers and parsers, see:

https://github.com/ajlopez/AjSharp
https://github.com/ajlopez/AjTalk
https://github.com/ajlopez/AjTalkJs
https://github.com/ajlopez/Mass
https://github.com/ajlopez/AjLispJava

and more. Two years ago, I met Ian Piumarta en la Smalltalks 2011, at Universidad de Quilmes (see AjSoda). He suggested I could work  on an implementation of a PEG:

http://en.wikipedia.org/wiki/Parsing_expression_grammar

Past year I tried to write something in JavaScript, but this year I put my effort on writing two implementations. I’m not sure they are PEGs, but they are close. Inm JavaScript, I have:

https://github.com/ajlopez/SimpleGrammar

I’m doing “dog fooding” using it at:

https://github.com/ajlopez/PageJs

with good results. But today, I would like to present my C# implementation:

https://github.com/ajlopez/GrammGen

It’s a solution built using TDD workflow. It has a class library and tests:

The idea is to have a parser with a list of rules. Each rule recognize a series of characters and patterns:

var rule = Rule.Get("0-9").OneOrMore().Generate("Integer");

The above rule recognize a series of digits, producing a non-terminal element with name “Integer” (the non-terminal are named with first letter in upper case). There is a fluent interface, and the .Generate method groups the result  (a character string) under a named element “Integer”.

The .Generate method could associate a custom object to “Integer” element:

Rule.Get("0-9").OneOrMore().Generate("Integer", 
    x => int.Parse((string)x));

In this case, it associates the conversion of the collected string to native integer.

You can write a list of rules:

var rules = new Rule[] {
    // ...
    Rule.Get("Term", Rule.Or('*', '/'), "Factor").Generate("Term", MakeBinaryOperatorExpresion),
    Rule.Get("Factor").Generate("Term"),
    Rule.Get("Integer").Generate("Factor"),
    // ...
}

and then, with you can create a Parser object:

var parser = new Parser("1+2", rules);

Then, you can get a named element:

var element = parser.Parse("Expression");
var expression = (IExpression)element.Value;

Our code should generate an element of IExpression. That is our interface: it’s not part of GrammarGen. Each element returned by the library can have an associated custom element, attached in .Generate methods. See the test code, and the calculator sample (ie., it has left recusrion).

Now I have a console sample:

It parse and evaluate an string with an arithmetic expression:

I could use GrammGen for more ambitious projects, ie. building an AST (Abstract Syntax Tree) for a programming language.

Keek tuned!

Angel “Java” Lopez

http://www.ajlopez.com

http://twitter.com/ajlopez

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: