• 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
  • 相关阅读:
    VC++下封装ADO类以及使用方法
    MFC浅析(7) CWnd类虚函数的调用时机、缺省实现
    JavaScript原生数组函数
    C#4.0泛型的协变,逆变深入剖析
    《这就是搜索引擎:核心技术详解》总结
    栈和队列
    JS菜单条智能定位效果
    实现模型工厂,依赖注入以及格式配置
    Intellij IDEA 快捷键整理
    printf code
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/4629873.html
Copyright © 2020-2023  润新知