0% found this document useful (0 votes)
5 views8 pages

C Programming: Identifiers, Keywords, and Parsing

Uploaded by

Keshav Bagaade
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)
5 views8 pages

C Programming: Identifiers, Keywords, and Parsing

Uploaded by

Keshav Bagaade
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

CD Lab External

1. Determine Whether a Given String is an


Identifier
#include <stdio.h>
#include <ctype.h>

int isIdentifier(const char* str) {


if (!isalpha(str[0]) && str[0] != '_') return 0;
for (int i = 1; str[i]; i++) {
if (!isalnum(str[i]) && str[i] != '_') return 0;
}
return 1;
}

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isIdentifier(str))
printf("The string is a valid identifier.\n");
else
printf("The string is not a valid identifier.\n");
return 0;
}

3. Check Whether the Given String is a


Keyword

CD Lab External 1
#include <stdio.h>
#include <string.h>

int isKeyword(const char* str) {


const char* keywords[] = {
"auto", "break", "case", "char", "const", "continue", "d
"else", "enum", "extern", "float", "for", "goto", "if",
"return", "short", "signed", "sizeof", "static", "struct
"union", "unsigned", "void", "volatile", "while", NULL
};

for (int i = 0; keywords[i]; i++) {


if (strcmp(str, keywords[i]) == 0)
return 1;
}
return 0;
}

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (isKeyword(str))
printf("The string is a keyword.\n");
else
printf("The string is not a keyword.\n");
return 0;
}

4. Heap Storage Allocation

CD Lab External 2
#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr;
int n;

printf("Enter the number of elements: ");


scanf("%d", &n);

arr = (int*) malloc(n * sizeof(int));


if (arr == NULL) {
printf("Memory allocation failed\n");
return 1;
}

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


printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]);
}

printf("Elements are: ");


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");

free(arr);
return 0;
}

CD Lab External 3
5. Check Whether the Given String
Contains Operators
#include <stdio.h>
#include <string.h>

int containsOperator(const char* str) {


const char* operators = "+-*/%&|^!";
for (int i = 0; str[i]; i++) {
if (strchr(operators, str[i])) return 1;
}
return 0;
}

int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
if (containsOperator(str))
printf("The string contains operators.\n");
else
printf("The string does not contain operators.\n");
return 0;
}

6. Prepare TAC (Three Address Code) for


the Given Postfix Notation
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

CD Lab External 4
#include <string.h>

#define MAX 100

typedef struct {
char op1[MAX], op2[MAX], result[MAX];
} TAC;

void postfixToTAC(const char* postfix) {


char stack[MAX][MAX];
int top = -1;
TAC tac[MAX];
int tacIndex = 0;

for (int i = 0; postfix[i]; i++) {


if (isalnum(postfix[i])) {
stack[++top][0] = postfix[i];
stack[top][1] = '\0';
} else {
char op1[MAX], op2[MAX];
strcpy(op2, stack[top--]);
strcpy(op1, stack[top--]);

snprintf(tac[tacIndex].result, MAX, "t%d", tacIndex)


snprintf(tac[tacIndex].op1, MAX, "%s", op1);
snprintf(tac[tacIndex].op2, MAX, "%s", op2);
tacIndex++;

printf("%s = %s %c %s\n", tac[tacIndex - 1].result,


strcpy(stack[++top], tac[tacIndex - 1].result);
}
}
}

int main() {
char postfix[MAX];

CD Lab External 5
printf("Enter postfix expression: ");
scanf("%s", postfix);
postfixToTAC(postfix);
return 0;
}

7. LALR
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STACK_SIZE 100


#define TOKEN_SIZE 10

typedef struct {
int state;
char token[TOKEN_SIZE];
} StackElement;

typedef struct {
StackElement stack[STACK_SIZE];
int top;
} ParseStack;

void push(ParseStack *s, int state, const char *token) {


if (s->top < STACK_SIZE - 1) {
s->top++;
s->stack[s->top].state = state;
strncpy(s->stack[s->top].token, token, TOKEN_SIZE - 1);
s->stack[s->top].token[TOKEN_SIZE - 1] = '\0';
} else {
fprintf(stderr, "Stack overflow\n");

CD Lab External 6
exit(1);
}
}

StackElement pop(ParseStack *s) {


if (s->top >= 0) {
return s->stack[s->top--];
} else {
fprintf(stderr, "Stack underflow\n");
exit(1);
}
}

int topState(ParseStack *s) {


return s->stack[s->top].state;
}

void parse(const char *input) {


ParseStack stack;
[Link] = -1;
push(&stack, 0, "$");

const char *token = strtok((char *)input, " ");


while (token != NULL) {
int state = topState(&stack);
printf("Current state: %d, token: %s\n", state, token);

// Simulated LALR parsing logic


// In reality, you'd use the LALR table to drive the par
if (strcmp(token, "id") == 0) {
// Example action for token 'id'
printf("Shift\n");
push(&stack, state + 1, token); // Dummy state trans
} else if (strcmp(token, "+") == 0 || strcmp(token, "*")
// Example action for operators
printf("Reduce\n");

CD Lab External 7
pop(&stack);
} else {
fprintf(stderr, "Syntax error: Unexpected token %s\n
exit(1);
}

token = strtok(NULL, " ");


}

if (strcmp([Link][[Link]].token, "$") == 0) {
printf("Parsing successful\n");
} else {
printf("Parsing failed\n");
}
}

int main() {
const char *input = "id + id";
parse(input);
return 0;
}

CD Lab External 8

You might also like