#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool is_valid_string(const char *s) {
int count_a = 0; // To count the number of 'a's
int count_b = 0; // To count the number of 'b's
// We need to first check that the string has no other characters than 'a' and 'b'
// and that all 'a's appear before any 'b'.
bool found_b = false; // Flag to check if we've started encountering 'b's
for (int i = 0; s[i] != '\0'; i++) {
if (s[i] == 'a') {
if (found_b) {
return false; // If 'b' is encountered before 'a', invalid string
}
count_a++;
}
else if (s[i] == 'b') {
found_b = true;
count_b++;
}
else {
return false; // If any character other than 'a' or 'b', invalid string
}
}
// For the string to be valid, the number of 'a's must be equal to the number of 'b's
return count_a == count_b;
}
int main() {
char input[100]; // Array to hold user input (up to 99 characters)
// Prompt the user for input
printf("Enter a string (only 'a' and 'b' characters are allowed): ");
scanf("%s", input);
// Check and print the result
if (is_valid_string(input)) {
printf("The string '%s' is valid (a^n b^n).\n", input);
} else {
printf("The string '%s' is invalid.\n", input);
}
return 0;
}
#include <stdio.h>
#include <string.h>
int isValidString(char *str) {
int count_a = 0, count_b = 0;
int state = 0; // q0
printf("\nTransition Table\n");
printf("Current State | Input | Next State\n");
printf("-------------------------------\n");
for (int i = 0; str[i] != '\0'; i++) {
char ch = str[i];
printf("q%d | %c | ", state, ch);
if (state == 0) {
if (ch == 'a') {
count_a++;
printf("q0\n");
} else if (ch == 'b') {
state = 1;
count_b++;
printf("q1\n");
} else {
printf("Reject (Invalid character)\n");
return 0;
}
} else if (state == 1) {
if (ch == 'b') {
count_b++;
printf("q1\n");
} else if (ch == 'a') {
printf("Reject (a after b)\n");
return 0;
} else {
printf("Reject (Invalid character)\n");
return 0;
}
}
}
// Final validation
if (state == 1 && count_a == count_b && count_a > 0) {
printf("\nThe string is valid (a^n b^n)\n");
return 1;
} else {
printf("\nThe string is invalid (a^n b^n)\n");
return 0;
}
}
int main() {
char input[100];
printf("Enter the string: ");
scanf("%99s", input); // Prevent buffer overflow
isValidString(input);
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
bool is_identifier(const char *token) {
if (!isalpha(token[0]) && token[0] != '_')
return false;
for (int i = 1; token[i] != '\0'; i++) {
if (!isalnum(token[i]) && token[i] != '_')
return false;
}
return true;
}
bool is_constant(const char *token) {
int i = 0, dot_count = 0;
if (token[i] == '-' || token[i] == '+') i++;
if (token[i] == '\0') return false;
for (; token[i] != '\0'; i++) {
if (token[i] == '.') {
if (++dot_count > 1) return false;
} else if (!isdigit(token[i])) {
return false;
}
}
return true;
}
bool is_operator(const char *token) {
const char *operators[] = {"+", "-", "*", "/", "=", "==", "!=", "<", ">", "<=", ">="};
for (int i = 0; i < sizeof(operators) / sizeof(operators[0]); i++) {
if (strcmp(token, operators[i]) == 0)
return true;
}
return false;
}
int main() {
char token[100];
printf("Enter a token: ");
scanf("%s", token);
if (is_operator(token))
printf("Operator\n");
else if (is_constant(token))
printf("Constant\n");
else if (is_identifier(token))
printf("Identifier\n");
else
printf("Unknown\n");
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
char keywords[10][10] = {"int", "float", "char", "if", "else", "while", "do", "return", "for", "void"};
int isKeyword(char *word) {
for (int i = 0; i < 10; i++) {
if (strcmp(keywords[i], word) == 0)
return 1;
}
return 0;
}
void lexer(char *code) {
int i = 0;
char ch, buffer[20];
int bufferIndex = 0;
while ((ch = code[i++]) != '\0') {
if (isalnum(ch)) {
buffer[bufferIndex++] = ch;
} else {
if (bufferIndex != 0) {
buffer[bufferIndex] = '\0';
bufferIndex = 0;
if (isKeyword(buffer))
printf("[KEYWORD => %s]\n", buffer);
else if (isdigit(buffer[0]))
printf("[NUMBER => %s]\n", buffer);
else
printf("[IDENTIFIER => %s]\n", buffer);
}
if (ch == ' ' || ch == '\n' || ch == '\t')
continue;
if (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '=')
printf("[OPERATOR => %c]\n", ch);
if (ch == '(' || ch == ')' || ch == ';' || ch == '{' || ch == '}')
printf("[SEPARATOR => %c]\n", ch);
}
}
}
int main() {
char code[1000];
printf("Enter code (e.g., int a = 10;):\n");
fgets(code, sizeof(code), stdin);
printf("\n--- Lexical Tokens ---\n");
lexer(code);
return 0;
}
#include <stdio.h>
#include <string.h>
char prod[2][10] = { "S->aA", "A->b" };
char nonTerminals[2][10] = { "S", "A" };
char terminals[3][10] = { "a", "b", "$" };
char table[3][4][15]; // 3 rows (S, A + header), 4 columns (a, b, $, + header)
int getRow(char nt) {
switch (nt) {
case 'S': return 1;
case 'A': return 2;
}
return 0;
}
int getCol(char t) {
switch (t) {
case 'a': return 1;
case 'b': return 2;
case '$': return 3;
}
return 0;
}
int main() {
// Initialize table with empty strings
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
strcpy(table[i][j], " ");
// Fill headers
strcpy(table[0][0], " ");
strcpy(table[0][1], "a");
strcpy(table[0][2], "b");
strcpy(table[0][3], "$");
strcpy(table[1][0], "S");
strcpy(table[2][0], "A");
// Fill table using FIRST sets
strcpy(table[getRow('S')][getCol('a')], "S->aA");
strcpy(table[getRow('A')][getCol('b')], "A->b");
// Print the table
printf("Predictive Parsing Table:\n");
printf("-----------------------------------------\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 4; j++) {
printf("%-12s", table[i][j]);
}
printf("\n-----------------------------------------\n");
}
return 0;
}
#include <stdio.h>
#define SUCCESS 1
#define FAILED 0
const char *cursor;
char input[64];
int E(), Edash(), T(), Tdash(), F();
int main() {
printf("Enter the string: ");
scanf("%s", input);
cursor = input;
printf("\nInput Action\n");
printf("-------------------------------\n");
if (E() && *cursor == '\0') {
printf("-------------------------------\n");
printf("String is successfully parsed\n");
} else {
printf("-------------------------------\n");
printf("Error in parsing String\n");
}
return 0;
}
int E() {
printf("%-15s E -> T E'\n", cursor);
return T() && Edash();
}
int Edash() {
if (*cursor == '+') {
printf("%-15s E' -> + T E'\n", cursor);
cursor++;
return T() && Edash();
}
printf("%-15s E' -> ε\n", cursor);
return SUCCESS;
}
int T() {
printf("%-15s T -> F T'\n", cursor);
return F() && Tdash();
}
int Tdash() {
if (*cursor == '*') {
printf("%-15s T' -> * F T'\n", cursor);
cursor++;
return F() && Tdash();
}
printf("%-15s T' -> ε\n", cursor);
return SUCCESS;
}
int F() {
if (*cursor == '(') {
printf("%-15s F -> ( E )\n", cursor);
cursor++;
if (E() && *cursor == ')') {
cursor++;
return SUCCESS;
}
return FAILED;
} else if (*cursor == 'i') {
printf("%-15s F -> i\n", cursor);
cursor++;
return SUCCESS;
}
return FAILED;
}
#include <stdio.h>
#include <string.h>
int tempVar = 1;
void newTemp(char *temp) {
sprintf(temp, "t%d", tempVar++);
}
void replace(char expr[][10], int *n, int i, char *temp) {
strcpy(expr[i - 1], temp);
for (int j = i + 2; j < *n; j++)
strcpy(expr[j - 2], expr[j]);
*n -= 2;
}
void generateTAC(char input[]) {
char lhs[10], rhs[100];
int n = 0;
// Split into lhs and rhs
sscanf(input, "%[^=]=%s", lhs, rhs);
char expr[50][10], temp[10];
// Tokenize RHS
for (int i = 0; rhs[i]; i++) {
char t[2] = {rhs[i], '\0'};
strcpy(expr[n++], t);
}
// First handle * and /
for (int i = 0; i < n; i++)
if (!strcmp(expr[i], "*") || !strcmp(expr[i], "/")) {
newTemp(temp);
printf("%s = %s %s %s\n", temp, expr[i - 1], expr[i], expr[i + 1]);
replace(expr, &n, i, temp);
i = -1;
}
// Then handle + and -
for (int i = 0; i < n; i++)
if (!strcmp(expr[i], "+") || !strcmp(expr[i], "-")) {
newTemp(temp);
printf("%s = %s %s %s\n", temp, expr[i - 1], expr[i], expr[i + 1]);
replace(expr, &n, i, temp);
i = -1;
}
// Final assignment
printf("%s = %s\n", lhs, expr[0]);
}
int main() {
char expr[100];
printf("Enter expression (e.g., a=b+c*d): ");
scanf("%s", expr);
generateTAC(expr);
return 0;
}
#include <stdio.h>
#include <string.h>
char prol[7][10] = { "S", "A", "A", "B", "B", "C", "C" };
char pror[7][10] = { "A", "Bb", "Cd", "aB", "@", "Cc", "@" };
char prod[7][10] = { "S->A", "A->Bb", "A->Cd", "B->aB", "B->@", "C->Cc", "C->@" };
char first[7][10] = { "abcd", "ab", "cd", "a@", "@", "c@", "@" };
char follow[7][10] = { "$", "$", "$", "a$", "b$", "c$", "d$" };
char table[5][6][10];
int numr(char c)
{
switch (c)
{
case 'S':
return 0;
case 'A':
return 1;
case 'B':
return 2;
case 'C':
return 3;
case 'a':
return 0;
case 'b':
return 1;
case 'c':
return 2;
case 'd':
return 3;
case '$':
return 4;
}
return (2);
}
int main()
{
int i, j, k;
for (i = 0; i < 5; i++)
for (j = 0; j < 6; j++)
strcpy(table[i][j], " ");
printf("The following grammar is used for Parsing Table:\n");
for (i = 0; i < 7; i++)
printf("%s\n", prod[i]);
printf("\nPredictive parsing table:\n");
fflush(stdin);
for (i = 0; i < 7; i++)
{
k = strlen(first[i]);
for (j = 0; j < 10; j++)
if (first[i][j] != '@')
strcpy(table[numr(prol[i][0]) + 1][numr(first[i][j]) + 1], prod[i]);
}
for (i = 0; i < 7; i++)
{
if (strlen(pror[i]) == 1)
{
if (pror[i][0] == '@')
{
k = strlen(follow[i]);
for (j = 0; j < k; j++)
strcpy(table[numr(prol[i][0]) + 1][numr(follow[i][j]) + 1], prod[i]);
}
}
}
strcpy(table[0][0], " ");
strcpy(table[0][1], "a");
strcpy(table[0][2], "b");
strcpy(table[0][3], "c");
strcpy(table[0][4], "d");
strcpy(table[0][5], "$");
strcpy(table[1][0], "S");
strcpy(table[2][0], "A");
strcpy(table[3][0], "B");
strcpy(table[4][0], "C");
printf("\n--------------------------------------------------------\n");
for (i = 0; i < 5; i++)
for (j = 0; j < 6; j++)
{
printf("%-10s", table[i][j]);
if (j == 5)
printf("\n--------------------------------------------------------\n");
}
}
#include <stdio.h>
#include <string.h>
#define NUM_NON_TERMINALS 5
#define NUM_TERMINALS 6
#define MAX_PRODUCTIONS 8
#define MAX_STRING_LENGTH 15
// Grammar productions
char prod[MAX_PRODUCTIONS][MAX_STRING_LENGTH] = {
"E->TE'",
"E'->+TE'",
"E'->@",
"T->FT'",
"T'->*FT'",
"T'->@",
"F->(E)",
"F->id"
};
// Non-terminals and terminals
char nonTerminals[NUM_NON_TERMINALS][MAX_STRING_LENGTH] = { "E", "E'", "T", "T'", "F" };
char terminals[NUM_TERMINALS][MAX_STRING_LENGTH] = { "id", "+", "*", "(", ")", "$" };
// First and Follow sets
char first[MAX_PRODUCTIONS][MAX_STRING_LENGTH] = { "id(", "+", "@", "id(", "*", "@", "(", "id" };
char follow[NUM_NON_TERMINALS][MAX_STRING_LENGTH] = { "$)", "$)", "+$)", "+$)", "+*)$" };
// Parsing table
char table[NUM_NON_TERMINALS + 1][NUM_TERMINALS + 1][MAX_STRING_LENGTH];
int getRow(char c) {
switch (c) {
case 'E': return 1;
case 'e': return 2; // treating E' as e
case 'T': return 3;
case 't': return 4; // T' as t
case 'F': return 5;
default: return 0;
}
}
int getCol(char *sym) {
for (int i = 0; i < NUM_TERMINALS; i++) {
if (strcmp(terminals[i], sym) == 0)
return i + 1;
}
return 0;
}
void fillParsingTable() {
// Initialize table with empty strings
for (int i = 0; i <= NUM_NON_TERMINALS; i++) {
for (int j = 0; j <= NUM_TERMINALS; j++) {
strcpy(table[i][j], "");
}
}
// Header row
strcpy(table[0][0], " ");
for (int i = 0; i < NUM_TERMINALS; i++)
strcpy(table[0][i + 1], terminals[i]);
// Left column (non-terminals)
for (int i = 0; i < NUM_NON_TERMINALS; i++)
strcpy(table[i + 1][0], nonTerminals[i]);
// Fill productions in the table
for (int i = 0; i < MAX_PRODUCTIONS; i++) {
char nt = prod[i][0]; // Non-terminal in production
int row = getRow(nt);
// Iterate through the first set of the production
for (int j = 0; j < strlen(first[i]); j++) {
char ch = first[i][j];
if (ch == '@') {
// Epsilon production, fill the table using the Follow set
for (int k = 0; k < strlen(follow[row - 1]); k++) {
char fch = follow[row - 1][k];
char sym[2] = { fch, '\0' };
int col = getCol(sym);
if (strcmp(table[row][col], "") == 0) // If cell is empty, fill it
strcpy(table[row][col], prod[i]);
}
} else {
char sym[3] = { ch, '\0', '\0' }; // Increase size to 3 to hold 'i' and 'd' for "id"
if (ch == 'i') strcpy(sym, "id"); // map 'i' to "id"
int col = getCol(sym);
if (strcmp(table[row][col], "") == 0) // If cell is empty, fill it
strcpy(table[row][col], prod[i]);
}
}
}
}
void printTable() {
printf("-------------------------------------------------------------\n");
printf(" id + * ( ) $ \n");
printf("-------------------------------------------------------------\n");
for (int i = 0; i <= NUM_NON_TERMINALS; i++) {
for (int j = 0; j <= NUM_TERMINALS; j++) {
if (strcmp(table[i][j], "") != 0) // Only print non-empty cells
printf("%-12s", table[i][j]);
else
printf("%-12s", " "); // Print space for empty cells
}
printf("\n-------------------------------------------------------------\n");
}
}
int main() {
fillParsingTable();
printTable();
return 0;
}