zoukankan      html  css  js  c++  java
  • ECNUOJ 2574 Principles of Compiler

    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] == ''?"Good":"Bad");
    41     }
    42     return 0;
    43 }
    View Code
  • 相关阅读:
    20162324 2016-2017-2《Java程序设计》课程总结
    Java实验五网络编程与安全
    Java结对编程之挑战出题
    实验四Android开发
    Java四则运算总结
    实验三
    Java结对编程四则运算一周小结
    队列课下作业
    20162325 金立清 S2 W5 C14
    2017-2018-1 我爱学Java 第二周 作业
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4629873.html
Copyright © 2011-2022 走看看