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

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

, 7 Oct 2008 CPOL
Introduction to the parsing method PEG with library and parser generator
PEG_GrammarExplorer.zip
PEG_GrammarExplorer
Documents
PEG_GrammarExplorer_fromCP-Dateien
PEG_GrammerExplorer.jpg
mssccprj.scc
PEG Explorer
mssccprj.scc
PEG Explorer.csproj.user
Properties
Settings.settings
vssver2.scc
vssver2.scc
PegBase
mssccprj.scc
Properties
vssver2.scc
vssver2.scc
PegSamples
BasicEncodingRules
input
CDURKR2URKR125195
DefiniteLengthForm
CDURKR2URKR125195
hello
TDAUTPTEUR0100011.tap3
vssver2.scc
hello
IndefiniteLengthForm
DefiniteLengthForm
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011.tap3
TDAUTPTEUR0100011_withError.tap3
vssver2.scc
vssver2.scc
calc0_direct
input
vssver2.scc
vssver2.scc
calc0_tree
input
vssver2.scc
vssver2.scc
CSharp3
docu
input
vssver2.scc
vssver2.scc
C_KernighanRitchie2
input
vssver2.scc
vssver2.scc
EMail
input
vssver2.scc
vssver2.scc
Json
input
vssver2.scc
peg_template
vssver2.scc
vssver2.scc
mssccprj.scc
PEG Samples.csproj.user
PegGenerator
input
C#
TestCases
C#
.cs
vssver2.scc
vssver2.scc
vssver2.scc
vssver2.scc
Properties
vssver2.scc
python_2_5_2
input
adwords
awapi_python_samples_1.0.0
src
decoratorators_01
vssver2.scc
Problems
Sample PEG Console Parser
PEG Console Parser
input
Properties
Sample PEG Console Parser.csproj.user
vssver2.scc
PEG_GrammarExplorer_Submission.zip
PEG_GrammarExplorer_Submission
PEG_GrammerExplorer.jpg
PEG_GrammerExplorer.zip
using Peg.Base;
using System;
using System.IO;
using System.Text;
namespace calc0_direct 
{
      
      enum Ecalc0_direct {Expr= 1, Sum= 2, Product= 3, Value= 4, Number= 5, S= 6};
      class calc0_direct  : PegCharParser 
      {
        class top{ // semantic top level block using C# as host language
  internal double result;
  internal bool print_(){Console.WriteLine("{0}",result);return true;}
}
top top;

        #region Input Properties
        public static EncodingClass encodingClass = EncodingClass.ascii;
        public static UnicodeDetection unicodeDetection = UnicodeDetection.notApplicable;
        #endregion Input Properties
        #region Constructors
        public calc0_direct ()
            : base()
        {
            top= new top();

        }
        public calc0_direct (string src,StreamWriter FerrOut)
			: base(src,FerrOut)
        {
            top= new top();

        }
        #endregion Constructors
        #region Overrides
        public override string GetRuleNameFromId(int id)
        {
            try
            {
                   Ecalc0_direct  ruleEnum = (Ecalc0_direct )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 Expr()    /*Expr:    S Sum   (!. print_ / FATAL<"following code not recognized">)				;*/
        {

           return And(()=>  
                     S()
                  && Sum()
                  && (    
                         And(()=>    Not(()=> Any() ) && top.print_() )
                      || Fatal("following code not recognized")) );
		}
   class _Sum{  //semantic rule related block using C# as host language (block can be implemented as nested struct)
        internal _Sum(calc0_direct parent){parent_=parent;}
        calc0_direct parent_;
	double v;
  	internal bool save_(){v= parent_.top.result;parent_.top.result=0; return true;}
	internal bool add_() {v+= parent_.top.result;parent_.top.result=0;return true;}
	internal bool sub_() {v-= parent_.top.result;parent_.top.result=0;return true;} 
  	internal bool store_(){parent_.top.result= v; return true;}
}	
        public bool Sum()    /*Sum
{  //semantic rule related block using C# as host language (block can be implemented as nested struct)
        internal _Sum(calc0_direct parent){parent_=parent;}
        calc0_direct parent_;
	double v;
  	internal bool save_(){v= parent_.top.result;parent_.top.result=0; return true;}
	internal bool add_() {v+= parent_.top.result;parent_.top.result=0;return true;}
	internal bool sub_() {v-= parent_.top.result;parent_.top.result=0;return true;} 
  	internal bool store_(){parent_.top.result= v; return true;}
}	:     
	Product save_
                ('+' S Product add_
		/'-' S Product sub_)* store_		;*/
        {

             var _sem= new _Sum(this);

           return And(()=>  
                     Product()
                  && _sem.save_()
                  && OptRepeat(()=>    
                            
                               And(()=>        
                                       Char('+')
                                    && S()
                                    && Product()
                                    && _sem.add_() )
                            || And(()=>        
                                       Char('-')
                                    && S()
                                    && Product()
                                    && _sem.sub_() ) )
                  && _sem.store_() );
		}
   class _Product{ //semantic rule related block using C# as host language (block can be implemented as nested struct)
        internal _Product(calc0_direct parent){parent_=parent;}
        calc0_direct parent_;
	double v;
	internal bool save_(){v= parent_.top.result;parent_.top.result=0; return true;}
	internal bool mul_(){v*= parent_.top.result;parent_.top.result=0; return true;}
	internal bool div_(){v/= parent_.top.result;parent_.top.result=0;return true;} 
	internal bool store_(){parent_.top.result= v;return true;}	
}	
        public bool Product()    /*Product
{ //semantic rule related block using C# as host language (block can be implemented as nested struct)
        internal _Product(calc0_direct parent){parent_=parent;}
        calc0_direct parent_;
	double v;
	internal bool save_(){v= parent_.top.result;parent_.top.result=0; return true;}
	internal bool mul_(){v*= parent_.top.result;parent_.top.result=0; return true;}
	internal bool div_(){v/= parent_.top.result;parent_.top.result=0;return true;} 
	internal bool store_(){parent_.top.result= v;return true;}	
}	: 
	Value  save_
	        ('*' S Value mul_
		/'/' S Value div_)* store_		;*/
        {

             var _sem= new _Product(this);

           return And(()=>  
                     Value()
                  && _sem.save_()
                  && OptRepeat(()=>    
                            
                               And(()=>        
                                       Char('*')
                                    && S()
                                    && Value()
                                    && _sem.mul_() )
                            || And(()=>        
                                       Char('/')
                                    && S()
                                    && Value()
                                    && _sem.div_() ) )
                  && _sem.store_() );
		}
        public bool Value()    /*Value:   Number S / '(' S Sum ')' S	;*/
        {

           return   
                     And(()=>    Number() && S() )
                  || And(()=>    
                         Char('(')
                      && S()
                      && Sum()
                      && Char(')')
                      && S() );
		}
   class _Number{ //semantic rule related block using C# as host language (block can be implemented as nested struct)
        internal _Number(calc0_direct parent){parent_=parent;}
        calc0_direct parent_;
	internal string sNumber;
	internal bool store_(){double.TryParse(sNumber,out parent_.top.result);return true;}
}
	
        public bool Number()    /*Number
{ //semantic rule related block using C# as host language (block can be implemented as nested struct)
        internal _Number(calc0_direct parent){parent_=parent;}
        calc0_direct parent_;
	internal string sNumber;
	internal bool store_(){double.TryParse(sNumber,out parent_.top.result);return true;}
}
	:  ([0-9]+ ('.' [0-9]+)?):sNumber store_	;*/
        {

             var _sem= new _Number(this);

           return And(()=>  
                     Into(()=>    
                      And(()=>      
                               PlusRepeat(()=> In('0','9') )
                            && Option(()=>        
                                    And(()=>          
                                                 Char('.')
                                              && PlusRepeat(()=> In('0','9') ) ) ) ),out _sem.sNumber)
                  && _sem.store_() );
		}
        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)

Share

About the Author

Martin.Holzherr

Switzerland Switzerland
No Biography provided

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141216.1 | Last Updated 7 Oct 2008
Article Copyright 2008 by Martin.Holzherr
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid