0% found this document useful (0 votes)
89 views8 pages

Lex and Yacc

The document describes the LEX and YACC programs to implement an integer calculator that can evaluate basic arithmetic expressions involving addition, subtraction, multiplication and division operators. The LEX program defines tokens for numeric values and operators. The YACC program defines grammar rules for expressions and uses operator precedence to evaluate expressions. It prints the result of evaluating the entered expression.

Uploaded by

Ganga Sreekumar
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)
89 views8 pages

Lex and Yacc

The document describes the LEX and YACC programs to implement an integer calculator that can evaluate basic arithmetic expressions involving addition, subtraction, multiplication and division operators. The LEX program defines tokens for numeric values and operators. The YACC program defines grammar rules for expressions and uses operator precedence to evaluate expressions. It prints the result of evaluating the entered expression.

Uploaded by

Ganga Sreekumar
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/ 8

Accept anbn

LEX PROGRAM
%{
#include "y.tab.h"
%}
%%
{
"\n" {return E; }
a {return A;}
b {return B;}
}
%%
int yywrap()
{
}
YACC PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char*);
%}
%token A B E
%%
P:S E { printf("matching"); exit(0); }
;
S:A S B |A B
;
%%
main()
{
printf("enter the string:");
yyparse();
}
void yyerror(char *p)
{
printf("mismatch");
}

Syntax of for loop


LEX PROGRAM
id[a-zA-Z0-9]
op "<"|">"|"<="|"<="|">="|"=="|"!="
i "++"|"--"|"+"|"-"|"*"|"/"
eq "="
ob "("
cb ")"
sem ";"
keyword "for"
%{
#include "y.tab.h"
%}
%%
{id} { return ID; }
{op} { return OP; }
{i} { return I; }
{ob} { return OB; }
{cb} { return CB; }
{sem} { return SEM; }
{keyword} { return F; }
{eq} { return EQ; }
"\n" { return E; }
%%
yywrap()
{
}

YACC PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *);
%}
%token OP I CB OB SEM ID F E EQ
%%
P:S E {printf("matching\n"); exit(0);}
;
S:F OB ID EQ ID SEM ID OP ID SEM ID I CB|
F OB ID EQ ID SEM ID OP ID SEM I ID CB|
F OB SEM ID OP ID SEM ID I CB|
F OB SEM ID OP ID SEM I ID CB
;
%%
main()
{
printf("enter the expression:\n");
yyparse();
}
void yyerror(char *p)
{
printf("mismatch\n");
}

Accept anbncmdm
LEX PROGRAM
%{
#include"y.tab.h"
%}
%%
{
a { return A; }
b { return B; }
c { return C; }
d { return D; }
\n { return E; }
}
%%
int yywrap()
{
}
YACC PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *);
%}
%token A B C D E
%%
P: S E { printf("accept\n"); exit(0); }
;
S: M N|M|N
;
M: A M B|A|B
;
N: C N D|C|D
;
%%
main()
{
printf("enter the expression:");
yyparse();
}
void yyerror(char *p)
{
printf("mismatch");
}

Relational Expression
LEX PROGRAM
letters [a-z A-Z 0-9]
op <| >| =
%{
#include y.tab.h
%}
%%
{
{letters}* { return ID; }
{op} { return OP; }
}
%%
int yywrap()
{
}
YACC PROGRAM
%{
#include<stdio.h>
void yyerror( char *);
%}
% token ID OP
%%
S : ID OP ID { printf(correct);
;
%%
main()
{
printf(enter the expression:);
yyparse();
}
void yyerror( char *p)
{
printf(mismatch);
}

Accept ancmdmbn
LEX PROGRAM
%{
#include"y.tab.h"
%}
%%
{
a { return A; }
b { return B; }
c { return C; }
d { return D; }
\n { return E; }
}
%%
int yywrap()
{
}
YACC PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *);
%}
%token A B C D E
%%
P: S E { printf("accept\n"); exit(0); }
;
S: A M B| A S B| A B| M
;
M: C M D| C D
;
%%
main()
{
printf("enter the expression:");
yyparse();
}
void yyerror(char *p)
{
printf("mismatch");
}

Integer calculator
LEX PROGRAM
op "+"|"-"|"*"|"/"
%{
#include <stdio.h>
#include "y.tab.h"
%}
%%
{op} { return *yytext; }
[0-9]* { yylval=atoi(yytext); return NUM; }
\n { return N; }
"(" { }
")" { }
%%
int yywrap()
{
}

YACC PROGRAM
%{
#include<stdio.h>
#include<stdlib.h>
void yyerror(char *);
%}
%token NUM N
%left '+' '-'
%left '*' '/'
%left '(' ')'
%%
S:E N { printf("result:%d\n",$1); exit(0); }
;
E:E '+' E { $$=$1+$3; }
|E '-' E { $$=$1-$3; }
|E '*' E { $$=$1*$3; }
|E '/' E { $$=$1/$3; }
|'(' E ')' { $$=$2; }
|NUM { $$=$1; }
;
%%
main()
{
printf("enter the expression:\n");
yyparse();
}
void yyerror(char *p)
{
printf("invalid\n");
}

You might also like