Open In App

DFA (Recognizer) for valid Pascal identifiers

Last Updated : 10 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Problem – Implement a recognizer for pascal identifiers based on a DFA that accepts strings belonging to the definition of the language of the same.

Here is a regular definition for the set of Pascal identifiers that are defined as the set of strings of letters and digits beginning with a letter.

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

The regular expression ID is the pattern for the Pascal identifier token and defines letter and digit where a letter is a regular expression for the set of all upper-case and lowercase letters in the alphabet and digit is regular for the set of all decimal digits.

State diagram of the DFA

Working code for the recognizer:

C++




// C++ program to implement DFA based recognizer that accepts
// all strings which follow the language
// L = { letter (letter | digit)* }
 
#include <bits/stdc++.h>
#include <iostream>
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;
}


Python




import re
 
# dfa tells the number associated with the present state
dfa = 0
 
# This function is for the starting state (zeroth) of DFA
def start(c):
    global dfa
    if c.isalpha():
        dfa = 1
    else:
        # -1 is used to check for any invalid symbol
        dfa = -1
 
# This function is for the first state of DFA
def state1(c):
    global dfa
    if c.isalnum():
        dfa = 1
    else:
        dfa = -1
 
def DFA_for_ID(token):
    global dfa
    dfa = 0
    for c in token:
        if dfa == 0:
            start(c)
        elif dfa == 1:
            state1(c)
        else:
            return False
    if dfa == 1:
        return True
    else:
        return False
 
# driver code
if __name__ == "__main__":
    input_str = "Geeks for Geeks is 9ice platfo$m for every1"
 
    # to separate all the tokens by space in the string and checking for each token
    tokens = re.findall(r'\S+', input_str)
    for token in tokens:
        isValid = DFA_for_ID(token)
        if isValid:
            print("%s : Valid" % token)
        else:
            print("%s : Invalid" % token)


Java




import java.util.StringTokenizer;
 
public class Main {
    // dfa tells the number associated with the present state
    static int dfa;
 
    // This function is for the starting state (zeroth) of DFA
    static void start(char c) {
        if (Character.isLetter(c))
            dfa = 1;
        else
            // -1 is used to check for any invalid symbol
            dfa = -1;
    }
 
    // This function is for the first state of DFA
    static void state1(char c) {
        if (Character.isLetterOrDigit(c))
            dfa = 1;
        else
            dfa = -1;
    }
 
    static boolean DFA_for_ID(String token) {
        dfa = 0;
        int len = token.length();
        for (int i = 0; i < len; i++) {
            if (dfa == 0)
                start(token.charAt(i));
            else if (dfa == 1)
                state1(token.charAt(i));
            else
                return false;
        }
        return dfa == 1;
    }
 
    public static void main(String[] args) {
        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
        StringTokenizer st = new StringTokenizer(input);
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            boolean isValid = DFA_for_ID(token);
            System.out.println(token + " : " + (isValid ? "Valid" : "Invalid"));
        }
    }
}


Javascript




let dfa = 0;
 
// This function is for the starting state (zeroth) of DFA
function start(c) {
    if (c.match(/[a-z]/i)) {
        dfa = 1;
    } else {
        // -1 is used to check for any invalid symbol
        dfa = -1;
    }
}
 
// This function is for the first state of DFA
function state1(c) {
    if (c.match(/[a-z0-9]/i)) {
        dfa = 1;
    } else {
        dfa = -1;
    }
}
 
function DFA_for_ID(token) {
    dfa = 0;
    for (let c of token) {
        if (dfa === 0) {
            start(c);
        } else if (dfa === 1) {
            state1(c);
        } else {
            return false;
        }
    }
    if (dfa === 1) {
        return true;
    } else {
        return false;
    }
}
 
let input_str = "Geeks for Geeks is 9ice platfo$m for every1";
 
// to separate all the tokens by space in the string and checking for each token
let tokens = input_str.split(' ');
for (let token of tokens) {
    let isValid = DFA_for_ID(token);
    if (isValid) {
        console.log(`${token} : Valid`);
    } else {
        console.log(`${token} : Invalid`);
    }
}


C#




using System;
 
namespace MainProgram
{
class MainClass
{
// dfa tells the number associated with the present state
static int dfa;
    // This function is for the starting state (zeroth) of DFA
    static void start(char c)
    {
        if (Char.IsLetter(c))
            dfa = 1;
        else
            // -1 is used to check for any invalid symbol
            dfa = -1;
    }
 
    // This function is for the first state of DFA
    static void state1(char c)
    {
        if (Char.IsLetterOrDigit(c))
            dfa = 1;
        else
            dfa = -1;
    }
 
    static bool DFA_for_ID(string token)
    {
        dfa = 0;
        int len = token.Length;
        for (int i = 0; i < len; i++)
        {
            if (dfa == 0)
                start(token[i]);
            else if (dfa == 1)
                state1(token[i]);
            else
                return false;
        }
        return dfa == 1;
    }
 
    public static void Main(string[] args)
    {
        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
        string[] tokens = input.Split(' ');
        foreach (string token in tokens)
        {
            bool isValid = DFA_for_ID(token);
            Console.WriteLine(token + " : " + (isValid ? "Valid" : "Invalid"));
        }
    }
}
}


Output: 

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

 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads