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

📄 grammar.h

📁 eC++编译器源码
💻 H
字号:
#pragma Grammar
#include <SYSTEM.h>
  typedef void *Grammar;
  typedef  char GetProc(/*tag*/ADDRESS a, unsigned int index); /*return 0C for EOS*/
  typedef  boolean ActionProc(/*tag*/ADDRESS a, unsigned int start,
                    unsigned int end, unsigned int arg); /*FALSE=stop*/

  void Open(Grammar &p, char &s[]); /* open with a grammar definition */
  boolean ParseS(Grammar p, char &input[], ActionProc act, ADDRESS tag);
  /* Parse a string */
  boolean Parse(Grammar p, GetProc get, ActionProc act, ADDRESS tag);
  /* Parse an input stream based on a callback function */
  void Close(Grammar &p);

#if defined(SampleProgram)
#include <Grammar.h> 
#include <cstring.h>
#include <stdio.h>
#include <SYSTEM.h>
  Grammar g;
  char s[1000], c;
  unsigned int i;

  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 5:
    case 6: break;
    
    case 1:  printf("pop pop or push\n");
             break;
    case 2:  printf("pop pop and push\n");
             break;
    case 3:  printf("pop not push\n");
             break;
    case 4:  printf("push "); c = s[start]; printf("%c\n", c);
             break;
    };
    return true;
  };

void main(void) {
  /* '#i' if next action doesn't match, accept with argument i
     '!i' call action routine with argument i
     'c..d' match any character from c to d
     'agfdhg' match this string exactly
     (agfdh) match any member of this set.
     \\dd can be used for octal chars; \\ for a \
     [ ] repeat forever. Note: #i conditionally terminates the loop
     | separates alternatives
     ; terminates productions
     . terminates the grammar
     NOTE THAT THE SPACING MUST CONFORM EXACTLY TO THE EXAMPLE.
  */
  s = "START := TERM [ '#6' '|' TERM '!1' ] ;";
  strcat(s, " TERM := FACTOR [ '#5' '&'  FACTOR '!2' ]  ;");
  strcat(s, " FACTOR :=  '~' FACTOR '!3' | LETTER | PAR ;");
  strcat(s, " LETTER :=  'a..z' '!4' ;");
  strcat(s, " PAR :=  '(' START  ')'  ; .");
  Open(g, s);
  for ( ;; ) {
    i = 0;
    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("\n");
  };
  Close(g);
};
#endif

⌨️ 快捷键说明

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