Click here to Skip to main content
15,885,985 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.9K   2.1K   118  
Introduction to the parsing method PEG with library and parser generator
/* created on 25.09.2008 15:13:26 from peg generator V1.0 using 'C_KernighanRitchie2_peg.txt' as input*/

using Peg.Base;
using System;
using System.IO;
using System.Text;
namespace IntSum
{
      
      enum EIntSum{Sum= 1, S= 2};
      class IntSum : PegCharParser 
      {
        
         #region Input Properties
        public static EncodingClass encodingClass = EncodingClass.ascii;
        public static UnicodeDetection unicodeDetection = UnicodeDetection.notApplicable;
        #endregion Input Properties
        #region Constructors
        public IntSum()
            : base()
        {
            
        }
        public IntSum(string src,TextWriter FerrOut)
			: base(src,FerrOut)
        {
            
        }
        #endregion Constructors
        #region Overrides
        public override string GetRuleNameFromId(int id)
        {
            try
            {
                   EIntSum ruleEnum = (EIntSum)id;
                    string s= ruleEnum.ToString();
                    int val;
                    if( int.TryParse(s,out val) ){
                        return base.GetRuleNameFromId(id);
                    }else{
                        return s;
                    }
            }
            catch (Exception)
            {
                return base.GetRuleNameFromId(id);
            }
        }
        public override void GetProperties(out EncodingClass encoding, out UnicodeDetection detection)
        {
            encoding = encodingClass;
            detection = unicodeDetection;
        } 
        #endregion Overrides
		#region Grammar Rules
        public bool Sum()    /*Sum:    S [0-9]+  ([+-] S [0-9]+)* S 	;*/
        {

           return And(()=>  
                     S()
                  && PlusRepeat(()=> In('0','9') )
                  && OptRepeat(()=>    
                      And(()=>      
                               OneOf("+-")
                            && S()
                            && PlusRepeat(()=> In('0','9') ) ) )
                  && S() );
		}
        public bool S()    /*S:	 [ \n\r\t\v]*			;*/
        {

           return OptRepeat(()=> OneOf(" \n\r\t\v") );
		}
		#endregion Grammar Rules
   }
}

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