Click here to Skip to main content
15,891,136 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 203.9K   2.1K   118  
Introduction to the parsing method PEG with library and parser generator
/* created on 21.09.2008 16:11:37 from peg generator V1.0*/

using Peg.Base;
using System;
using System.IO;
using System.Text;
namespace calc0_tree
{
      
      enum Ecalc0_tree{Calc= 1, Assign= 2, Sum= 3, Prod= 4, Value= 5, Call= 6, Number= 7, 
                        ident= 8, S= 9};
      class calc0_tree : PegCharParser 
      {
        
         #region Input Properties
        public static EncodingClass encodingClass = EncodingClass.ascii;
        public static UnicodeDetection unicodeDetection = UnicodeDetection.notApplicable;
        #endregion Input Properties
        #region Constructors
        public calc0_tree()
            : base()
        {
            
        }
        public calc0_tree(string src,TextWriter FerrOut)
			: base(src,FerrOut)
        {
            
        }
        #endregion Constructors
        #region Overrides
        public override string GetRuleNameFromId(int id)
        {
            try
            {
                   Ecalc0_tree ruleEnum = (Ecalc0_tree)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 Calc()    /*[1]^^Calc:  ((^'print' / Assign / Sum) 
            ([\r\n]/!./FATAL<"end of line expected">)
            [ \r\n\t\v]* )+ 
            (!./FATAL<"not recognized">);*/
        {

           return TreeNT((int)Ecalc0_tree.Calc,()=>
                And(()=>  
                     PlusRepeat(()=>    
                      And(()=>      
                               (        
                                       TreeChars(()=> Char('p','r','i','n','t') )
                                    || Assign()
                                    || Sum())
                            && (        
                                       OneOf("\r\n")
                                    || Not(()=> Any() )
                                    || Fatal("end of line expected"))
                            && OptRepeat(()=> OneOf(" \r\n\t\v") ) ) )
                  && (    Not(()=> Any() ) || Fatal("not recognized")) ) );
		}
        public bool Assign()    /*[2]^Assign:S ident S '=' S Sum;*/
        {

           return TreeAST((int)Ecalc0_tree.Assign,()=>
                And(()=>  
                     S()
                  && ident()
                  && S()
                  && Char('=')
                  && S()
                  && Sum() ) );
		}
        public bool Sum()    /*[3]^Sum:   Prod  (^[+-] S @Prod)*;*/
        {

           return TreeAST((int)Ecalc0_tree.Sum,()=>
                And(()=>  
                     Prod()
                  && OptRepeat(()=>    
                      And(()=>      
                               TreeChars(()=> OneOf("+-") )
                            && S()
                            && (    Prod() || Fatal("<<Prod>> expected")) ) ) ) );
		}
        public bool Prod()    /*[4]^Prod:  Value (^[* /] S @Value)*;*/
        {

           return TreeAST((int)Ecalc0_tree.Prod,()=>
                And(()=>  
                     Value()
                  && OptRepeat(()=>    
                      And(()=>      
                               TreeChars(()=> OneOf("*/") )
                            && S()
                            && (    Value() || Fatal("<<Value>> expected")) ) ) ) );
		}
        public bool Value()    /*[5] Value: (Number/'('S Sum @')'S/Call/ident) S;*/
        {

           return And(()=>  
                     (    
                         Number()
                      || And(()=>      
                               Char('(')
                            && S()
                            && Sum()
                            && (    Char(')') || Fatal("<<')'>> expected"))
                            && S() )
                      || Call()
                      || ident())
                  && S() );
		}
        public bool Call()    /*[6]^Call:   ident S '(' @Sum @')' S;*/
        {

           return TreeAST((int)Ecalc0_tree.Call,()=>
                And(()=>  
                     ident()
                  && S()
                  && Char('(')
                  && (    Sum() || Fatal("<<Sum>> expected"))
                  && (    Char(')') || Fatal("<<')'>> expected"))
                  && S() ) );
		}
        public bool Number()    /*[7]^Number:[0-9]+ ('.' [0-9]+)?([eE][+-][0-9]+)?;*/
        {

           return TreeAST((int)Ecalc0_tree.Number,()=>
                And(()=>  
                     PlusRepeat(()=> In('0','9') )
                  && Option(()=>    
                      And(()=>      
                               Char('.')
                            && PlusRepeat(()=> In('0','9') ) ) )
                  && Option(()=>    
                      And(()=>      
                               OneOf("eE")
                            && OneOf("+-")
                            && PlusRepeat(()=> In('0','9') ) ) ) ) );
		}
        public bool ident()    /*[8]^ident: [A-Za-z_][A-Za-z_0-9]*;*/
        {

           return TreeAST((int)Ecalc0_tree.ident,()=>
                And(()=>  
                     (In('A','Z', 'a','z')||OneOf("_"))
                  && OptRepeat(()=>    
                      (In('A','Z', 'a','z', '0','9')||OneOf("_")) ) ) );
		}
        public bool S()    /*[9] S:	    [ \t\v]*;*/
        {

           return OptRepeat(()=> OneOf(" \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