DFA (Recognizer) for valid Pascal identifiers
Last Updated :
10 Apr, 2023
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++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int dfa;
void start( char c)
{
if ( isalpha (c))
dfa = 1;
else
dfa = -1;
}
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;
}
int main()
{
string input = "Geeks for Geeks is 9ice platfo$m for every1 " ;
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 = 0
def start(c):
global dfa
if c.isalpha():
dfa = 1
else :
dfa = - 1
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
if __name__ = = "__main__" :
input_str = "Geeks for Geeks is 9ice platfo$m for every1"
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 {
static int dfa;
static void start( char c) {
if (Character.isLetter(c))
dfa = 1 ;
else
dfa = - 1 ;
}
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 " ;
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;
function start(c) {
if (c.match(/[a-z]/i)) {
dfa = 1;
} else {
dfa = -1;
}
}
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" ;
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
{
static int dfa;
static void start( char c)
{
if (Char.IsLetter(c))
dfa = 1;
else
dfa = -1;
}
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" ;
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
Share your thoughts in the comments
Please Login to comment...