0% found this document useful (0 votes)
7 views12 pages

Compiler Design

Uploaded by

Swastik Sharma
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)
7 views12 pages

Compiler Design

Uploaded by

Swastik Sharma
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
You are on page 1/ 12

PROGRAM-1

AIM : Practice of LEX/YACC of compiler writing.


THEORY : Lex is a tool or a computer program that generates Lexical
Analyzers (converts the stream of characters into tokens). The Lex
tool itself is a compiler. The Lex compiler takes the input and
transforms that input into input patterns. It is commonly used with
YACC(Yet Another Compiler Compiler). It was written by Mike Lesk
and Eric Schmidt.
Yacc (for “yet another compiler compiler.”) is the standard parser
generator for the Unix operating system. An open source program,
yacc generates code for the parser in the C programming language.
The acronym is usually rendered in lowercase but is occasionally seen
as YACC.
CODE :
1) LEX Program to count number of vowels and consonants
%{
int vow_count=0;
int const_count =0;
%}
%%
[aeiouAEIOU] {vow_count++;}
[a-zA-Z] {const_count++;}
%%
int yywrap(){}
int main()
{
printf("Enter the string of vowels and consonants:");
yylex();
printf("Number of vowels are: %d\n", vow_count);
printf("Number of consonants are: %d\n", const_count);
return 0;
}

2) YACC program to implement a Calculator and recognize a valid


Arithmetic expression.
%{
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
%%
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;}
;
%%
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");
}
void yyerror()
{
printf("\nEntered arithmetic expression is Invalid\n\n");
flag=1;
}

OUTPUT :
1)
2)
PROGRAM-2
AIM : Write a program to check whether a string belongs to the
grammar or not.
THEORY : The steps involved in checking whether a string belongs to
a Grammar or not, are as follows:
Start with the Start Symbol and choose the closest production that
matches the given string.
Replace the Variables with their most appropriate production. Repeat
the process until the string is generated or until no other productions
are left.
1.1) S -> aS
S -> Sb
S -> ab
String of the form : aab
1.2) S -> aSa
S -> bSb
S -> a
S -> b
The Language generated is : All Odd Length Palindromes
1.3) S -> aSbb
S -> abb
The Language generated is : anb2n , where n>1
1.4) S -> aSb
S -> ab
The Language generated is : anbn, where n>0
CODE :
#include<iostream>
using namespace std;
int main(){
int inp;
cout<<"Enter number 1 for grammar 1"<<endl;
cout<<"Enter number 2 for grammar 2"<<endl;
cout<<"Enter number 3 for grammar 3"<<endl;
cout<<"Enter number 4 for grammar 4"<<endl;
cin>>inp;
if(inp==1){
cout<<"Enter a string: ";
char str[10];
cin>>str;
if(str[0]!='a')
{ cout<<"String is invalid because of incorrect first character";
exit(0); }
int n=1;
while(str[n]=='a') n++;
if ( str[n] != 'b')
{ cout<<"string does not belong to grammar";
exit(0); }
n++;
while (str[n]=='b') n++;
if (str [n] != '\0')
{ cout<<"String does not belong to grammar";
exit(0);}
cout<<"string is valid";
}
else if(inp==2){
char str[10];
cout<<"Enter a string: ";
cin>>str;
int c=0;
int i=0;
while(str[i]!='\0'){
c+=1;
i+=1;
}
if(c%2==0){
cout<<"string does not belong to grammar\n";
exit(0);
}
else{
int s=0,e=c-1;
while(s<e){
if(str[s]==str[e]){
s=s+1;
e=e-1;
}
else{
cout<<"string does not belong to grammar\n";
exit(0);
}
}
cout<<"String belongs to grammar\n";
}
}
else if(inp==3){
char str[10];
cout<<"Enter a string: ";
cin>>str;
if(str[0]!='a'){
cout<<"string does not belong to grammar\n";
exit(0);
}
int ca=0,cb=0,i=0;
while(str[i]=='a'){
ca+=1;
i=i+1;
}
while(str[i]=='b'){
cb+=1;
i=i+1;
}
if(str[i]!='\0'){
cout<<"string does not belong to grammar\n";
exit(0);
}
else{
if(cb==2*ca){
cout<<"string belongs to grammar\n"; }
else{
cout<<"string does not belong to grammar\n"; }
}
}
else if(inp==4){
char str[10];
cout<<"Enter a string: ";
cin>>str;
if(str[0]!='a'){
cout<<"string does not belong to grammar\n";
exit(0);
}
int ca=0,cb=0,i=0;
while(str[i]=='a'){
ca+=1;
i=i+1;
}
while(str[i]=='b'){
cb+=1;
i=i+1;
}
if(str[i]!='\0'){
cout<<"string does not belong to grammar\n";
exit(0); }
else{
if(cb==ca){
cout<<"string belongs to grammar\n"; }
else{
cout<<"string does not belong to grammar\n";
}
}
}
return 0;
}

OUTPUT :
PROGRAM-3
AIM : Write a program to check whether a string includes a keyword
or not.
THEORY : We break the given string into tokens and check each
word if its a keyword of C++ language. Keywords are predefined
words, we cannot name variables as keywords, there are 32
keywords in C++. We print the count of keywords present in the
string.
CODE :
#include <bits/stdc++.h>
using namespace std;
void simple_tokenizer(string s)
{
stringstream ss(s);
string word;
map<string,int> m;
string
key[32]={"auto","break","case","char","const","continue","default","do","doub
le","else","enum","extern","float","for","goto","if","int","long","register","retur
n","short","signed","sizeof","static","struct","switch","typedef","union","unsig
ned","void","volatile","while"};
while (ss >> word) {
for(int i=0;i<32;i++){
if(key[i] == word){
m[word]+=1;
}
}
}
if(!m.empty()){
for (auto i = m.begin(); i != m.end(); i++)
cout << i->first << " " << i->second << endl;
}
else{
cout<<"No keyword is present"<<endl; }
}
int main()
{
string a;
cout<<"enter a string"<<endl;
getline(cin,a);
simple_tokenizer(a);
cout << endl;
cout<<"Swastik Sharma 039";
return 0; }

OUTPUT :

You might also like