Click here to Skip to main content
15,883,606 members
Articles / Containers / Virtual Machine

Parsing Expression Grammar Support for C# 3.0 Part 1 - PEG Lib and Parser Generator

Rate me:
Please Sign up or sign in to vote.
4.95/5 (49 votes)
7 Oct 2008CPOL40 min read 202.6K   2.1K   118  
Introduction to the parsing method PEG with library and parser generator
using System;
delegate void D(int x);
class C
{
	public static void M1(int i) {
		Console.WriteLine("C.M1: " + i);
	}
	public static void M2(int i) {
		Console.WriteLine("C.M2: " + i);
	}
	public void M3(int i) {
		Console.WriteLine("C.M3: " + i);
	}
}
class Test
{
	static void Main() { 
		D cd1 = new D(C.M1);
		cd1(-1);				// call M1
		D cd2 = new D(C.M2);
		cd2(-2);				// call M2
		D cd3 = cd1 + cd2;
		cd3(10);				// call M1 then M2
		cd3 += cd1;
		cd3(20);				// call M1, M2, then M1
		C c = new C();
		D cd4 = new D(c.M3);
		cd3 += cd4;
		cd3(30);				// call M1, M2, M1, then M3
		cd3 -= cd1;			// remove last M1
		cd3(40);				// call M1, M2, then M3
		cd3 -= cd4;
		cd3(50);				// call M1 then M2
		cd3 -= cd2;
		cd3(60);				// call M1
		cd3 -= cd2;			// impossible removal is benign
		cd3(60);				// call M1
		cd3 -= cd1;			// invocation list is empty so cd3 is null
		//		cd3(70);		// System.NullReferenceException thrown
		cd3 -= cd1;			// impossible removal is benign
	}
}

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions