Click here to Skip to main content
15,892,737 members
Articles / Programming Languages / C#

Parsing - It Is Easy. Base CSharp Classes and Expressions Calculator

Rate me:
Please Sign up or sign in to vote.
3.67/5 (4 votes)
13 Mar 2008CPOL1 min read 20.9K   318   18  
Provide Base C# parsing classes with demo application and description
// SimpexKeyword.cs:
//
//    Copyright (C) 2007  by Alex Nek
//    alexnek@russinger.com
//    simpleparser.russinger.com
//
//    This file is part of ExpressionParser classes
//
//    ExpressionParser classes is free software:
//    you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>
//////////////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
using SimpleParser;

namespace ExpressionsParser
{
    /// <summary>
    /// Simple expression keyword class
    /// We have functions name as keyword
    /// </summary>
    class CSimpexKeyword : CParseObject
    {
        #region Data Members
        /// <summary>
        /// Function node
        /// </summary>
        private CFunctionNode m_FunctionNode = null;
        #endregion

        /// <summary>
        /// Consructor
        /// </summary>
        public CSimpexKeyword ()
        {
        }

        /// <summary>
        /// Parse function name and parameters
        /// </summary>
        /// <param name="Executor">Data Storage. We store parsed data here</param>
        /// <param name="Parser">Data Parser. We parse input stream here</param>
        /// <param name="Token">Next Token. We always have the next Token after parsing the current item</param>
        public override void Parse (CExecutor Executor, CExpressionParser Parser, ref CToken Token)
        {
            //Check if we really have a function call/keyword
            if (Token.TokenClass == CToken.ETokenClass.Keyword)
            {
                m_FunctionNode = new CFunctionNode(Token.Value, Token.TokenType);
                //Get function parameters count by function id
                int ParametersCount = CFunctionNode.GetParametrsCount(Token.TokenType);
                //skip keyword
                Parser.GetNextToken(ref Token);
                if (ParametersCount > 0)
                {
                    if (Token.TokenType == (int)CSimpexToken.ETokenType.OpenBracket)
                    {
                        //skip (
                        Parser.GetNextToken(ref Token);

                        //loop for exact parameter count
                        for (int i = 0; i < ParametersCount; i++)
                        {
                            CExpressionContainer ExpressionContainer = new CExpressionContainer(Executor.GetTempExpressionName());
                            Executor.AddExpresion(ExpressionContainer);

                            CSimpexExpression Expression = new CSimpexExpression();

                            //parse exression
                            Expression.Parse(Executor, Parser, ref Token);

                            ExpressionContainer.SetRoot(Expression.Node);

                            CExpressionNode Node = new CExpessionNode(ExpressionContainer.Name);
                            m_FunctionNode.AddParameter(Node);

                            //here we must have parameter delimiter
                            if (Token.TokenType == (int)CSimpexToken.ETokenType.Comma)
                            {
                                //skip ,
                                Parser.GetNextToken(ref Token);
                            }
                            else 
                            { 
                                //Do we need fire an error?
                            }

                        }

                        //After parameters list we must have closed bracket
                        if (Token.TokenType == (int)CSimpexToken.ETokenType.CloseBracket)
                        {
                            //skip )
                            Parser.GetNextToken(ref Token);
                        }
                        else
                        {
                            Parser.Error("Expected - ')'");
                            Parser.GetNextToken(ref Token);
                        }
                    }
                }
                else if (ParametersCount < 0)
                {
                    Parser.Error("Unknown parameters count for function " + m_FunctionNode.Name);
                    Parser.GetNextToken(ref Token);
                }
            }
            else
            {
                Parser.Error("Expected function name");
                Parser.GetNextToken(ref Token);
            }
        }
        #region Properties
        /// <summary>
        /// Current node
        /// </summary>
        public CExpressionNode Node
        {
            get
            {
                return m_FunctionNode;
            }
        }
        #endregion
    }
}
#region Syntax
/*
function_designator ::= FUNCTION_NAME [ actual_parameter_list ] 
actual_parameter_list ::= '(' actual_parameter ( ',' actual_parameter )? ')' 
actual_parameter ::= expression
*/
#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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
I started my way with Fortran and going via Pascal, Delphi, C and C++. I'm falling in love with C# now.

Comments and Discussions