📜  用于有效 Pascal 标识符的 DFA(识别器)

📅  最后修改于: 2022-05-13 01:54:10.554000             🧑  作者: Mango

用于有效 Pascal 标识符的 DFA(识别器)

问题——实现一个基于 DFA 的 pascal 标识符识别器,该 DFA 接受属于相同语言定义的字符串。

这是一组 Pascal 标识符的常规定义,这些标识符被定义为以字母开头的字母和数字字符串的集合。

letter : A | B | . . . | Z | a | b | . . . | z
digit  : 0 | 1 | 2 | . . . | 9
ID : letter (letter | digit)* 

正则表达式 ID 是 Pascal 标识符标记的模式,并定义字母和数字,其中字母是字母表中所有大写和小写字母集合的正则表达式,数字是所有十进制数字集合的正则表达式。

DFA的状态图

识别器的工作代码:

C++
// C++ program to implement DFA based regonizer that accepts
// all strings which follow the language
// L = { letter (letter | digit)* }
 
#include 
#include 
using namespace std;
 
// dfa tells the number associated
// with the present state
int dfa;
 
// This function is for
// the starting state (zeroth) of DFA
void start(char c)
{
    if (isalpha(c))
        dfa = 1;
    else
 
// -1 is used to check for any invalid symbol
        dfa = -1;
}
 
// This function is for the first state of DFA
void state1(char c)
{
    if (isalnum(c))
        dfa = 1;
    else
        dfa = -1;
}
 
bool DFA_for_ID(string token)
{
    dfa = 0;
    int i, len = token.length();
    for (i = 0; i < len; i++) {
        if (dfa == 0)
            start(token[i]);
        else if (dfa == 1)
            state1(token[i]);
        else
            return 0;
    }
    if (dfa == 1)
        return 1;
    else
        return 0;
}
 
// driver code
int main()
{
    string input = "Geeks for Geeks is 9ice platfo$m for every1  ";
 
// to separate all the tokens by space in the string
// and checking for each token
    stringstream ss(input);
    string token;
    while (ss >> token) {
        bool isValid = DFA_for_ID(token);
        if (isValid)
            cout << token << " : "
                 << "Valid" << endl;
        else
            cout << token << " : "
                 << "Invalid" << endl;
    }
    return 0;
}


输出:

Geeks : Valid
for : Valid
Geeks : Valid
is : Valid
9ice : Invalid
platfo$m : Invalid
for : Valid
every1 : Valid