Professional Documents
Culture Documents
Lab Assig 2021
Lab Assig 2021
Lab Assig 2021
1a. Write a C/C++/Java/Python program which recognizes the key strokes as you press different
keys on keyboard (letters/ digits/ special characters/ white spaces).
CODE:
import java.util.Scanner;
public class AlphabetDigitSpecial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter any caracter : ");
char ch = scanner.next().charAt(0);
if((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
System.out.println(ch + " is A ALPHABET.");
} else if(ch >= '0' && ch <= '9') {
System.out.println(ch + " is A DIGIT.");
} else {
System.out.println(ch + " is A SPECIAL CHARACTER.");
}
}
}
OUTPUT:
CODE:
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
ch == '/' || ch == ',' || ch == ';' || ch == '>' || ch == '<' || ch == '=' || ch == '(' || ch == ')' || ch == '[' || ch == ']' || ch ==
'{' || ch == '}') return (true);
return (false);
{
if (ch == '+' || ch == '-' || ch == '*' ||
return (true);
return (false);
str[0] == '6' || str[0] == '7' || str[0] == '8' || str[0] == '9' || isDelimiter(str[0]) == true) return (false);
return (true);
!strcmp(str, "break") ||
return (false);
}
if (len == 0)
return (true);
&& str[i] != '9' || (str[i] == '-' && i > 0)) return (false);
if (len == 0)
if (str[i] == '.')
hasDecimal = true;
return (hasDecimal);
}
int i;
|| (right == len && left != right)) { char* subStr = subString(str, left, right - 1)
if (isKeyword(subStr) == true)
return;
left = right;
// DRIVER FUNCTION
int main()
return (0);
}
OUTPUT:
OUTPUT:
Grammar Output
E -> E + T | T E -> TE’
T -> T * F |F E’ -> +TE’ | Є
F -> (E) | id T -> FT’
T’ -> *FT’ | Є
F -> (E) | id
4a. Write a program to simulate lexical analyzer for validating operators.
CODE:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package cc_lab5;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CC_Lab5 {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String[] input = {"+", "2021", "xyz", "i++", "j++", "/", "34", "2abc2", "<", ">", "=", "<=", ">=="};
//RE for arithmetic operators
Pattern re1 = Pattern.compile("^[+-/*%]|[$_A-Za-z]*[A-Za-z0-9_]+[++]|[$_A-Za-z]*[A-Za-z0-9_]*[--]$");
//RE for relational operators
Pattern re2 = Pattern.compile("^[<>]?[=]?$");
//RE for logical and ternary operators
Pattern re3 = Pattern.compile("^[|&!?:]$");
Matcher m1, m2, m3;
for (int i = 0; i < input.length; i++) {
m1 = re1.matcher(input[i]);
m2 = re2.matcher(input[i]);
m3 = re3.matcher(input[i]);
if ( m1.find() | m2.find() | m3.find() ) {
System.out.println("Input: " + input[i] + " - Valid operator");
} else {
System.out.println("Input: " + input[i] + " - Invalid operator");
}
}
}
}
OUTPUT
4b. 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<string.h>
#include<conio.h>
#include<ctype.h>
#include<stdio.h>
void main()
{
FILE *f1;
char c,str[10];
int lineno=1,num=0,i=0;
printf("\nEnter the c program\n");
f1=fopen("input.txt","w");
while((c=getchar())!=EOF)
putc(c,f1);
fclose(f1);
f1=fopen("input.txt","r");
while((c=getc(f1))!=EOF) // TO READ THE GIVEN FILE
{
if(isdigit(c)) // TO RECOGNIZE NUMBERS
{
num=c-48;
c=getc(f1);
while(isdigit(c))
{
num=num*10+(c-48);
c=getc(f1);
}
printf("%d is a number \n",num);
ungetc(c,f1);
}
else if(isalpha(c)) // TO RECOGNIZE KEYWORDS AND IDENTIFIERS
{
str[i++]=c;
c=getc(f1);
while(isdigit(c)||isalpha(c)||c=='_'||c=='$')
{
str[i++]=c;
c=getc(f1);
}
str[i++]='\0';
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||
strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||
strcmp("double",str)==0||strcmp("static",str)==0||
strcmp("switch",str)==0||strcmp("case",str)==0) // TYPE 32 KEYWORDS
printf("%s is a keyword\n",str);
else
printf("%s is a identifier\n",str);
ungetc(c,f1);
i=0;
}
else if(c==' '||c=='\t') // TO IGNORE THE SPACE
printf("\n");
else if(c=='\n') // TO COUNT LINE NUMBER
lineno++;
else // TO FIND SPECIAL SYMBOL
printf("%c is a special symbol\n",c);
}
printf("Total no. of lines are: %d\n",lineno);
fclose(f1);
getch();
}
OUTPUT:
5. Implement the stack program for the following grammar which take the input string
“)id *+id$”
E→TE'
E'→+TE/ ε
T→FT'
T`→*FT'/ ε
F→ (E)/id
CODE:
#include <cstdlib>
#include <iostream>
class arrayStack
{
protected char arr[];
protected int top,size;
public arrayStack()
{
size=20;
arr=new char[size];
top=-1;
}
public void push(char i)
{
if (top + 1>= size)
{
print("error, skip");}
if (top+1< size){
arr[++top] = i;}}
public char pop()
{
return arr[top--];}
public void display()
{
for (int i= top; i >=0; i--)
print(arr[i]);}
}
OUTPUT
For the string “)id*+id$” given grammar will generate an error but for any other string like “a+a*a$” given
grammar will be parsed completely.
• Here A=E`, B=T` and a=id
}
else
printf("\nString not accepted"); getch();
} E()
{ if(T())
{ if(EP())
return(1); else return(0);
}
Else
return(0);
} EP()
{
if(input[i]=='+')
{ i++;
if(T())
{ if(EP())
return(1); else return(0);
}
else return(0);
}
else return(1);
} T()
{
if(F())
{ if(TP())
return(1); else return(0);
}
else return(0);
} TP()
{
if(input[i]=='*')
{ i++;
if(F())
{ if(TP())
return(1); else return(0);
}
else return(0);
}
else return(1);
} F()
{
if(input[i]=='(')
{ i++;
if(E())
if(input[i]==')')
{ i++;
return(1);
}
else return(0);
}
else return(0);
}
else if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z')
{ i++;
return(1);
else return(0);
}
OUTPUT:
7. Write a program to implement operator precedence parsing.
#include<iostream>
#include<string>
#include<deque>
using namespace std;
int n,n1,n2;
int getPosition(string arr[], string q, int size)
{
for(int i=0;i<size;i++)
{
if(q == arr[i])
return i;
}
return -1;
}
int main()
{
string prods[10],leads[10],trails[10],nonterms[10],terms[10];
char op_table[20][20] = {};
cout<<"Enter the number of productions : ";
cin>>n;
cin.ignore();
cout<<"Enter the productions"<<endl;
for(int i=0;i<n;i++)
{
getline(cin,prods[i]);
}
cout<<"Enter the number of Terminals : ";
cin>>n2;
cin.ignore();
cout<<"Enter the Terminals"<<endl;
for(int i=0;i<n2;i++)
{
cin>>terms[i];
}
terms[n2] = "$";
n2++;
cout<<"Enter the number of Non-Terminals : ";
P a g e 18 | 41
cin>>n1;
cin.ignore();
for(int i=0;i<n1;i++)
{
cout<<"Enter Non-Terminal : ";
getline(cin,nonterms[i]);
cout<<"Enter Leads of "<<nonterms[i]<<" : ";
getline(cin,leads[i]);
cout<<"Enter Trails of "<<nonterms[i]<<" : ";
getline(cin,trails[i]);
}
cout<<"Enter the Rules (exit to stop)"<<endl;
string rule = "";
while(rule != "exit")
{
getline(cin,rule);
if(rule[0] == '1')
{
int row = getPosition(terms,rule.substr(2,1),n2);
int column = getPosition(terms,rule.substr(4,1),n2);
op_table[row][column] = '=';
}
if(rule[0] == '2')
{
int ntp = getPosition(nonterms,rule.substr(4,1),n1);
int row = getPosition(terms,rule.substr(2,1),n2);
for(int j=0;j<leads[ntp].size();j++)
{
int col = getPosition(terms,leads[ntp].substr(j,1),n2);
op_table[row][col] = '<';
}
}
if(rule[0] == '3')
{
int col = getPosition(terms,rule.substr(4,1),n2);
int ntp = getPosition(nonterms,rule.substr(2,1),n1);
for(int j=0;j<trails[ntp].size();j++)
{
int row = getPosition(terms,trails[ntp].substr(j,1),n2);
op_table[row][col] = '>';
}
}
}
for(int j=0;j<leads[0].size();j++)
{
int col = getPosition(terms,leads[0].substr(j,1),n2);
op_table[n2-1][col] = '<';
}
for(int j=0;j<trails[0].size();j++)
{
int row = getPosition(terms,trails[0].substr(j,1),n2);
op_table[row][n2-1] = '>';
}
P a g e 19 | 41
cout<<endl;
cout<<"Grammar"<<endl;
for(int i=0;i<n;i++)
{
cout<<prods[i]<<endl;
}
//Display Table
for(int j=0;j<n2;j++)
cout<<"\t"<<terms[j];
cout<<endl;
for(int i=0;i<n2;i++)
{
cout<<terms[i]<<"\t";
for(int j=0;j<n2;j++)
{
cout<<op_table[i][j]<<"\t";
}
cout<<endl;
}
//Parsing String
char c;
do{
string ip;
deque<string> op_stack;
op_stack.push_back("$");
cout<<"Enter the string to be parsed : ";
getline(cin,ip);
ip.push_back('$');
cout<<"Stack\ti/p Buffer\tRelation\tAction"<<endl;
while(true)
{
for(int i=0;i<op_stack.size();i++)
cout<<op_stack[i];
cout<<"\t";
cout<<ip<<"\t";
int row = getPosition(terms,op_stack.back(),n2);
int column = getPosition(terms,ip.substr(0,1),n2);
if(op_table[row][column] == '<')
{
op_stack.push_back("<");
op_stack.push_back(ip.substr(0,1));
ip = ip.substr(1);
cout<<"\t"<<"<\t\tPush";
}
else if(op_table[row][column] == '=')
{
op_stack.push_back("=");
op_stack.push_back(ip.substr(0,1));
ip = ip.substr(1);
cout<<"\t"<<"=\t\tPush";
}
else if(op_table[row][column] == '>')
{
P a g e 20 | 41
string last;
do
{
op_stack.pop_back();
last = op_stack.back();
op_stack.pop_back();
}while(last != "<");
cout<<"\t"<<">\t\tPop";
}
else
{
if(ip[0] == '$' && op_stack.back() == "$")
{
cout<<"\t\t\tAccept\nString Parsed."<<endl;
break;
}
else
{
cout<<endl<<"String cannot be Parsed."<<endl;
break;
}
}
cout<<endl;
}
cout<<"Continue?(Y/N) ";
cin>>c;
cin.ignore();
}while(c=='y' || c=='Y');
return 0;
}
P a g e 21 | 41
8. Consider following grammar
S → DF | AF | DB | AB
X→ AY | BY | a | b
P a g e 22 | 41
Y→ AY | BY | a | b,
D→ AX
F→ BX
A→ a
B→ b
Write the C/C++/Java/Python program using the CYK algorithm to recognize the
strings produced by the above grammar.
CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char input[100];
int i,l;
main()
{
printf("\nRecursive descent parsing for the following grammar\n");
printf("\nE->TE'\nE'->+TE'/@\nT->FT'\nT'->*FT'/@\nF->(E)/ID\n");
printf("\nEnter the string to be checked:");
gets(input);
if(E())
{
if(input[i+1]=='\0')
printf("\nString is accepted");
else
printf("\nString is not accepted");
}
else
printf("\nString not accepted");
getch();
}
E()
{if(T()){
if(EP())
return(1);
else
return(0);}
else
return(0);}
P a g e 23 | 41
EP()
{if(input[i]=='+')
{i++;
if(T()){
if(EP())
return(1);
else
return(0);}
else
return(0);}
else
return(1);}
T()
{if(F())
{if(TP())
return(1);
else
return(0);}
else
return(0);}
TP()
{if(input[i]=='*')
{i++;
if(F())
{if(TP())
return(1);
else
return(0);}
else
return(0);}
else
return(1);}
F()
{if(input[i]=='(')
{i++;
if(E())
{if(input[i]==')')
{i++;
return(1);}
else
return(0);}
else
return(0);}
else if(input[i]>='a'&&input[i]<='z'||input[i]>='A'&&input[i]<='Z')
{i++;
P a g e 24 | 41
return(1);}
else
return(0);}
OUTPUT:
9. Write a C/C++/ Java/Python program to implement the LR(0) and SLR(1) parser.
a. Create the LR(0) item sets .
b. Make LR(0) and SLR(1) table.
P a g e 25 | 41
c. Create the parser and call the table according to parser required.
d. Print the error also.
CODE:
P a g e 26 | 41
10. Write a program for implementing the functionalities of predictive parser for the
mini language as follows:
{int a[3],t1,t2;
T1=2;
A[0]=1;a[1]=2;a[t]=3;
T2=-( a[2]+t1*6)/(a[2]-t1);
If t2>5then
Print(t2)
Else{
Int t3;
T3=99;
T2=25;
Print(-t1+t2*t3);/*this is a comment on 2 lines*/
}endif
}
CODE:
#include<stdio.h>
#include<conio.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];
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);
}
void main()
{
int i,j,k;
clrscr(); for(i=0;i<5;i++) for(j=0;j<6;j++) strcpy(table[i][j]," ");
printf("\nThe following is the predictive parsing table for the following grammar:\n");
for(i=0;i<7;i++)
printf("%s\n",prod[i]); printf("\nPredictive parsing table is\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++)
P a g e 27 | 41
{
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");
}
getch();
}
INPUT & OUTPUT:
The following is the predictive parsing table for the following grammar: S->A
A->Bb
A->Cd
B->aB
B->@
C->Cc
C->@
Predictive parsing table is
abcd$
S → T S’
S’ → +S | ε
T → F T’
T’ → T | ε
F → P F’
F’ → *F | ε
P → (S) | x | y | ep
CODE:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int count, n = 0;
P a g e 29 | 41
int main(int argc, char **argv)
{
int jm = 0;
int km = 0;
int i, choice;
char c, ch;
count = 13;
int kay;
char done[count];
int ptr = -1;
P a g e 30 | 41
// Checking if First of c has
// already been calculated
for(kay = 0; kay <= ptr; kay++)
if(c == done[kay])
xxx = 1;
if (xxx == 1)
continue;
// Function call
findfirst(c, 0, 0);
ptr += 1;
if (first[i] == calc_first[point1][lark])
{
chk = 1;
break;
}
}
if(chk == 0)
{
printf("%c, ", first[i]);
calc_first[point1][point2++] = first[i];
}
}
printf("}\n");
jm = n;
point1++;
}
P a g e 31 | 41
printf("\n");
printf("-----------------------------------------------\n\n");
char donee[count];
ptr = -1;
// Checking if Follow of ck
// has alredy been calculated
for(kay = 0; kay <= ptr; kay++)
if(ck == donee[kay])
xxx = 1;
if (xxx == 1)
continue;
land += 1;
// Function call
follow(ck);
ptr += 1;
P a g e 32 | 41
for(lark = 0; lark < point2; lark++)
{
if (f[i] == calc_follow[point1][lark])
{
chk = 1;
break;
}
}
if(chk == 0)
{
printf("%c, ", f[i]);
calc_follow[point1][point2++] = f[i];
}
}
printf(" }\n\n");
km = m;
point1++;
}
}
void follow(char c)
{
int i, j;
P a g e 33 | 41
{
// Calculate the follow of the Non-Terminal
// in the L.H.S. of the production
follow(production[i][1]);
}
}
}
}
}
P a g e 35 | 41
{
// Recursion to the next symbol
// in case we encounter a "#"
followfirst(production[c1][c2], c1, c2+1);
}
}
j++;
}
}
}
OUTPUT:
12. Convert The BNF (Backus-Naur Form) rules into YACC (Yet Another
Compiler Compiler) form and write code to generate abstract syntax tree.
CODE:
Lex<Bnf.L>
%{
#include"y.tab.h"
#include<stdio.h>
#include<string.h>
P a g e 36 | 41
int LineNo=1;
%}
identifier [a-zA-Z][_a-zA-Z0-9]* number [0-9]+|([0-9]*\.[0-9]+)
%%
main\(\) return MAIN; if return IF;
else return ELSE; while return WHILE; int |
char |
float return TYPE;
{identifier} {strcpy(yylval.var,yytext); return VAR;}
{number} {strcpy(yylval.var,yytext); return NUM;}
\< |
\> |
\>= |
\<= |
== {strcpy(yylval.var,yytext); return RELOP;}
[ \t] ;
\n LineNo++;
. return yytext[0];
%%
Yacc <Bnf.Y>
%{
#include<string.h> #include<stdio.h> struct quad
{
char op[5]; char arg1[10]; char arg2[10]; char result[10];
}QUAD[30];
struct stack
{
int items[100]; int top;
}stk;
int Index=0,tIndex=0,StNo,Ind,tInd; extern int LineNo;
%}
%union
{
char var[10];
}
%token <var> NUM VAR RELOP
%token MAIN IF ELSE WHILE TYPE
%type <var> EXPR ASSIGNMENT CONDITION IFST ELSEST WHILELOOP
%left '-' '+'
%left '*' '/'
%%
PROGRAM : MAIN BLOCK
;
BLOCK: '{' CODE '}'
;
CODE: BLOCK
| STATEMENT CODE
P a g e 37 | 41
| STATEMENT
;
STATEMENT: DESCT ';'
| ASSIGNMENT ';'
| CONDST
| WHILEST
;
DESCT: TYPE VARLIST
;
VARLIST: VAR ',' VARLIST
| VAR
;
ASSIGNMENT: VAR '=' EXPR{
strcpy(QUAD[Index].op,"="); strcpy(QUAD[Index].arg1,$3); strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,$1); strcpy($$,QUAD[Index++].result);
};
EXPR: EXPR '+' EXPR {AddQuadruple("+",$1,$3,$$);}
| EXPR '-' EXPR {AddQuadruple("-",$1,$3,$$);}
| EXPR '*' EXPR {AddQuadruple("*",$1,$3,$$);}
| EXPR '/' EXPR {AddQuadruple("/",$1,$3,$$);}
| '-' EXPR {AddQuadruple("UMIN",$2,"",$$);}
| '(' EXPR ')' {strcpy($$,$2);}
| VAR
| NUM
;
CONDST: IFST{
Ind=pop(); sprintf(QUAD[Ind].result,"%d",Index); Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
}
| IFST ELSEST
;
IFST: IF '(' CONDITION ')' {
strcpy(QUAD[Index].op,"=="); strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"FALSE"); strcpy(QUAD[Index].result,"-1"); push(Index);
Index++;
}
BLOCK {
strcpy(QUAD[Index].op,"GOTO"); strcpy(QUAD[Index].arg1,"");
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,"-1");
push(Index); Index++;
};
ELSEST: ELSE{
tInd=pop(); Ind=pop(); push(tInd);
sprintf(QUAD[Ind].result,"%d",Index);
} BLOCK{
P a g e 38 | 41
Ind=pop(); sprintf(QUAD[Ind].result,"%d",Index);
};
CONDITION: VAR RELOP VAR {AddQuadruple($2,$1,$3,$$); StNo=Index-1;
}
| VAR
| NUM
;
WHILEST: WHILELOOP{
Ind=pop(); sprintf(QUAD[Ind].result,"%d",StNo); Ind=pop();
sprintf(QUAD[Ind].result,"%d",Index);
};
WHILELOOP: WHILE '(' CONDITION ')' {
strcpy(QUAD[Index].op,"=="); strcpy(QUAD[Index].arg1,$3);
strcpy(QUAD[Index].arg2,"FALSE"); strcpy(QUAD[Index].result,"-1"); push(Index);
Index++;
}
BLOCK {
strcpy(QUAD[Index].op,"GOTO"); strcpy(QUAD[Index].arg1,"");
strcpy(QUAD[Index].arg2,"");
strcpy(QUAD[Index].result,"-1"); push(Index);
Index++;
};
%%
extern FILE *yyin;
}
yyparse();
printf("\n\n\t\t ----------------------------\n\t\t Pos Operator Arg1 Arg2
Result\n\t\t--------------------
");
for(i=0;i<Index;i++)
{
printf("\n\t\t %d\t %s\t %s\t %s\t
%s",i,QUAD[i].op,QUAD[i].arg1,QUAD[i].arg2,QUAD[i].result);
}
printf("\n\t\t "); printf("\n\n");
return 0;
}
P a g e 39 | 41
void push(int data)
{s tk.top++;
if(stk.top==100)
{
printf("\n Stack overflow\n"); exit(0);
}s tk.items[stk.top]=data;
} int pop()
{ int data; if(stk.top==-1)
{
printf("\n Stack underflow\n"); exit(0);
}
while(a<b)
{a=a+b;} if(a<=b)
{c=a-b;} else
{c=a+b;}
}
OUTPUT:
P a g e 40 | 41
<<< GOOD LUCK>>>
P a g e 41 | 41