Trail DC
Trail DC
code:
#include <stdio.h>
#include <string.h>
int main() {
char alpha[2];
char input_string[100];
char unique_alpha[2];
int unique_count = 0;
if (unique_count < 2) {
printf("Invalid String\n");
return 0;
}
int count_alpha1 = 0;
int count_alpha2 = 0;
return 0;
}
b. Set of all strings ending with two symbols of the same type.
CODE:-
#include <stdio.h>
#include <string.h>
int main() {
char a[100]; // Assuming a maximum length of 100 characters for the input
string
printf("Enter String: ");
scanf("%s", a);
char l_Str[100]; // Assuming a maximum length of 100 characters for the set
int len_Str;
return 0;
}
Week-2
int main() {
char expression[100];
printf("Enter Expression: ");
scanf("%s", expression);
printf("\t\t\t\tSymbol Table\n");
printf("%-10s %-10s %-10s\n", "Symbol", "Address", "Type");
if ((currentSymbol >= 'a' && currentSymbol <= 'z') || (currentSymbol >= 'A'
&& currentSymbol <= 'Z')) {
strcpy(type, "identifier");
} else if (currentSymbol == '+' || currentSymbol == '-' || currentSymbol ==
'*' || currentSymbol == '/' || currentSymbol == '%') {
strcpy(type, "operator");
} else {
strcpy(type, "unknown");
}
return 0;
}
%%
[aeiouAEIOU] { printf("It is vowel ",yytext); }
. { printf("It is Consonant ",yytext); }
%%
int yywrap(){
return 1;
}
int main()
{
printf("Enter String: ");
yylex();
getch();
return 0;
}
%%
[aeiouAEIOU] { count++; }
[A-Za-z] { count1++; }
\n { printf("The vowels are : %d\nThe Consonants are :%d\n",count,count1); }
%%
int yywrap() {
return 1;
}
int main() {
printf("Enter a string: ");
yylex();
return 0;
%%
\n { line_count++; }
.|\n
%%
int yywrap() {
return 1;
}
int main() {
printf("Enter input (Ctrl+Z followed by Enter to end):\n");
yylex();
printf("The line count is %d",line_count);
getch();
return 0;
}
%%
.*00$ c=1;
. c=0;
%%
int yywrap(void)
{
return 1;
}
int main()
{ printf("Enter a string:");
yylex();
if(c==1)
printf("Accepted");
else
printf("Not Accepted");
getch();
return 0;
}
E) Recognize a string with three consecutive 0’s
%{
#include <stdio.h>
int c = 0;
%}
%%
.*000 { c=1; }
. { c=0; }
%%
int yywrap() {
return 1;
}
int main() {
printf("Enter the String: ");
yylex();
if (c == 1)
printf("Accepted\n");
else
printf("Not Accepted\n");
getch(); return 0;
}
WEEK-3
%%
[0-9]00 c=1;
. c=0;
%%
int yywrap(void)
{
return 1;
}
int main()
{ printf("Enter a string:");
yylex();
if(c==1)
printf("Accepted");
else
printf("Not Accepted");
getch();
return 0;
}
B) The set of all strings with three consecutive 222’s.
%{
#include<stdio.h>
int c=0;
%}
%%
([0-9]*222[0-9]*)+ { c=1; }
. {c=0; }
%%
int yywrap(){
return 1;
}
int main(){
printf("Enter String");
yylex();
if(c==1)
printf("Accepted");
else
printf("Not accepted");
getch();
return 0;
}
C) The set of all string such that every block of five consecutive symbols
contains at least two 5’s.
%{
#include<stdio.h>
int c=0;
%}
%%
5[0-9][0-9][0-9]5 { c=1; }
5[0-9][0-9]5[0-9] { c=1; }
5[0-9]5[0-9][0-9] { c=1; }
55[0-9][0-9][0-9] { c=1; }
[0-9]5[0-9][0-9]5 { c=1; }
[0-9]5[0-9]5[0-9] { c=1; }
[0-9][0-9]5[0-9]5 { c=1; }
[0-9][0-9][0-9]55 { c=1; }
[0-9]55[0-9][0-9] { c=1; }
[0-9][0-9]55[0-9] { c=1; }
.*[a-zA-Z].* { c=-1; }
.* { c=0; }
%%
int yywrap(){
return 1;
}
int main(){
printf("Enter String: ");
yylex();
if(c==-1)
printf("Invalid String");
else if(c==1)
printf("Accepted");
else
printf("Not accepted");
getch();
return 0;
}
D) The set of all strings beginning with a 1 which, interpreted as the binary
representation of an integer, is congruent to zero modulo 5.
%{
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int c=0;
int c1=0;
%}
%%
1[0|1]* {
int length =strlen(yytext)-1;
int num=atoi(yytext);
int i=0,base=1;
int count=0;
while(num!=0)
{
int b=num%10;
c+=b*pow(2,count);
num=num/10;
count++;
}
}
. { c1=1; }
%%
int yywrap(void)
{
return 1;
}
int main()
{ printf("Enter a string:");
yylex();
if(c1==1)
printf("Invalid String");
else if(c%5==0 && c1!=1){
printf("The binary number is %d\n",c);
printf("Accepted");
}
else
printf("Not Accepted");
getch();
return 0;
}
E) The set of all strings such that the 10th symbol from the right end is 1.
%{
#include <stdio.h>
int c=0;
%}
%%
[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]1.* { c=1; }
.* { c=0; }
%%
int yywrap(){
return 1;
}
int main() {
printf("Enter the String: ");
yylex();
if(c==1)
printf("Accepted");
else
printf("Not Accepted");
getch();
return 0;
}
%%
[1-9][0-9][0-9][0-9] {
int num=atoi(yytext);
while(num!=0)
{
c+=num%10;
num=num/10;
}
}
. { c1=1; }
%%
int yywrap(void)
{
return 1;
}
int main()
{ printf("Enter a string:");
yylex();
if(c1==1)
printf("Invalid String");
else if(c==9 && c1!=1)
printf("Accepted");
else
printf("Not Accepted");
getch();
return 0;
}
G) The set of all four digital numbers, whose individual digits are in ascending
order from left to right
%{
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
int c=0;
int c1=0;
%}
%%
[1-6][2-7][3-8][4-9] { c=1; }
[a-zA-Z]* { c1=1; }
%%
int yywrap(void)
{
return 1;
}
int main()
{ printf("Enter a string:");
yylex();
if(c1==1)
printf("Invalid String");
else if(c==1 && c1!=1)
printf("Accepted");
else
printf("Not Accepted");
getch();
return 0;
}
%%
[0-9]*\.[0-9]+ { c=1; }
.* { c=0; }
%%
int yywrap(void)
{
return 1;
}
int main()
{ printf("Enter a string:");
yylex();
if(c==1 )
printf("Accepted");
else
printf("Not Accepted");
getch();
return 0;
}
Week-4
1. Write a C Program to Scan and Count the number of characters, words, and lines
in a
file.
Steps to be followed:
a. Take input of a file name and open that file in a read only mode. Don’t
continue if the file can’t be opened.
b. Traverse the file character by character until you get the EOF character. Every
file ends with the EOF character.
i. Increment the character count.
ii. If the character is not a white-space character, set a flag
in_word to 1.
iii. If the character is a white-space and the in_word flag is 1,
increment the word count and set the in_word flag to 0.
iv. If the character is either ‘\n’ or ‘\0’, increment the line count.
Code:-
#include <stdio.h>
int main() {
char filename[100];
FILE *file;
char ch;
int charCount = 0, wordCount = 0, lineCount = 0, in_word = 0;
if (file == NULL) {
printf("Unable to open the file.\n");
return 0;
}
fclose(file);
return 0;
}
2. Design a lexical analyzer for given language and the lexical analyzer should
ignore
redundant spaces, tabs and new lines. It should also ignore comments. Although the
syntax specification states that identifiers can be arbitrarily long, you may
restrict the
length to some reasonable value. Simulate the same in C language.
CODE:-
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <ctype.h>
#include<stdlib.h>
int isKeyword(const char *word) {
const char *keywords[] = {"int", "float", "void","char","double","if","else
if","else","return"};
for (int i = 0; i < sizeof(keywords) / sizeof(keywords[0]); i++) {
if (strcmp(word, keywords[i]) == 0) {
return 1;
}
}
return 0;
}
fclose(ptr);
return 0;
}
Week-5
%{
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
[0-9]+(\.[0-9]+)? {
if (isNumber(yytext)) {
printf("Number: %s\n", yytext);
}
}
[a-zA-Z_][a-zA-Z_0-9]* {
if (isKeyword(yytext)) {
printf("Keyword: %s\n", yytext);
} else {
printf("Identifier: %s\n", yytext);
}
}
.
%%
int yywrap()
{
return 1;
}
int main() {
yyin = fopen("trail.c", "r");
yylex();
if (!yyin) {
fprintf(stderr, "Failed to open input file\n");
return 1;
}
fclose(yyin);
printf("Press Enter to exit...");
getch();
return 0;
}
WEEK-6
// Function prototypes
bool E();
bool T();
bool match(char token);
char input[100];
int pos = 0;
int main() {
printf("Enter an expression (ending with $): ");
scanf("%s", input);
if (E() && input[pos] == '$') {
printf("Parsing Successful\n");
} else {
printf("Parsing Error\n");
}
return 0;
}
bool E() {
if (input[pos] == 'x') {
pos++;
if (match('+') && T()) {
return true;
}
}
return false;
}
bool T() {
if (input[pos] == '(') {
pos++;
if (E() && match(')')) {
return true;
}
} else if (input[pos] == 'x') {
pos++;
return true;
}
return false;
}
WEEK-7
CODE: -
#include <stdio.h>
#include <string.h>
#define max 20
char prod[max][10];
char ter[10], nt[10];
char first[10][10], follow[10][10];
int eps[10];
int count_var = 0;
int findpos(char ch) {
int n;
for (n = 0; nt[n] != '\0'; n++)
if (nt[n] == ch)
break;
if (nt[n] == '\0')
return 1;
return n;
}
int IsCap(char c) {
if (c >= 'A' && c <= 'Z')
return 1;
return 0;
}
void findfollow() {
int i, j, k, n, e, n1;
n = findpos(prod[0][0]);
add(follow[n], '$');
for (i = 0; i < count_var; i++) {
for (j = 0; j < count_var; j++) {
k = strlen(prod[j]) - 1;
for (; k > 0; k--) {
if (IsCap(prod[j][k])) {
n = findpos(prod[j][k]);
if (prod[j][k + 1] == '\0') {
n1 = findpos(prod[j][0]);
addarr(follow[n], follow[n1]);
}
if (IsCap(prod[j][k + 1])) {
n1 = findpos(prod[j][k + 1]);
addarr(follow[n], first[n1]);
if (eps[n1] == 1) {
n1 = findpos(prod[j][0]);
addarr(follow[n], follow[n1]);
}
} else if (prod[j][k + 1] != '\0')
add(follow[n], prod[j][k + 1]);
}
}
}
}
}
int main() {
char s[max], i;
printf("Enter the productions\n");
scanf("%s", s);
while (strcmp("end", s)) {
addprod(s);
scanf("%s", s);
}
findfirst();
findfollow();
printf("%s\t%s\t%s\n","Non-Terminals","First-Set","Follow-Set");
for (i = 0; i < strlen(nt); i++) {
printf("%c\t\t", nt[i]);
printf("{ ");
if (strlen(first[i]) > 0) {
for (int j = 0; j < strlen(first[i]); j++) {
printf("%c", first[i][j]);
if (j < strlen(first[i]) - 1) {
printf(", ");
}
}
}
printf(" }\t\t{ ");
if (strlen(follow[i]) > 0) {
for (int j = 0; j < strlen(follow[i]); j++) {
printf("%c", follow[i][j]);
if (j < strlen(follow[i]) - 1) {
printf(", ");
}
}
}
else {
printf("%s", "na");
}
printf(" }\n");
}
return 0;
}
WEEk -
8
Predictive parsing uses a stack and a parsing table to parse the input and generate
a parse tree.
Both the stack and the input contains an end symbol $ to denote that the stack is
empty and
the input is consumed. The parser refers to the parsing table to take any decision
on the input
and stack element combination.
CODE: -
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
char FIRST[MAX_SYMBOLS][MAX_SYMBOLS];
char FOLLOW[MAX_SYMBOLS][MAX_SYMBOLS];
char T[MAX_SYMBOLS], NT[MAX_SYMBOLS], G[MAX_RULES][MAX_SYMBOLS],
STACK[MAX_SYMBOLS];
int LL1[MAX_SYMBOLS][MAX_SYMBOLS];
int CREATE_LL1_TABLE() {
int flag = 0;
for (int i = 0; i < cr; i++) {
char arr[MAX_SYMBOLS];
arr[0] = '\0';
count = 0;
FIND_FIRST(arr, G[i][3]);
for (int j = 0; j < count; j++) {
if (arr[j] == '!') {
FIND_FOLLOW(arr, G[i][0]);
break;
}
}
int pos = -1;
for (int k = 0; k < nt; k++) {
if (NT[k] == G[i][0]) {
pos = k;
break;
}
}
for (int j = 0; j < count; j++) {
if (arr[j] != '!') {
for (int k = 0; k < t; k++) {
if (arr[j] == T[k]) {
if (LL1[pos][k] > 0) {
printf("Conflict occur between %s and %s rules!\n",
G[LL1[pos][k] - 1], G[i]);
printf("Given grammar is not LL(1) grammar!\n");
flag = 1;
return flag;
} else
LL1[pos][k] = i + 1;
break;
}
}
}
}
}
return flag;
}
int main() {
char STR[MAX_SYMBOLS];
int flag, ch1;
printf("Enter production rules of grammar in the form A->B\n\n");
flag = 1;
while (flag == 1) {
printf("\n1) Insert Production Rules\n2) Show Grammar\n3) Exit");
printf("\nEnter your choice: ");
scanf("%d", &ch1);
switch (ch1) {
case 1:
printf("Enter rule %d of grammar: ", cr + 1);
scanf("%s", G[cr++]);
case 2:
if (cr > 0) {
printf("\nGrammar\n");
printf("Starting symbol is: %c\n", G[0][0]);
printf("Non-terminal symbols: ");
for (int i = 0; i < nt; i++)
printf("%c ", NT[i]);
printf("\nTerminal symbols: ");
for (int i = 0; i < t; i++)
printf("%c ", T[i]);
printf("\nProduction rules: ");
for (int i = 0; i < cr; i++)
printf("%s ", G[i]);
printf("\n");
} else {
printf("Enter at least one production rule.\n");
}
break;
case 3:
flag = 0;
break;
}
}
FIRST_SHOW();
FOLLOW_SHOW();
T[t++] = '$';
T[t] = '\0';
flag = CREATE_LL1_TABLE();
PARSING_TABLE_SHOW(flag);
if (flag == 0) {
printf("Enter string for parsing: ");
scanf("%s", STR);
LL1_PARSER(STR);
}
return 0;
}
void FIRST_SHOW() {
for (int i = 0; i < nt; i++) {
char arr[MAX_SYMBOLS] = "";
FIND_FIRST(arr, NT[i]);
strcpy(FIRST[i], arr);
}
printf("\nFIRST:\n\n");
for (int i = 0; i < nt; i++) {
printf("FIRST( %c ): { %s }\n", NT[i], FIRST[i]);
}
}
void FOLLOW_SHOW() {
for (int i = 0; i < nt; i++) {
count = 0;
char arr[MAX_SYMBOLS] = "";
FIND_FOLLOW(arr, NT[i]);
strcpy(FOLLOW[i], arr);
}
printf("\nFOLLOW:\n\n");
for (int i = 0; i < nt; i++) {
printf("FOLLOW( %c ): { %s }\n", NT[i], FOLLOW[i]);
}
}
STR[strlen(STR)] = '$';
STACK[top++] = '$';
STACK[top] = G[0][0];
i = 0;
while (STACK[top] != '$') {
for (j = 0; STACK[j] != '\0'; j++)
printf("%c ", STACK[j]);
printf("\t\t");
if (STR[i] == STACK[top]) {
printf("\t\tShift: %c", STACK[top]);
STACK[top] = '\0';
top = top - 1;
i = i + 1;
} else {
for (j = 0; j < nt; j++) {
if (STACK[top] == NT[j]) {
pos = j;
break;
}
}
for (j = 0; j < t; j++) {
if (STR[i] == T[j]) {
pos1 = j;
break;
}
}
n = LL1[pos][pos1];
if (G[n - 1][3] == '!') {
STACK[top] = '\0';
top--;
} else {
for (j = 3; G[n - 1][j] != '\0'; j++)
k = j;
STACK[top] = '\0';
for (j = k; j > 2; j--)
STACK[top++] = G[n - 1][j];
top--;
}
printf("\t\tReplace(in Reverse): %s", G[n - 1]);
}
printf("\n");
}
for (j = 0; STACK[j] != '\0'; j++)
printf("%c ", STACK[j]);
printf("\t\t");
printf("\n");
if (STACK[top] == '$' && STR[i] == '$')
printf("\nParsing successfully\n");
}
WEEK -
9
Shift-Reduce Parsing
Shift-reduce parsing uses two unique steps for bottom-up parsing. These steps are
known as
shift-step and reduce-step.
Shift step: The shift step refers to the advancement of the input pointer to the
next
input symbol, which is called the shifted symbol. This symbol is pushed onto the
stack. The shifted symbol is treated as a single node of the parse tree.
Reduce step: When the parser finds a complete grammar rule (RHS) and replaces it
to (LHS), it is known as reduce-step. This occurs when the top of the stack
contains a
handle. To reduce, a POP function is performed on the stack which pops off the
handle and replaces it with LHS non-terminal symbol.
CODE: -
#include <stdio.h>
#include <string.h>
int z = 0, i = 0, j = 0, c = 0;
char a[20], ac[20], stk[20], act[10];
void check() {
strcpy(ac, "REDUCE TO ");
for (z = 0; z < c; z++) {
if (stk[z] == '(' && stk[z + 1] == 'E' && stk[z + 2] == ')') {
printf("%s E->(E)", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%s\t%s$\t", stk, a);
}
if (stk[z] == 'E' && stk[z + 1] == '*' && stk[z + 2] == 'E') {
printf("%s E->E*E", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}
if (stk[z] == 'E' && stk[z + 1] == '+' && stk[z + 2] == 'E') {
printf("%s E->E+E", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
stk[z + 2] = '\0';
printf("\n$%s\t%s$\t", stk, a);
i = i - 2;
}
if (stk[z] == 'd') {
printf("%s E-> d", ac);
stk[z] = 'E';
stk[z + 1] = '\0';
printf("\n$%s\t%s$\t", stk, a);
}
}
return;
}
int main() {
printf("GRAMMAR is -\nE->E+E\nE->E*E\nE->(E)\nE->d\n");
char a1[100];
printf("Enter A String: ");
scanf("%s", a1);
strcpy(a, a1);
c = strlen(a);
strcpy(act, "SHIFT");
printf("\nstack \t input \t action");
printf("\n$\t%s$\t", a);
for (i = 0, j = 0; j < c; i++, j++) {
printf("%s", act);
stk[i] = a[j];
stk[i + 1] = '\0';
a[j] = ' ';
printf("\n$%s\t%s$\t", stk, a);
check();
}
check();
if (stk[0] == 'E' && stk[1] == '\0' && a[j] == '\0') {
printf("ACCEPT\nInput successfully parsed.\n");
} else {
printf("REJECT\nInput cannot be parsed.\n");
}
return 0;
}
WEEK -
10
YAAC FILE(.Y):-
%{
#include<stdio.h>
int flag=0;
%}
%token NUMBER
/* Rule Section */
%%
ArithmeticExpression: E{
printf("\nResult=%d\n", $$);
return 0;
};
E:E'+'E {$$=$1+$3;}
|E'-'E {$$=$1-$3;}
|E'*'E {$$=$1*$3;}
|E'/'E {$$=$1/$3;}
|E'%'E {$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
%%
//driver code
void main()
{
printf("\nEnter Any Arithmetic Expression which can have operations Addition,
Subtraction, Multiplication, Division, Modulus and Round brackets:\n");
yyparse();
if(flag==0)
printf("\nEntered arithmetic expression is Valid\n\n");
getch();
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
flag=1;
}
LEX CODE(.l): -
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
WEEK-
11
CODE (. Y) :-
%{
#include <stdio.h>
int flag = 0;
int yylex();
%}
ArithmeticExpression: E {
/*printf("\nResult = %d\n", $1);*/
return 0;
};
E: E '+' E { $$ = $1 + $3; }
| E '-' E { $$ = $1 - $3; }
| E '*' E { $$ = $1 * $3; }
| E '/' E { $$ = $1 / $3; }
| E '%' E { $$ = $1 % $3; }
| '(' E ')' { $$ = $2; }
| NUMBER { $$ = $1; }
| E'!'E { yyerror(); }
| E '@' E { yyerror(); }
| E '#' E { yyerror(); }
| E '$' E { yyerror(); }
| E '^' E { yyerror(); }
| E '&' E { yyerror(); }
| E '_' E { yyerror(); }
| E '~' E { yyerror(); }
| E '`' E { yyerror(); }
;
%%
int main() {
printf("\nEnter Any Infix Expression:");
yyparse();
if (flag == 0)
printf("\nString can be generated");
getch();
return 0;
}
CODE (. l): -
%{
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
[-+*/%()] { return yytext[0]; }
. return yytext[0];
%%
int yywrap()
{
return 1;
}
WEEEK-12
CODE: -
#include <stdio.h>
#include <string.h>
int i = 1, j = 0, no = 0, tmpch = 90;
char str[100], left[15], right[15];
struct exp {
int pos;
char op;
} k[15];
void findOperators();
void explore();
void extractLeftOperand(int);
void extractRightOperand(int);
int main() {
printf("\t\tINTERMEDIATE CODE GENERATION\n\n");
printf("Enter the Expression: ");
scanf("%s", str);
findOperators();
explore();
return 0;
}
void findOperators() {
const char operators[] = {':', '/', '*', '+', '-'};