0% found this document useful (0 votes)
38 views24 pages

Problem 11: Design A LEX Code To Check Whether The Given Number Is Even or Odd

The document contains multiple problems with corresponding LEX and YACC code implementations for various tasks, such as checking if a number is even or odd, counting vowels and consonants, validating email addresses, and evaluating arithmetic expressions. Each problem includes a problem statement, source code, and author details. The code snippets demonstrate the use of lexical analysis and parsing techniques in programming.

Uploaded by

sv3592270
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)
38 views24 pages

Problem 11: Design A LEX Code To Check Whether The Given Number Is Even or Odd

The document contains multiple problems with corresponding LEX and YACC code implementations for various tasks, such as checking if a number is even or odd, counting vowels and consonants, validating email addresses, and evaluating arithmetic expressions. Each problem includes a problem statement, source code, and author details. The code snippets demonstrate the use of lexical analysis and parsing techniques in programming.

Uploaded by

sv3592270
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/ 24

Problem 11

Problem Statement: Design a LEX code to check whether the given number is even or odd.
Source Code:
%{
#include<stdio.h>
#include<stdlib.h>
%}
%%
[0-9]+ {int num=atoi(yytext);
if(num%2==0)
printf("%s is even\n", yytext);
else
printf("%s is odd\n", yytext);}
[A-Za-z0-9]+ {printf("%s is not a valid number\n", yytext);}
.|\n ;
%%

int main(){
printf("Eenter the string:");
yylex();
return 0;
}
int yywrap(){
return 1;
}

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 12
Problem Statement: Design a LEX Code to count number of vowels and consonants in a given
pattern.

Source Code:
%{
#include <stdio.h>
int v=0,c=0;
%}
%%
[aeiouAEIOU] {v++;}
[a-zA-Z] {c++;}
.|\n;
%%
int yywrap(){
return 1;
}
int main(){
printf("Enter a string: ");
yylex();
printf("Total number of vowels are:%d\n",v);
printf("Total number of consonants are:%d\n",c);
return 0;
}

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 13
Problem Statement: Design a LEX Code to check for a valid E-mail Id.
Source Code:
%{
#include <stdio.h>
%}

%%
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,} { printf("Valid Email: %s\n",
yytext); }
.|\n {}
%%

int main() {
printf("Enter the E-mail: ");
yylex();
return 0;
}

int yywrap() {
return 1;
}

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 14
Problem Statement: Design a DFA in LEX Code which accepts all possible set of string
containing even number of ‘a’ and even number of ‘b’ over input alphabet Σ = {a, b}.

Source Code:
%{
#include<stdio.h>
%}

%s A DEAD

%%
<INITIAL>a BEGIN A;
<INITIAL>b BEGIN INITIAL;
<INITIAL>\n printf("Accepted\n"); BEGIN INITIAL;
<INITIAL>[^ab\n] BEGIN DEAD;
<A>a BEGIN INITIAL;
<A>b BEGIN A;
<A>\n printf("Not Accepted\n"); BEGIN INITIAL;
<A>[^ab\n] BEGIN DEAD;
<DEAD>\n printf("Invalid Input\n"); BEGIN INITIAL;
<DEAD>[^\n] BEGIN DEAD;
%%

int yywrap(){
return 1;
}

int main(){
printf("Enter the input: ");
yylex();
return 0;
}

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 15
Problem Statement: Design a DFA in LEX Code to Identify and print Integer, Float Constants
and Identifier.

Source Code:
%{
#include<stdio.h>
%}
%s INT A FLOAT IDENTIFIER DEAD
%%
<INITIAL>[0-9] BEGIN INT;
<INITIAL>[a-zA-Z_] BEGIN IDENTIFIER;
<INITIAL>[^a-zA-Z0-9_\n] BEGIN DEAD;
<INITIAL>\n BEGIN INITIAL;
<INT>[0-9] BEGIN INT;
<INT>[.] BEGIN A;
<INT>[^0-9.\n] BEGIN DEAD;
<INT>\n printf("Integer\n"); BEGIN INITIAL;
<A>[0-9] BEGIN FLOAT;
<A>[^0-9\n] BEGIN DEAD;
<A>\n printf("Invalid Input\n"); BEGIN INITIAL;
<FLOAT>[0-9] BEGIN FLOAT;
<FLOAT>[^0-9\n] BEGIN DEAD;
<FLOAT>\n printf("Float\n"); BEGIN INITIAL;
<IDENTIFIER>[a-zA-Z0-9_] BEGIN IDENTIFIER;
<IDENTIFIER>[^a-zA-Z0-9_\n] BEGIN DEAD;
<IDENTIFIER>\n printf("Identifier\n"); BEGIN INITIAL;
<DEAD>[^\n] BEGIN DEAD;
<DEAD>\n printf("INVALID\n"); BEGIN INITIAL;
%%
int yywrap(){
return 1;
}
int main() {
printf("Enter a input: ");
yylex();
return 0; }

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 16
Problem Statement: Design a DFA in LEX Code over Σ = {a, b} which contains set of all possible
strings where every string starts with a and ends with b.

Source Code:
%{
#include<stdio.h>
%}
%s A B DEAD
%%
<INITIAL>a BEGIN A;
<INITIAL>[^a\n] BEGIN DEAD;
<INITIAL>\n printf("Not Accepted\n"); BEGIN INITIAL;
<A>a BEGIN A;
<A>b BEGIN B;
<A>[^ab\n] BEGIN DEAD;
<A>\n printf("Not Accepted\n"); BEGIN INITIAL;
<B>b BEGIN B;
<B>a BEGIN A;
<B>[^ab\n] BEGIN DEAD;
<B>\n printf("ACCEPTED\n"); BEGIN INITIAL;
<DEAD>[^\n] BEGIN DEAD;
<DEAD>\n printf("Invalid Input\n"); BEGIN INITIAL;
%%
int yywrap() {
return 1;
}
int main() {
printf("Enter a string:\n");
yylex();
return 0;
}

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 17
Problem Statement: Design YACC / LEX code to recognize valid arithmetic expression with
operators +,-,* and /.

YACC PART:
%{
#include <stdio.h>
int valid = 1;
int yylex();
int yyerror(const char *s);
%}

%token num id op

%%

start : id '=' s ';';

s: id x
| num x
| '-' num x

Arnav Rana G2 13
| '(' s ')' x
;

x : op s
| '-' s
|
;

%%

int yyerror(const char *s)


{
valid = 0;
printf("\nInvalid expression: %s\n", s);

Name: Tanishq Bisht University Roll: 2219803 Section: A2


return 0;
}

int main()
{
printf("\nEnter the expression:\n");
yyparse();
if (valid)
{
printf("\nValid expression!\n");
}
return 0;
}

LEX Code:
%{
#include "exp.tab.h"
%}

%%
[0-9]+ { yylval = atoi(yytext); return num; }
[a-zA-Z_][a-zA-Z0-9_]* { return id; }
[-+*/] { return op; }
"=" { return '='; }
";" { return ';'; }
"(" { return '('; }
")" { return ')'; }
[ \t\n]+ ; // Ignore whitespace
. { return yytext[0]; }

%%

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 18
Problem Statement: Design YACC / LEX code to evaluate arithmetic expression involving
operators +, -, * and / without operator precedence grammar and with operator precedence
grammar.

YACC PART:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror(const char *s);
%}

%token NUMBER
%left '+' '-'
%left '*' '/'
%left UMINUS

%%
input:
/* empty */
| input line
;

line:
'\n'
| expr '\n' { printf("= %d\n", $1); }
;

expr:
expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr {
if ($3 == 0) {
yyerror("Division by zero");
$$ = 0;
} else {

Name: Tanishq Bisht University Roll: 2219803 Section: A2


$$ = $1 / $3;
}
}
| '-' expr %prec UMINUS { $$ = -$2; }
| '(' expr ')' { $$ = $2; }
| NUMBER { $$ = $1; }
;
%%

int main() {
printf("Simple Desk Calculator (Ctrl+D to exit):\n");
yyparse();
return 0;
}

void yyerror(const char *s) {


fprintf(stderr, "Error: %s\n", s);
}
LEX Code:
%{
#include "y.tab.h"
#include <stdlib.h>
%}

%%
[0-9]+ { yylval = strdup(yytext); return NUMBER; }
[ \t]+ ;
\n { return '\n'; }
. { return yytext[0]; }
%%

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 19
Problem Statement: Design YACC / LEX code that translates INFIX Expression to POSTFIX
Expression.

YACC PART:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror(const char *s);
%}

%token NUMBER
%left '+' '-'
%left '*' '/'
%left UMINUS

%%
input:
/* empty */
| input line
;

line:
'\n'
| expr '\n' { printf("= %d\n", $1); }
;

expr:
expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr {
if ($3 == 0) {
yyerror("Division by zero");
$$ = 0;
} else {

Name: Tanishq Bisht University Roll: 2219803 Section: A2


$$ = $1 / $3;
}
}
| '-' expr %prec UMINUS { $$ = -$2; }
| '(' expr ')' { $$ = $2; }
| NUMBER { $$ = $1; }
;
%%

int main() {
printf("Simple Desk Calculator (Ctrl+D to exit):\n");
yyparse();
return 0;
}

void yyerror(const char *s) {


fprintf(stderr, "Error: %s\n", s);
}
LEX Code:
%{
#include "y.tab.h"
#include <stdlib.h>
%}

%%
[0-9]+ { yylval = strdup(yytext); return NUMBER; }
[ \t]+ ;
\n { return '\n'; }
. { return yytext[0]; }
%%

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Problem 20
Problem Statement: Design a Desk Calculator using YACC / LEX code.
YACC PART:
%{
#include <stdio.h>
#include <stdlib.h>
int yylex(void);
void yyerror(const char *s);
%}

%token NUMBER
%left '+' '-'
%left '*' '/'
%left UMINUS

%%
input:
/* empty */
| input line
;

line:
'\n'
| expr '\n' { printf("= %d\n", $1); }
;

expr:
expr '+' expr { $$ = $1 + $3; }
| expr '-' expr { $$ = $1 - $3; }
| expr '*' expr { $$ = $1 * $3; }
| expr '/' expr {
if ($3 == 0) {
yyerror("Division by zero");
$$ = 0;
} else {
$$ = $1 / $3;

Name: Tanishq Bisht University Roll: 2219803 Section: A2


}
}
| '-' expr %prec UMINUS { $$ = -$2; }
| '(' expr ')' { $$ = $2; }
| NUMBER { $$ = $1; }
;
%%

int main() {
printf("Simple Desk Calculator (Ctrl+D to exit):\n");
yyparse();
return 0;
}
void yyerror(const char *s) {
fprintf(stderr, "Error: %s\n", s);
}
LEX Code:
%{
#include <stdio.h>
%}

%%
(abb)+[b]* { printf("Valid: %s\n", yytext); }
[a-b]+ { printf("Invalid: %s\n", yytext); }
.|\n ;
%%

int main() {
printf("Enter strings over {a, b} (Ctrl+D to end):\n");
yylex();
return 0;
}

Name: Tanishq Bisht University Roll: 2219803 Section: A2


Output

Name: Tanishq Bisht University Roll: 2219803 Section: A2

You might also like