Notes![what is notes.io? What is notes.io?](/theme/images/whatisnotesio.png)
![]() ![]() Notes - notes.io |
#include <stdlib.h>
enum state {q0,q1,q2,qf};
enum state delta(enum state s, char ch)
{
enum state current_state;
switch (s)
{
case q0:
if (ch == '0')
current_state = q1;
else
current_state = q2;
break;
case q1:
if (ch == '1')
current_state = qf;
else
current_state = q2;
break;
case qf:
if (ch == '0')
current_state = qf;
else
current_state = qf;
break;
case q2:
if (ch == '0')
current_state = q2;
else
current_state = q2;
break;
}
return current_state;
}
int main()
{
char ch, choice;
char input[100];
enum state current_state = q0;
int i = 0;
printf("------DFA TO ACCEPT STRING STARTS WITH 01------");
printf("n Enter the String: ");
gets(input);
ch = input[i];
while (ch != ' ')
{
current_state = delta(current_state, ch);
ch = input[++i];
}
if (current_state == qf)
printf("Acceptedn");
else
printf("Rejectedn");
return 0;
}
#include <stdio.h>
int main()
{
enum state {q0,q1,qf};
char ch;
char input[100];
enum state current_state = q0;
int i = 0;
int flag = 0;
printf("------NDFA TO ACCEPT STRING STARTS WITH 01------");
printf("n Enter the String: ");
gets(input);
ch = input[i];
while (ch != ' '){
switch(current_state)
{
case q0:
if (ch == '0')
current_state = q1;
else
flag = 1;
break;
case q1:
if (ch == '1')
current_state = qf;
else
flag=1;
break;
case qf:
if(ch=='0'||ch=='1')
current_state = qf;
break;
}
if(flag)
break;
ch=input[++i];}
if (current_state == qf)
printf("The string %s is Accepted.",input);
else
printf("The string %s is Rejected.",input);
return 0;}
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#include<process.h>
int iskeywords(char b[])
{
char keywords[32][10] =
{
"auto","double","int","struct","break","else","long","switch","case","enum","register","typedef","char","extern","return","union","const","float","short","unsigned","continue","for","signed","void","default","goto","sizeof","volatile","do","if","static","while"
};
int i, flag=0;
for(i=0; i<32; ++i)
{
if(strcmp(keywords[i],b)==0)
{
flag=1;
break;
}
}
return flag;
}
int main()
{
char ch, buffer[15], operators[]="+-*/%=";
FILE *fp;
int i, j=0;
fp=fopen("aa.txt","r");
if(fp==NULL)
{
printf("n File Cannot be opened");
exit(0);
}
while((ch=fgetc(fp))!=EOF)
{
for(i=0; i<6; i++)
{
if(ch==operators[i])
printf("%c is operator n",ch);
}
if(isalnum(ch))
{
buffer[j++]=ch;
}
else if((ch==' '|| ch=='n')&&j!=0)
{
buffer[j]=' ';
j=0;
if(iskeywords(buffer)==1)
printf("%s is a keyword n",buffer);
else
printf("%s is a identifern",buffer);
}
}
fclose(fp);
return 0;
}
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
int main()
{
char a[10];
int flag, i=1;
printf("n Enter the Identifier: ");
gets(a);
if(isalpha(a[0])||a[0]=='_')
flag=1;
else
{
printf("n Not a valid identifer");
exit (0);
}
while(a[i]!=' ')
{
if(!isdigit(a[i])&& !isalpha(a[i])&& a[i]!='_')
{
flag=0;
break;
}
i++;
}
if(flag==1)
printf("n Valid Identifer") ;
else
printf("n Invalid Identifer");
}
//To test whether given entered string is valid single line or multiline comments.
#include <stdio.h>
#include <string.h>
// Function to check if a string is a valid single-line comment
int isValidSingleLineComment(const char* str) {
// Check if the string starts with //
return strncmp(str, "//", 2) == 0;
}
// Function to check if a string is a valid multiline comment
int isValidMultilineComment(const char* str) {
int len = strlen(str);
// Check if the string starts with /* and ends with */
return len >= 4 && strncmp(str, "/*", 2) == 0 && strncmp(str + len - 2, "*/", 2) == 0;
}
int main() {
char input[256];
printf("Enter a comment to validate: ");
fgets(input, sizeof(input), stdin);
// Remove newline character at the end if present
size_t len = strlen(input);
if (len > 0 && input[len - 1] == 'n') {
input[len - 1] = ' ';
}
if (isValidSingleLineComment(input)) {
printf("The input is a valid single-line comment.n");
} else if (isValidMultilineComment(input)) {
printf("The input is a valid multiline comment.n");
} else {
printf("The input is not a valid comment.n");
}
return 0;
}
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#define MAX 10
struct Grammar {
char production[MAX][MAX];
int numProductions;
char nonTerminals[MAX];
int numNonTerminals;
} grammar;
struct Sets {
char first[MAX][MAX];
char follow[MAX][MAX];
} sets;
void addToSet(char *set, char ch) {
int len = strlen(set);
for (int i = 0; i < len; i++) {
if (set[i] == ch) {
return;
}
}
set[len] = ch;
set[len + 1] = ' ';
}
void computeFirst(char symbol, char *result) {
if (!isupper(symbol)) {
addToSet(result, symbol);
return;
}
for (int i = 0; i < grammar.numProductions; i++) {
if (grammar.production[i][0] == symbol) {
if (grammar.production[i][2] == ' ') {
addToSet(result, '#');
} else {
int j = 2;
while (grammar.production[i][j] != ' ') {
computeFirst(grammar.production[i][j], result);
if (strchr(sets.first[grammar.production[i][j] - 'A'], '#') == NULL) {
break;
}
j++;
}
}
}
}
}
void computeFollow(char symbol) {
if (grammar.nonTerminals[0] == symbol) {
addToSet(sets.follow[symbol - 'A'], '$');
}
for (int i = 0; i < grammar.numProductions; i++) {
for (int j = 2; j < strlen(grammar.production[i]); j++) {
if (grammar.production[i][j] == symbol) {
if (grammar.production[i][j + 1] != ' ') {
computeFirst(grammar.production[i][j + 1], sets.follow[symbol - 'A']);
}
if (grammar.production[i][j + 1] == ' ' || strchr(sets.first[grammar.production[i][j + 1] - 'A'], '#') != NULL) {
computeFollow(grammar.production[i][0]);
strcat(sets.follow[symbol - 'A'], sets.follow[grammar.production[i][0] - 'A']);
}
}
}
}
}
int main() {
printf("Enter the number of productions: ");
scanf("%d", &grammar.numProductions);
printf("Enter the productions (e.g., A=BC):n");
for (int i = 0; i < grammar.numProductions; i++) {
scanf("%s", grammar.production[i]);
grammar.nonTerminals[i] = grammar.production[i][0];
}
grammar.numNonTerminals = strlen(grammar.nonTerminals);
// Compute FIRST sets
for (int i = 0; i < grammar.numNonTerminals; i++) {
computeFirst(grammar.nonTerminals[i], sets.first[grammar.nonTerminals[i] - 'A']);
}
// Compute FOLLOW sets
for (int i = 0; i < grammar.numNonTerminals; i++) {
computeFollow(grammar.nonTerminals[i]);
}
// Print FIRST sets
printf("nFIRST sets:n");
for (int i = 0; i < grammar.numNonTerminals; i++) {
printf("FIRST(%c) = { %s }n", grammar.nonTerminals[i], sets.first[grammar.nonTerminals[i] - 'A']);
}
// Print FOLLOW sets
printf("nFOLLOW sets:n");
for (int i = 0; i < grammar.numNonTerminals; i++) {
printf("FOLLOW(%c) = { %s }n", grammar.nonTerminals[i], sets.follow[grammar.nonTerminals[i] - 'A']);
}
return 0;
}
Source Code:
//To construct a LL(1) parser using C program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 10
#define MAX_RULES 10
struct Grammar {
char productions[MAX_RULES][MAX];
int numProductions;
char nonTerminals[MAX];
int numNonTerminals;
char terminals[MAX];
int numTerminals;
} grammar;
struct Sets {
char first[MAX][MAX];
char follow[MAX][MAX];
} sets;
char parsingTable[MAX][MAX][MAX];
void addToSet(char *set, char ch) {
if (!strchr(set, ch)) {
int len = strlen(set);
set[len] = ch;
set[len + 1] = ' ';
}
}
void computeFirst(char symbol, char *result) {
if (!isupper(symbol)) {
addToSet(result, symbol);
return;
}
for (int i = 0; i < grammar.numProductions; i++) {
if (grammar.productions[i][0] == symbol) {
if (grammar.productions[i][2] == ' ') {
addToSet(result, '#');
} else {
for (int j = 2; grammar.productions[i][j] != ' '; j++) {
computeFirst(grammar.productions[i][j], result);
if (!strchr(sets.first[grammar.productions[i][j] - 'A'], '#')) {
break;
}
}
}
}
}
}
void computeFollow(char symbol) {
if (grammar.nonTerminals[0] == symbol) {
addToSet(sets.follow[symbol - 'A'], '$');
}
for (int i = 0; i < grammar.numProductions; i++) {
for (int j = 2; grammar.productions[i][j] != ' '; j++) {
if (grammar.productions[i][j] == symbol) {
if (grammar.productions[i][j + 1] != ' ') {
computeFirst(grammar.productions[i][j + 1], sets.follow[symbol - 'A']);
}
if (grammar.productions[i][j + 1] == ' ' || strchr(sets.first[grammar.productions[i][j + 1] - 'A'], '#')) {
computeFollow(grammar.productions[i][0]);
strcat(sets.follow[symbol - 'A'], sets.follow[grammar.productions[i][0] - 'A']);
}
}
}
}
}
void constructParsingTable() {
for (int i = 0; i < grammar.numNonTerminals; i++) {
for (int j = 0; j < grammar.numTerminals; j++) {
parsingTable[i][j][0] = ' ';
}
}
for (int i = 0; i < grammar.numProductions; i++) {
char nonTerminal = grammar.productions[i][0];
char firstSet[MAX] = "";
computeFirst(grammar.productions[i][2], firstSet);
for (int j = 0; j < strlen(firstSet); j++) {
if (firstSet[j] != '#') {
int row = nonTerminal - 'A';
int col = firstSet[j] - 'a';
strcpy(parsingTable[row][col], grammar.productions[i]);
}
}
if (strchr(firstSet, '#')) {
char followSet[MAX] = "";
strcat(followSet, sets.follow[nonTerminal - 'A']);
for (int j = 0; j < strlen(followSet); j++) {
int row = nonTerminal - 'A';
int col = followSet[j] - 'a';
strcpy(parsingTable[row][col], grammar.productions[i]);
}
}
}
}
void parseString(char *input) {
char stack[MAX] = "$S";
int top = 1;
int i = 0;
printf("nParsing Steps:n");
printf("%-10s%-10s%-10sn", "Stack", "Input", "Action");
while (top >= 0) {
printf("%-10s%-10s", stack, input + i);
char stackTop = stack[top];
char currentInput = input[i];
if (stackTop == '$' && currentInput == '$') {
printf("Acceptedn");
return;
}
if (stackTop == currentInput) {
top--;
i++;
printf("Match %cn", currentInput);
} else if (isupper(stackTop)) {
int row = stackTop - 'A';
int col = currentInput - 'a';
if (strlen(parsingTable[row][col]) == 0) {
printf("Errorn");
return;
}
printf("Output %sn", parsingTable[row][col]);
top--;
for (int j = strlen(parsingTable[row][col]) - 1; j >= 2; j--) {
if (parsingTable[row][col][j] != '#') {
stack[++top] = parsingTable[row][col][j];
}
}
} else {
printf("Errorn");
return;
}
}
}
int main() {
printf("Enter the number of productions: ");
scanf("%d", &grammar.numProductions);
printf("Enter the productions (e.g., S=AB|a):n");
for (int i = 0; i < grammar.numProductions; i++) {
scanf("%s", grammar.productions[i]);
}
grammar.numNonTerminals = 0;
grammar.numTerminals = 0;
for (int i = 0; i < grammar.numProductions; i++) {
char nonTerminal = grammar.productions[i][0];
if (!strchr(grammar.nonTerminals, nonTerminal)) {
grammar.nonTerminals[grammar.numNonTerminals++] = nonTerminal;
}
for (int j = 2; grammar.productions[i][j] != ' '; j++) {
if (!isupper(grammar.productions[i][j]) && !strchr(grammar.terminals, grammar.productions[i][j])) {
grammar.terminals[grammar.numTerminals++] = grammar.productions[i][j];
}
}
}
for (int i = 0; i < grammar.numNonTerminals; i++) {
computeFirst(grammar.nonTerminals[i], sets.first[grammar.nonTerminals[i] - 'A']);
}
for (int i = 0; i < grammar.numNonTerminals; i++) {
computeFollow(grammar.nonTerminals[i]);
}
constructParsingTable();
printf("nFIRST sets:n");
for (int i = 0; i < grammar.numNonTerminals; i++) {
printf("FIRST(%c) = { %s }n", grammar.nonTerminals[i], sets.first[grammar.nonTerminals[i] - 'A']);
}
printf("nFOLLOW sets:n");
for (int i = 0; i < grammar.numNonTerminals; i++) {
printf("FOLLOW(%c) = { %s }n", grammar.nonTerminals[i], sets.follow[grammar.nonTerminals[i] - 'A']);
}
printf("nParsing Table:n");
for (int i = 0; i < grammar.numNonTerminals; i++) {
for (int j = 0; j < grammar.numTerminals; j++) {
if (strlen(parsingTable[i][j]) > 0) {
printf("M[%c, %c] = %sn", grammar.nonTerminals[i], grammar.terminals[j], parsingTable[i][j]);
}
}
}
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX 100
typedef struct {
char op[3];
char arg1[10];
char arg2[10];
char result[10];
} TAC;
TAC tac[MAX];
int tempCount = 0;
int tacCount = 0;
void generateTAC(char* expr);
void parseExpression(char* expr, char* result);
void generateTemp(char* temp) {
sprintf(temp, "t%d", tempCount++);
}
void addTAC(char* op, char* arg1, char* arg2, char* result) {
strcpy(tac[tacCount].op, op);
strcpy(tac[tacCount].arg1, arg1);
strcpy(tac[tacCount].arg2, arg2);
strcpy(tac[tacCount].result, result);
tacCount++;
}
void parseExpression(char* expr, char* result) {
char *pos = strchr(expr, '+');
if (pos != NULL) {
char left[MAX], right[MAX];
strncpy(left, expr, pos - expr);
left[pos - expr] = ' ';
strcpy(right, pos + 1);
char temp1[10], temp2[10];
parseExpression(left, temp1);
parseExpression(right, temp2);
generateTemp(result);
addTAC("+", temp1, temp2, result);
return;
}
pos = strchr(expr, '*');
if (pos != NULL) {
char left[MAX], right[MAX];
strncpy(left, expr, pos - expr);
left[pos - expr] = ' ';
strcpy(right, pos + 1);
char temp1[10], temp2[10];
parseExpression(left, temp1);
parseExpression(right, temp2);
generateTemp(result);
addTAC("*", temp1, temp2, result);
return;
}
if (isalpha(expr[0])) {
strcpy(result, expr);
} else {
generateTemp(result);
addTAC("=", expr, "", result);
}
}
void generateTAC(char* expr) {
char result[10];
parseExpression(expr, result);
addTAC("=", result, "", expr);
}
int main() {
char expr[MAX];
printf("Enter the expression: ");
scanf("%s", expr);
generateTAC(expr);
printf("nThree-Address Code:n");
for (int i = 0; i < tacCount; i++) {
if (strlen(tac[i].arg2) > 0) {
printf("%s = %s %s %sn", tac[i].result, tac[i].arg1, tac[i].op, tac[i].arg2);
} else {
printf("%s = %sn", tac[i].result, tac[i].arg1);
}
}
return 0;
}
//To generate assembly code for given source code using C program.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 100
typedef struct {
char op[3];
char arg1[10];
char arg2[10];
char result[10];
} TAC;
TAC tac[MAX];
int tacCount = 0;
void readTAC();
void generateAssembly();
void generateInstruction(char* instruction, char* result, char* arg1, char* arg2);
int main() {
readTAC();
generateAssembly();
return 0;
}
void readTAC() {
printf("Enter the number of three-address code instructions: ");
scanf("%d", &tacCount);
printf("Enter the three-address code instructions (format: result op arg1 arg2):n");
for (int i = 0; i < tacCount; i++) {
scanf("%s %s %s %s", tac[i].result, tac[i].op, tac[i].arg1, tac[i].arg2);
}
}
void generateAssembly() {
char instruction[MAX];
printf("nGenerated Assembly Code:n");
for (int i = 0; i < tacCount; i++) {
if (strcmp(tac[i].op, "=") == 0) {
sprintf(instruction, "MOV %s, %s", tac[i].result, tac[i].arg1);
} else if (strcmp(tac[i].op, "+") == 0) {
generateInstruction("ADD", tac[i].result, tac[i].arg1, tac[i].arg2);
} else if (strcmp(tac[i].op, "-") == 0) {
generateInstruction("SUB", tac[i].result, tac[i].arg1, tac[i].arg2);
} else if (strcmp(tac[i].op, "*") == 0) {
generateInstruction("MUL", tac[i].result, tac[i].arg1, tac[i].arg2);
} else if (strcmp(tac[i].op, "/") == 0) {
generateInstruction("DIV", tac[i].result, tac[i].arg1, tac[i].arg2);
}
printf("%sn", instruction);
}
}
void generateInstruction(char* instruction, char* result, char* arg1, char* arg2) {
char line1[MAX], line2[MAX], line3[MAX];
sprintf(line1, "MOV R1, %s", arg1);
sprintf(line2, "%s R1, %s", instruction, arg2);
sprintf(line3, "MOV %s, R1", result);
printf("%sn%sn%sn", line1, line2, line3);
}
![]() |
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team