⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testgr.cpp

📁 eC++编译器源码
💻 CPP
字号:
/*********************************************
* PROGRAM: GRAMMER                           *
* AUTHOR: CC                      *
* DATE: 4/23/96                              *
* DESCRIPTION:                               *
*   This is the grammer parsing assignment   *
* that implements % * / + - ~                *
* It also recognizes parentheses.            *
* Note: The operator ~ is used to represent  *
* a negative number. Thus, ~8 is the same as *
* -8 and 2+~3 = -1.                          *
*                                            *
* I used a stack class to hold the operands  *
**********************************************/
#include <Grammar.h> 
#include <cstring.h>
#include <stdio.h>
#include <SYSTEM.h>
//#include <math.h>
 Grammar g;
  char s[1000], c;
  unsigned int i;
  int value;

const int STACK_SIZE = 100;

/******************************************
* Stack class                             *
*                                         *
* Member functions:                       *
*    init -- initialize the stack         *
*    push -- put an item on the stack     *
*    pop -- remove an item from the stack *
*                                         *
*******************************************/
class stack {
   public:
     int count;        //number of items in the stack
     int data[STACK_SIZE];   //the items themselves

     //initialize the stack
     void init(void);

     //push an item on the stack
     void push(const int item);

     //pop an item from the stack
     int pop(void);
};

/**************************************
* stack::init -- initialize the stack *
**************************************/
stack a_stack;
void stack::init(void)
{
   count = 0;    //zero the stack
};

/**************************************
* stack::push -- put item on stack    *
*     warning: I do not check overflow*
*                                     *
* parameters:                         *
*     item -- item to put in the stack*
**************************************/
void stack::push(const int item)
{
   data[count]=item;
   count = count + 1;
};

/***************************************
* stack::pop -- get item from stack    *
*     warning: I do not check underflow*
*                                      *
* Returns:                             *
*     the top item from the stack      *
****************************************/
int stack::pop(void)
{
   count = count - 1;
   return (data[count]);
};

  char get(ADDRESS tag, unsigned int index)
  {
    if (index < i-1) return s[index]; else return '\0';
  };

  boolean put(ADDRESS tag, unsigned int start, unsigned int end, unsigned int arg)
  { char c;
    switch (arg) {
    case 8:
    case 9: break;
    
             //implement subtraction
    case 1:  value = a_stack.pop();
             value = a_stack.pop() - value;
             a_stack.push(value);
             printf("pop pop subtract push %d\n",value);
             break;

             //implement addition
    case 2:  value = a_stack.pop() + a_stack.pop();
             a_stack.push(value);
             printf("pop pop add push %i\n",value);
             break;

             //implement modulus
    case 3:  value = a_stack.pop();
             value = a_stack.pop() % value;
             a_stack.push(value);
             printf("pop pop modulus push %d\n",value);
             break;

             //implement multiplication
    case 4:  value = a_stack.pop() * a_stack.pop();
             a_stack.push(value);
             printf("pop pop multiply push %d\n",value);
             break;

             //implement division
    case 5:  value = a_stack.pop();
             value = a_stack.pop() / value;
             a_stack.push(value);
             printf("pop pop divide push %d\n",value);
             break;

             //negate a number
    case 6:  value = 0 - a_stack.pop();
             a_stack.push(value);
             printf("pop negate push %d\n",value);
             break;

    case 7:  printf("push "); c = s[start]; printf("%c\n", c);    
             //convert and push on an integer value
             a_stack.push(ORD(c) - 48);
             break;
    };
    return true;
  };

void main(void) {
   

  /*     Grammar accepts  and evaluates natural numbers in expressions with ( )
         and operators /,*,+,- ;   Operator precedence is as listed, left to right;
         Division is integer division;  Signed numbers are not allowed, but the
         expression (0-N) can be used in place of any negative natual number;
  */
  s = "START := TERM [ '#9' '-' TERM '!1' ] ;";
  strcat(s, " TERM := ADD [ '#8' '+'  ADD '!2' ]  ;");
  strcat(s, " ADD := MOD [ '#8' '%'  MOD '!3' ]  ;");
  strcat(s, " MOD := MULT [ '#8' '*'  MULT '!4' ]  ;");
  strcat(s, " MULT := FACTOR [ '#8' '/'  FACTOR '!5' ]  ;");
  strcat(s, " FACTOR := '~' FACTOR '!6' | NUMBER | PAR ;");
 // strcat(s, " W :=  NUMBER | PAR ;");
  strcat(s, " NUMBER :=  '0..9' '!7' ;");
  strcat(s, " PAR :=  '(' START ')'  ; .");
  Open(g, s);
  for ( ;; ) {
    a_stack.init();
    i = 0;
    printf("calc: ");
    do {
      if (scanf("%c", c)!=1) HALT;  
      printf("%c\n", c);
      s[i] = c;
      i = i+1;
    } while (c != '.');
    s[i-1] = '\0';
    if (s[0] == '.') break;
    if (!Parse(g, get, put, NULL)) printf("failed");
    printf("Evaluation: %d\n",value);

 };
  Close(g);
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -