This module contains the recursive descent parser that does not use variables : Parser « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » ParserScreenshots 
This module contains the recursive descent parser that does not use variables
This module contains the recursive descent 
   parser that does not use variables

/*
C#: The Complete Reference 
by Herbert Schildt 

Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*  
   This module contains the recursive descent 
   parser that does not use variables. 
*/ 
 
using System; 
 
// Exception class for parser errors. 
class ParserException : ApplicationException 
  public ParserException(string str: base(str) { }  
 
  public override string ToString() { 
    return Message; 
  

 
class Parser 
  // Enumerate token types. 
  enum Types NONE, DELIMITER, VARIABLE, NUMBER }
  // Enumerate error types. 
  enum Errors SYNTAX, UNBALPARENS, NOEXP, DIVBYZERO }
 
  string exp;    // refers to expression string 
  int expIdx;    // current index into the expression 
  string token;  // holds current token 
  Types tokType; // holds token's type 
 
  // Parser entry point. 
  public double Evaluate(string expstr
  
    double result; 
   
    exp = expstr; 
    expIdx = 0;  
  
    try {  
      GetToken()
      if(token == "") { 
        SyntaxErr(Errors.NOEXP)// no expression present 
        return 0.0
      
 
      EvalExp2(out result)
 
      if(token != ""// last token must be null 
        SyntaxErr(Errors.SYNTAX)
 
      return result; 
    catch (ParserException exc) { 
      // Add other error handling here, as desired. 
      Console.WriteLine(exc)
      return 0.0
    
  
   
  // Add or subtract two terms. 
  void EvalExp2(out double result
  
    string op; 
    double partialResult; 
   
    EvalExp3(out result)
    while((op = token== "+" || op == "-") { 
      GetToken()
      EvalExp3(out partialResult)
      switch(op) { 
        case "-"
          result = result - partialResult; 
          break
        case "+"
          result = result + partialResult; 
          break
      
    
  
   
  // Multiply or divide two factors. 
  void EvalExp3(out double result
  
    string op; 
    double partialResult = 0.0
   
    EvalExp4(out result)
    while((op = token== "*" || 
           op == "/" || op == "%") { 
      GetToken()
      EvalExp4(out partialResult)
      switch(op) { 
        case "*"
          result = result * partialResult; 
          break
        case "/"
          if(partialResult == 0.0
            SyntaxErr(Errors.DIVBYZERO)
          result = result / partialResult; 
          break
        case "%"
          if(partialResult == 0.0
            SyntaxErr(Errors.DIVBYZERO)
          result = (intresult % (intpartialResult; 
          break
      
    
  
   
  // Process an exponent. 
  void EvalExp4(out double result
  
    double partialResult, ex; 
    int t; 
   
    EvalExp5(out result)
    if(token == "^") { 
      GetToken()
      EvalExp4(out partialResult)
      ex = result; 
      if(partialResult == 0.0) { 
        result = 1.0
        return
      
      for(t=(int)partialResult-1; t > 0; t--
        result = result * (double)ex; 
    
  
   
  // Evaluate a unary + or -. 
  void EvalExp5(out double result
  
    string  op; 
   
    op = ""
    if((tokType == Types.DELIMITER&& 
        token == "+" || token == "-") { 
      op = token; 
      GetToken()
    
    EvalExp6(out result)
    if(op == "-"result = -result; 
  
   
  // Process a parenthesized expression. 
  void EvalExp6(out double result
  
    if((token == "(")) { 
      GetToken()
      EvalExp2(out result)
      if(token != ")"
        SyntaxErr(Errors.UNBALPARENS)
      GetToken()
    
    else Atom(out result)
  
   
  // Get the value of a number. 
  void Atom(out double result
  
    switch(tokType) { 
      case Types.NUMBER: 
        try 
          result = Double.Parse(token)
        catch (FormatException) { 
          result = 0.0
          SyntaxErr(Errors.SYNTAX)
        
        GetToken()
        return
      default
        result = 0.0
        SyntaxErr(Errors.SYNTAX)
        break
    
  
   
  // Handle a syntax error. 
  void SyntaxErr(Errors error
  
    string[] err = 
      "Syntax Error"
      "Unbalanced Parentheses"
      "No Expression Present"
      "Division by Zero" 
    }
 
    throw new ParserException(err[(int)error])
  
   
  // Obtain the next token. 
  void GetToken() 
  
    tokType = Types.NONE; 
    token = ""
    
    if(expIdx == exp.Lengthreturn// at end of expression 
   
    // skip over white space 
    while(expIdx < exp.Length && 
          Char.IsWhiteSpace(exp[expIdx])) ++expIdx; 
 
    // trailing whitespace ends expression 
    if(expIdx == exp.Lengthreturn
 
    if(IsDelim(exp[expIdx])) { // is operator 
      token += exp[expIdx]
      expIdx++; 
      tokType = Types.DELIMITER; 
    
    else if(Char.IsLetter(exp[expIdx])) { // is variable 
      while(!IsDelim(exp[expIdx])) { 
        token += exp[expIdx]
        expIdx++; 
        if(expIdx >= exp.Lengthbreak
      
      tokType = Types.VARIABLE; 
    
    else if(Char.IsDigit(exp[expIdx])) { // is number 
      while(!IsDelim(exp[expIdx])) { 
        token += exp[expIdx]
        expIdx++; 
        if(expIdx >= exp.Lengthbreak
      
      tokType = Types.NUMBER; 
    
  
   
  // Return true if c is a delimiter. 
  bool IsDelim(char c
  
    if((" +-/*%^=()".IndexOf(c!= -1)) 
      return true
    return false
  
   



// Demonstrate the parser. 

 
public class ParserDemo 
  public static void Main() 
  
    string expr; 
    Parser p = new Parser()
 
    Console.WriteLine("Enter an empty expression to stop.")
 
    for(;;) { 
      Console.Write("Enter expression: ")
      expr = Console.ReadLine()
      if(expr == ""break
      Console.WriteLine("Result: " + p.Evaluate(expr))
    
  
}


           
       
Related examples in the same category
1.This module contains the recursive descent parser that recognizes variables
java2s.com  |  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.