[go: up one dir, main page]

0% found this document useful (0 votes)
9 views3 pages

Check Terminal NonTerminal

Uploaded by

kumshubham9870
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views3 pages

Check Terminal NonTerminal

Uploaded by

kumshubham9870
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <stdio.

h>

#include <ctype.h>

#include <string.h>

#define MAX 100

// Func on to check if the character is a non-terminal

int isNonTerminal(char c) {

return isupper(c); // In most grammars, non-terminals are uppercase

// Func on to check if the character is a terminal

int isTerminal(char c) {

return !isupper(c) && c != '|' && c != '-' && c != '>'; // Not uppercase and not part of grammar rule
symbols

int main() {

char grammar[MAX][MAX]; // Array to store grammar rules

int n, i, j;

char terminals[MAX], nonTerminals[MAX];

int terminalCount = 0, nonTerminalCount = 0;

// Input number of produc ons

prin ("Enter the number of produc ons: ");

scanf("%d", &n);

// Input grammar produc ons

prin ("Enter the produc ons (use format A -> BCD | a):\n");

for (i = 0; i < n; i++) {

scanf("%s", grammar[i]);
}

// Traverse each produc on rule

for (i = 0; i < n; i++) {

for (j = 0; grammar[i][j] != '\0'; j++) {

char symbol = grammar[i][j];

// Check if it's a non-terminal

if (isNonTerminal(symbol)) {

if (!strchr(nonTerminals, symbol)) { // Check if it's already in the non-terminals list

nonTerminals[nonTerminalCount++] = symbol;

// Check if it's a terminal

else if (isTerminal(symbol)) {

if (!strchr(terminals, symbol)) { // Check if it's already in the terminals list

terminals[terminalCount++] = symbol;

// Null-terminate the strings

terminals[terminalCount] = '\0';

nonTerminals[nonTerminalCount] = '\0';

// Output non-terminals

prin ("\nNon-terminals: ");

for (i = 0; i < nonTerminalCount; i++) {

prin ("%c ", nonTerminals[i]);

}
// Output terminals

prin ("\nTerminals: ");

for (i = 0; i < terminalCount; i++) {

prin ("%c ", terminals[i]);

return 0;

You might also like