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
/*
**  BINSRCH.C - Binary search arrays for a key.
**
**  Public domain demo by Bob Stout
*/

#include <stdio.h>
#include <stddef.h>
#include "binsrch.h"

#if !(__cplusplus)

/*
**  BinSearchI(): Search a int key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchI(int key, int *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                        return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return -1;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

/*
**  BinSearchC(): Search a character key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchC(signed char key, signed char *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                      return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return Error_;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

/*
**  BinSearchB(): Search a byte key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchB(unsigned char key, unsigned char *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                      return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return -1;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

/*
**  BinSearchS(): Search a short key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchS(signed short key, signed short *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                        return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return -1;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

/*
**  BinSearchW(): Search a word key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchW(unsigned short key, unsigned short *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                        return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return -1;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

/*
**  BinSearchL(): Search a long key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchL(signed long key, signed long *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                        return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return -1;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

/*
**  BinSearchDW(): Search a double word key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchDW(unsigned int key, unsigned int *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                        return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return -1;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

/*
**  BinSearchF(): Search a float key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchF(float key, float *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                        return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return -1;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

/*
**  BinSearchD(): Search a double key
**
**  Arguments: 1 - Address to return the key position
**             2 - Key to find
**             3 - Pointer to the beginning of the array
**             4 - Number of elements in the array
**
**  Returns: Index if found, else -1
*/

int BinSearchD(double key, double *r, unsigned n)
{
      int high, i, low;

      if (n > 1)
      {
            for (low = 0, high = n-1;  high - low > 1; )
            {
                  i = (high+low) / 2;
                  if (key == r[i])
                        return i;
                  else if (key < r[i])
                        high = i;
                  else  low  = i;
            }
            if (key == r[high])
                  return high;
            else if (key == r[low])
                  return low;
            else  return -1;
      }
      else if (r[0] == key)
            return 0;
      else  return -1;
}

#ifdef TEST

#include <stdlib.h>

#include <stdio.h>

main(int argc, char *argv[])
{
      double a1[] = { 1, 3, 17, 39, 66, 67, 88, 111, 222, 333, 345 };
      double a2[] = { 1, 3, 17, 39, 66, 67, 88, 111, 222, 333, 345, 678};
      double a3[] = { 5, 9, 37 };
      double a4[] = { 5, 9, 13 };
      double a5[] = { 5, 9 };
      double a6[] = { 6 };
      double a7[] = { -20, 1, 30 };
      double key;
      int    idx;
      
      while (--argc)
      {
            key = atof(*(++argv));
            if (-1 != (idx = BinSearchD(key, (double *)a1, sizeof(a1) / sizeof(key))))
                  printf("Key %f found at position %d in a1[]\n", key, idx);
            else  printf("Key %f not found in a1[]\n", key);
            if (-1 != (idx = BinSearchD(key, (double *)a2, sizeof(a2) / sizeof(key))))
                  printf("Key %f found at position %d in a2[]\n", key, idx);
            else  printf("Key %f not found in a2[]\n", key);
            if (-1 != (idx = BinSearchD(key, (double *)a3, sizeof(a3) / sizeof(key))))
                  printf("Key %f found at position %d in a3[]\n", key, idx);
            else  printf("Key %f not found in a3[]\n", key);
            if (-1 != (idx = BinSearchD(key, (double *)a4, sizeof(a4) / sizeof(key))))
                  printf("Key %f found at position %d in a4[]\n", key, idx);
            else  printf("Key %f not found in a4[]\n", key);
            if (-1 != (idx = BinSearchD(key, (double *)a5, sizeof(a5) / sizeof(key))))
                  printf("Key %f found at position %d in a5[]\n", key, idx);
            else  printf("Key %f not found in a5[]\n", key);
            if (-1 != (idx = BinSearchD(key, (double *)a6, sizeof(a6) / sizeof(key))))
                  printf("Key %f found at position %d in a6[]\n", key, idx);
            else  printf("Key %f not found in a6[]\n", key);
            if (-1 != (idx = BinSearchD(key, (double *)a7, sizeof(a7) / sizeof(key))))
                  printf("Key %f found at position %d in a7[]\n", key, idx);
            else  printf("Key %f not found in a7[]\n", key);
      }
      
      return EXIT_SUCCESS;
}


#endif /* TEST */

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
Web04 | 2.8.141223.1 | Last Updated 7 Oct 2008
Article Copyright 2008 by Martin.Holzherr
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid