test1.cpp
来自「eC++编译器源码」· C++ 代码 · 共 66 行
CPP
66 行
#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
[ ] 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);
};
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?