Principles of Compiler
Time Limit:1000MS Memory Limit:65536KB
Total Submit:473 Accepted:106
Description
After learnt the Principles of Compiler,partychen thought that he can solve a simple expression problem.So he give you strings of less than 100 characters which strictly adhere to the following grammar (given in EBNF):
A:= '(' B')'|'x'.
B:=AC.
C:={'+'A}.
Can you solve them too?
Input
The first line of input gives the number of cases, N(1 ≤ N ≤ 100). N test cases follow.
The next N lines will each contain a string as described above.
Output
For each test case,if the expression is adapt to the EBNF above output “Good”,else output “Bad”.
Sample Input
3
(x)
(x+(x+x))
()(x)
Sample Output
Good
Good
Bad
Source
解题:几十万只草泥马呼啸而过,一直没搞懂{+A}的含义
重复 { ... } 也就是说这个是表示+A可以出现0次或多次
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 210; 4 char str[maxn]; 5 int cur; 6 bool A(); 7 bool B(); 8 bool C(); 9 bool A() { 10 if(str[cur] == 'x') { 11 cur++; 12 return true; 13 } 14 if(str[cur] == '(') { 15 cur++; 16 if(B() && str[cur] == ')') { 17 cur++; 18 return true; 19 } 20 } 21 return false; 22 } 23 bool B() { 24 return A() && C(); 25 } 26 bool C() { 27 while(str[cur] == '+'){ 28 cur++; 29 if(!A()) return false; 30 } 31 return true; 32 } 33 int main() { 34 int kase; 35 scanf("%d",&kase); 36 getchar(); 37 while(kase--) { 38 gets(str); 39 cur = 0; 40 puts(A() && str[cur] == '