Compiler
Compiler
Compiler
#include <stdio.h>
int main()
{
char pat[50],*str ,ch;
int state = 1;
printf("enter pattern");
scanf("%s",pat);
str = pat;
while (*str)
{
/* code */
ch = *str;
switch(ch){
case '0':
if(state ==1)
state=1;
else
state=3;
break;
case '1':
if (state == 1)
state =2;
else
state = 3;
break; }
str++;
}
if (state ==2)
printf("valid");
else
printf("invalid");
}
*****************************************************
#include <stdio.h>
int main()
{
char check[50],*s,ch;
int state=0;
printf("Enter a pattern:");
scanf("%s",check);
s=check;
while(*s)
{
if(((*s>='a')&&(*s<='z'))||((*s>='A')&&(*s<='Z')))
{
if(state==0)
state=1;
else
state=1;
s=s+1;
}
else if ((*s>='0')&&(*s<='9'))
{
if(state==0)
state=0;
else
state=1;
s=s+1;
}
}
if(state==0)
printf("Invalid");
else
printf("Valid");
}
***************************************************************************
2. Write a lex program to recognise and display keyword, number and words in a
given statement.
%{
#include<stdio.h>
%}
%%
if |
else |
printf {printf("\n%s is a keyword",yytext);}
[0-9]+ {printf("\n%s is a number",yytext);}
[a-zA-Z]+ {printf("\n%s is a word",yytext);}
.|\n {ECHO;}
%%
int main()
{
printf("enter a string");
yylex();
}
int yywrap()
{
return 1;
}
*****************************************************************************
3.i) Write a lex program to display capital words from the given input string.
%{
#include<stdio.h>
%}
%%
[A-Z]+[\t\n ] { printf("%s",yytext); }
. ;
%%
int main( )
{
printf("Enter some string with capital words in between\n");
yylex();
}
int yywrap( )
{
return 1;
}
********************************************************************************
3)ii) write a program in lex to count the 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;
}
***********************************************************************************
*
4)i) write a lex code to remove the comment from any C program given at runtime and
stored into out.c file.
/*Definition Section*/
%{
#include <stdio.h>
%}
/*Rule Section*/
%%
\/\/(.*) {};
\/\*(.*\n)*.*\*\/ {};
%%
/*Auxiliary function*/
/*Driver function*/
int main()
{
yyin=fopen("comm.c","r");
yyout=fopen("out.c","w");
/*call the yylex function.*/
yylex();
return 0;
}
******************************************************************************
4)ii) write a lex program to check whether a number is Armstrong number or not.
/* Rule Section */
%%
[0-9]+ check(yytext);
%%
int main()
{
/* yyin as pointer of File type */
extern FILE* yyin;
yyin = fopen("num", "r");
return 0;
}
void check(char* a)
{
int len = strlen(a), i, num = 0;
for (i = 0; i < len; i++)
int yywrap()
{
return 1;
}
*********************************************************
%{
/* Definition section */
#include<stdio.h>
int num, r, b=0, p=1;
%}
DIGIT [0-9]
/* Rule Section */
%%
{DIGIT}+ { num=atoi(yytext);
while (num > 0)
{
r= num % 2;
b+= r*p;
p*= 10;
num/= 2;
}
printf("%d", b);
}
.|\n ECHO;
%%
// driver code
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 0;
}
*********************************************************
%{
/* Definition section */
#include<stdio.h>
int num, r, b=0, p=1;
%}
DIGIT [0-9]
/* Rule Section */
%%
{DIGIT}+ { num=atoi(yytext);
while (num > 0)
{
r= num % 2;
b+= r*p;
p*= 10;
num/= 2;
}
printf("%d", b);
}
.|\n ECHO;
%%
// driver code
int main()
{
yylex();
return 0;
}
int yywrap()
{
return 0;
}
**********************************************************************
%{
#include <stdio.h>
#include <math.h>
%}
%%
[01]+ {
int decimal = 0;
int length = yyleng;
int i;
%%
int yywrap() {
return 1;
}
int main() {
printf("Enter a binary number:\n");
yylex();
return 0;
}
***********************************************************************
6)i) write a YACC program to recognise a valid arithmetic expression that user
operator +,-,*,/
LEX:
%{
#include"y.tab.h"
%}
%%
[0-9]+ {yylval=atoi(yytext); return NUMBER;}
[a-zA-Z]+ {return ID;}
[\t]+ ;
\n {return 0;}
. {return yytext[0];}
%%
int yywrap() {
return 1;
}
YACC:
%{
#include<stdio.h>
int flag = 1;
%}
%token NUMBER ID
%left '+' '-'
%left '*' '/'
%%
expr: expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| '-'NUMBER
| '-'ID
|'('expr')'
|NUMBER
|ID
;
%%
int main()
{
printf("Enter the expression:\n");
yyparse();
if (flag)
printf("\nExpression valid");
else
printf("\nExpression invalid");
}
int yyerror(char *s)
{
flag = 0;
}
*************************************************************************
LEX:
%{
/* Definition section */
#include<stdio.h>
#include "y.tab.h"
extern int yylval;
%}
/* Rule Section */
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t] ;
[\n] return 0;
. return yytext[0];
%%
int yywrap()
{
return 1;
}
YACC:
%{
/* Definition section */
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+' '-'
%left '*' '/' '%'
%left '(' ')'
/* 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:\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;
}
************************************************************************
LEX:
%{
#include<stdio.h>
#include "y.tab.h"
%}
%%
[a-zA-Z][0-9a-zA-Z]* { return ID; }
[\t] ;
[\n] {return 0;}
. {return yytext[0];}
%%
int yywrap()
{
return 1;
}
YACC:
%{
#include<stdio.h>
int flag=0;
%}
%token ID
%%
E : E'+'T {printf("Rule 1\n");}
| T {printf("Rule 2\n");}
;
T : T'*'F {printf("Rule 3\n");}
| F {printf("Rule 4\n");}
;
F : '('E')' {printf("Rule 5\n");}
| ID {printf("Rule 6\n");}
;
%%
void main()
{
printf("Enter arithmetic expression:\n");
yyparse();
if (flag==0)
printf("Valid\n");
}
*******************************************************************************
LEX:
%{
#include "y.tab.h"
%}
%%
a return A;
b return B;
.|\n return yytext[0];
%%
int yywrap()
{
return 1;
}
YACC:
%{
#include<stdio.h>
int valid=1;
%}
%token A B
%%
str:S'\n' {return 0;}
S:A S B
|
;
%%
main()
{
printf("Enter the string:\n");
yyparse();
if(valid==1)
printf("\nvalid string");
}
int yyerror(char *msg)
{
printf("invalid string\n");
exit(0);
}
****************************************************************************
LEX:
%{
#include "y.tab.h"
%}
%%
"if" {return IF;}
[sS][0-9]* {return S;}
"<"|">"|"=="|"<="|">="|"!=" {return RELOP;}
[0-9]+ {return NUMBER;}
[a-z][a-zA-Z0-9_]* {return ID;}
\n {return NL;}
. {return yytext[0];}
%%
YACC:
%{
#include<stdio.h>
#include<stdlib.h>
int count=0;
%}
%token IF RELOP S NUMBER ID NL
%%
stmt: if_stmt NL {printf("No. of nested if statements=%d\n",count);exit(0);}
;
if_stmt : IF'('cond')''{'if_stmt'}' {count++;}
|S
;
cond: x RELOP x
;
x:ID | NUMBER
;
%%
int yyerror(char *msg)
{
printf("the statement is invalid\n");
exit(0);
}
main()
{
printf("enter the statement\n");
yyparse();
}