文法去除空产生式_文法化简 (CFG Simplification) 翻译
时间: 2024-04-06 08:33:26 浏览: 26
文法去除空产生式,也称为文法化简,是上下文无关文法(CFG)的一种常见化简形式。该化简形式的目的是消除文法中的空产生式,即可以直接推导出空串的产生式。
具体来说,文法去除空产生式的步骤如下:
1. 找出所有可以直接推导出空串的产生式,将它们从产生式集合中删除。
2. 对于剩下的产生式集合中的每个非终结符,找出所有可以通过一系列推导过程得到空串的产生式,将这些产生式中该非终结符的出现位置全部替换成空串。
3. 重复步骤2,直到没有新的产生式可以替换。
经过文法去除空产生式的化简后,产生式集合中不再存在可以直接推导出空串的产生式,这样可以使得我们在进行语法分析时更加简洁和高效。
需要注意的是,文法去除空产生式可能会导致某些非终结符无法推导出任何符号串,这些非终结符称为无用符号,可以通过进一步的文法化简来消除。
相关问题
CFG消除空产生式C++代码
以下是一份使用 C++ 实现的消除空产生式的代码:
```c++
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <algorithm>
using namespace std;
// 用于存储产生式
struct Production {
string left;
string right;
Production(string l, string r) : left(l), right(r) {}
};
vector<Production> productions; // 存储 CFG 的所有产生式
set<char> nullable; // 存储所有可空的符号
set<string> visited; // 存储已经访问过的产生式,避免重复访问
// 判断一个符号是否可空
bool isNullable(char symbol) {
return nullable.count(symbol) > 0;
}
// 判断一个产生式是否可空
bool isNullable(const string& str) {
for (char c : str) {
if (!isNullable(c)) {
return false;
}
}
return true;
}
// 判断一个产生式是否已经被访问过
bool hasVisited(const string& str) {
return visited.count(str) > 0;
}
// 计算所有可空的符号
void calculateNullable() {
bool changed = true;
while (changed) {
changed = false;
for (auto& p : productions) {
if (isNullable(p.right)) {
if (!isNullable(p.left[0])) {
nullable.insert(p.left[0]);
changed = true;
}
}
}
}
}
// 将一个字符串中的所有可空符号去除
void removeNullable(string& str) {
string result;
for (char c : str) {
if (!isNullable(c)) {
result += c;
}
}
str = result;
}
// 生成新的产生式,去除其中的可空符号
void generateNewProduction(const Production& p, const string& str, vector<Production>& newProductions) {
string newStr = str;
removeNullable(newStr);
if (!newStr.empty()) {
newProductions.emplace_back(p.left, newStr);
}
}
// 处理可空产生式
void handleNullableProductions() {
vector<Production> newProductions;
for (auto& p : productions) {
if (isNullable(p.right)) {
generateNewProduction(p, "", newProductions);
for (int i = 0; i < p.right.size(); i++) {
if (isNullable(p.right[i])) {
string subStr = p.right.substr(0, i) + p.right.substr(i + 1);
generateNewProduction(p, subStr, newProductions);
}
}
}
}
productions.insert(productions.end(), newProductions.begin(), newProductions.end());
}
// 消除 CFG 中的空产生式
void eliminateNullProductions() {
calculateNullable();
handleNullableProductions();
set<string> newProductions;
for (auto& p : productions) {
if (hasVisited(p.right)) {
continue;
}
visited.insert(p.right);
newProductions.insert(p.right);
}
productions.clear();
for (auto& p : newProductions) {
productions.emplace_back("S", p);
}
}
// 测试代码
int main() {
productions.emplace_back("S", "ABCD");
productions.emplace_back("A", "aA|a");
productions.emplace_back("B", "bB|b");
productions.emplace_back("C", "cC|c");
productions.emplace_back("D", "dD|d|");
eliminateNullProductions();
for (auto& p : productions) {
cout << p.left << " -> " << p.right << endl;
}
return 0;
}
```
该代码实现了从 CFG 中消除空产生式的算法。它首先计算出所有可空的符号,然后处理所有可空的产生式,生成新的产生式,并将其中的可空符号去除。最后,它将产生式中的重复项合并,并将它们存储到一个新的 vector 中。
GCC_GP2_CFG_RCGR
GCC_GP2_CFG_RCGR 是 Qualcomm Snapdragon 处理器中的一个寄存器,用于配置 Global Clock Generator 2 (GP2) 中的 Reduced Clock Generator (RCG)。它包含如下字段:
- SRC_SEL:用于选择源时钟,可选项为 PLL 或者 XO。
- SRC_DIV:用于设置分频比,该值为 2N-1,其中 N 为分频比。例如,若分频比为 2.5,那么 SRC_DIV 应该设置为 00100。
- FABRIC_SEL:用于选择时钟域,可选项为 PCIE、USB、或者 CAMSS。
- FABRIC_DIV:用于设置时钟域的分频比,该值为 2N-1,其中 N 为分频比。
通过对 GCC_GP2_CFG_RCGR 的配置,可以控制 GP2 中的 RCG 时钟的频率和时钟域。