Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Spart, a parser generator framework 100% C#

, 22 Dec 2003
Spart is the C# sister of Spirit. It lets you quickly create code parsers directly in your application.
spart_bin.zip
Release
Spart.Demo.exe
Spart.dll
Spart.Tests.dll
SpartDoc.chm
spart_src.zip
SpartParser
spart.ndoc
Spart
Actions
Actors
Parsers
Composite
NonTerminal
Primitives
Testers
Scanners
Spart.csproj.user
Spart.Demo
App.ico
Spart.Demo.csproj.user
Spart.Tests
Actions
Actors
Parsers
Composite
Primitives
Scanners
Spart.Tests.csproj.user
using System;

namespace Spart.Demo
{
	using Spart.Parsers.NonTerminal;
	using Spart.Parsers;
	using Spart.Scanners;
	using Spart.Actions;
	using Spart.Debug;

	public class Calculator
	{
		Rule group;
		Rule term;
		Rule factor;
		Rule expression;
		Rule integer;
		Debugger debug;

		/// <summary>
		/// A very simple calculator parser
		/// </summary>
		public Calculator()
		{
			// creating rules and assigning names (for debugging)
			group = new Rule(); group.ID = "group"; 
			term = new Rule(); term.ID ="term";
			factor = new Rule(); factor.ID="factor";
			expression = new Rule(); expression.ID="expression";
			integer  = new Rule(); integer.ID="integer";

			// debuggger
			debug = new Debugger(Console.Out);
			debug += factor;
			debug+=term;
			debug+=group;
			debug += expression;
			debug += integer;

			// creating sub parsers
			Parser add = Ops.Seq('+',term);
			// attaching semantic action
			add.Act += new ActionHandler(this.Add);
			Parser sub = Ops.Seq('-',term);
			sub.Act += new ActionHandler(this.Sub);
			Parser mult = Ops.Seq('*',factor);
			mult.Act += new ActionHandler(this.Mult);
			Parser div = Ops.Seq('/',factor);
			div.Act += new ActionHandler(this.Div);

			// assigning parsers to rules
			integer.Parser = Prims.Digit;

			group.Parser       = Ops.Seq('(',Ops.Seq(expression,')'));
			factor.Parser      = group | integer;
			term.Parser        = Ops.Seq( factor, Ops.Klenee(mult	| div ));
			expression.Parser  = Ops.Seq(term,Ops.Klenee(add | mult ));		
		}

		/// <summary>
		/// Parse a string and return parse match
		/// </summary>
		/// <param name="s"></param>
		/// <returns></returns>
		public ParserMatch Parse(String s)
		{	
			StringScanner sc = new StringScanner(s);
			return expression.Parse(sc);
		}

		#region Semantic Actions
		public void Add(Object sender, ActionEventArgs args)
		{
			Console.Out.WriteLine("add");
		}


		public void Sub(Object sender, ActionEventArgs args)
		{
			Console.Out.WriteLine("sub");
		}


		public void Mult(Object sender, ActionEventArgs args)
		{
			Console.Out.WriteLine("mult");
		}

		public void Div(Object sender, ActionEventArgs args)
		{
			Console.Out.WriteLine("div");
		}
		#endregion
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Jonathan de Halleux
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

| Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 23 Dec 2003
Article Copyright 2003 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid