0% found this document useful (0 votes)
5 views15 pages

Lab Record

The document contains source code for a simple arithmetic expression parser and calculator written in C. It includes code for lexical analysis using Lex and parsing using Yacc, handling input expressions, and validating them. The program outputs whether the input expression is valid and calculates the result if it is.

Uploaded by

tl21btcs0361
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)
5 views15 pages

Lab Record

The document contains source code for a simple arithmetic expression parser and calculator written in C. It includes code for lexical analysis using Lex and parsing using Yacc, handling input expressions, and validating them. The program outputs whether the input expression is valid and calculates the result if it is.

Uploaded by

tl21btcs0361
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/ 15

-~ Ii /Jh I

L----
\ g I \
.. >
I

(2~P l'1.. .c.~


i----

,'),s ,,t~ <fkt: f'.hJ ,..t.~0 -- oF l.l"-'' =°J wofY )J ~~D Utft l)..~ ~
i--
.
tN J\ N 1 w f.)ul Tr-}Q T
i-----
.
i---

At,-.,,
..........

i--
,--

w-J 'k ~ ,~ h.!Vf) '.....~ .\or)


I I,
}-o cl.~ ~
I
~ ,_ . •~ l'
l .1·-.,
• , 9{_ (nu
~ a,J.. ~t\i' 1-i a..., ~· "
- J i~if
I

111. p A,-.J-t'\.. "'-"\


~

0 - \ I I
'

,,
J \
;'\
'J
J
,~ kvY
ll~
1~~~l
Y\...(_

~
~\--
~

lO~
I
I

~(_ ,~ ,~
\

--hv
' I

I
,i

W'3-f"d..A.
I I '.

, L'--'u -kc
I I

1~ .
'
I l

I
I.\ s~ h--t I c..l "O~ . • L,v~C::U . L\ t<A ~ '
0
,
~1
I

S11vft.\ov\tJ-- ~ "'(01J I, L\,-{_


- .I ~- I
I
r I '
/ \
t '
,, I
I
G\ t?l~ .. ~ V\.!t ~- I , l
/ I I
;J\ -
}to.o .
J I
I ' ' ' I \(
'

'
' ' I ,,
~N t.5
~ ~ ~~ MJ _l) ~ w. ~~ ()~ ~.u l ~
' "

-
,_ '
"
r---

r---

r---

----
D
--- ~
SOURCE CODE
%{
#include<stdio.h>
int sc = 0, we = 0, le = 0, cc =0;
%}

%%
[\n] { le++; we++; }
[\t] { sc++; we++; }
l" ·1 {sc++; we++;}
[A\n\t ]+ {cc+= yyleng; }
%%
I
\ ~oid mainQ

I\ printfC-Enter the input: \n");


yylexQ;
printf("The number of lines = %d\n",lc);
printf("The number of spaces = %d\n",sc);
printf("The number of words = %d\n",wc);
printf("The number of characters= %d\n",cc);
i
}

I
int yywrapQ
{
I
return 1;
}

S cd sradha -coMp i.ler


S lex di.s.l
S cc lex.yy .c -lf.
Enter the i.nput:
S ./a.ou t
Thi.s i.s coMpi.ler lab
The nuMber of li.nes = 1
The nuMber of spaces = 3
The nuMber of words= 4
The nuM ber of charac ters= 17
ll I
l)i~j

Au-<

, ky } ' I
a, /Vl,U J~. ' \

~dl ~ ~ 1--o
I

I I .

~ . ( I
CL t

nrlt.. M- ~
\J&-14,

,, I ..
I I ( I I ' ' / I (. . ~ • ' ' • (
$)URGE oureYI
Enter the expression: 12+8*60
Valid Expression!
~upexe
SQURCE coD.E
Enter the expression: a+-*
//L£XCODE
Invalid Expression!
I %{
#include "y.tab.h.
%}
[0-9)+ {return NUMBER;}
!
1 [a-zA-2) [a-zA-Z0-9)* {return ID;}
, \n {return 0;}
\ . {return yytext[0];}
1
i %%
I: int yywrapO
I { •

return 1;
}
//YACC CODE
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token NUMBER ID
%left '+·_•.,,./'
%%
Exp : exp'+'exp
I exp'-'exp
I exp'....,exp
I exp'/'exp
I NUMBER
I ID;
%%
int mainQ
{
printf("Enter the expres . . •
yyparseQ; . s1on. );

} printf("Valid Expression!\n");

void yyerrorQ
{ .

printtrlnvalid Expression!\n");
exit(0);
~\ /o ~ I~ I l )_ I

lti"y u1

'hJ(e

I \ I
I , I
I
\ l
1 ,'/ '
VALID VARIABLE
} SOURCE CODE SOURCE OUTPUT
//LEX CODE Enter the expression: Good
j %{ Valid expression!
( #include •y.tab.h•
%} Enter the expression: 12as
} %% Invalid expression!
[0-9] {return DIGIT;}
[a-zA-Z] {return ALPHA;}
\n {return O;}
. {return yytext[0];}
%%
//YACC CODE
%{
#include<stdio.h>
#include<stdlib.h>
%}
%token DIGIT ALPHA
%%
var: ALPHA
I var ALPHA
I var DIGIT;
%%
int mainQ
{
printf("Enter a variable name:\t");
yypars~O;
11 printf('Valid variable!\n");
l return 0;
I }
int yyerrorO
{
printf("lnvalid Variable!\n");
exit(1 );
}
int yywrapQ
{
return 1;
}
/.> • / , / :ii. I •~. I

w~

lt1

h.J Y1 U- ·

I '
ARITHMETIC CALCULATOR {
SOURCE CODE printf("Error: %s",s);
IILEXCODE }
%{ SOURCE OUTPUT
1 1'incfude"y.tab.h" Enter the expression: 5+10*(9)
#indude<stdio. h> Answer= 95
extern int yytval;
%}
%%
, (0-9]+ {yylval = atoi(yytext); return
NUMBER;}
. {return yytext{0];}
(\tJ+ ;
\n {return O;}
%%
//YACC CODE
%{
#incfude<stdio.h>
%}
%token NUMBER
%left '+' '-'
%left,.. 'f
%%
st: exp {printf {"Answer= %d\n",$$);}

exp : exp'+'exp {$$ = $1 + $3;}


I exp'-'exp {$$ = $1 - $3;}
I exp'-'exp {$$ = $1 * $3;}
I exp'fexp {$$ = $1 / $3;}
I '('exp')' {$$ = $2;}
I NUMBER {$$ = $1 ;}

%%
int main()
I
j {
printf("Enter the expression:\t");
l yyparse();

I }
return O;

int yywrap()
l {
}
return O;
}
yyerror(char *s)
- -c..--~ -- - - - - - - - - - - - - -- - --
0:J t\f=LK ~ : Y ~ 1 a bp-- ~v, ~vt-u k.~ ~ 1?

t~

i-t e

C?. hv~
~ ~ r< t "'--- ,l ~.
. ' \ I ,, \ •

h" y ~ V\4..w V\011 -~0 ?.. a,J ~f'1 N.lr l


Lw .~ .
rl.. t.. • I '
11 it\...

yv l-tt ~ h'o~ , I
'

aLi
t/Jl Wt
" ~ 4~ ,1 Q.J qr-
~ ~~I r \.J;{ ,• '
?.. , ' ,- ti\ ol .

l
~/_ _/_ I [ l.fo. \

I I

I I
'
'

~ w ~ ~k~ ...
~
\~
~ \

\ I
', I

. \
I
'I \
'
I I

I ' \
\ \
\
\ I I
' \. '

I
\ I I
' \
\
I
~
'\
\ I I \
\
' '
I \
\ \ I

~
I
1\ ' \ I
, t 1\ \
\ ( ,, \ \
\ \ \ I
I
I \
"\ \ \ \
1
~

I
\ I
\
\

1\ \ \\
I

I'
\ \
\
1
\
\
§OURCE CODE
I
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char input[100);
int i = 0, error = O;
void E();
void T();
void Eprime();
void Tprime();
void F();
int main()
{
printf("Enter an arithmetic expression: \n");
if (fgets(input, sizeof(input), stdin) == NULL) {
printf("lnput error!\n");
return 1; }
size_t len = strlen(input);
I if (len > 0 && input[len - 1] == '\n')
input[len - 1] = '\O';
{
}
I E();
!I if (strlen(input) == i && error== 0)
printf("\nAccepted I! !\n");
else ·
prin~("\nRejected!!!\n");
return O;
}
void E() {
T();
Eprime(); }

void Eprime() {
if (input[i] == '+') {
i++;
T();
Eprime(); }}

void T() {
F();
Tprime(); }

void Tprime() {
if (input[i] == '*') {
i++;
void F() {
If (lnput[i]== '(') (
i++· I

E() ;
if (input[i]== ')')
i++·I

else
error= 1; }
else if (isalnum(input[i]) II input[i] == '_') {
i++·
I

while (isalnum(input[i]) II input[i] == '_')


i++; }
else {
error= 1; } }

Enter an artt hMetlc expression:


su rri+t. nterest

Acceptedl ! !
• ,- : 1 , ', i. ~
.. ! i t . 1...t •_;;- ?~
t.~ .-5 : \
> .{.:...; .
• $ • /a. out
Enter an artthrrietlc expresslon:
suM+avg*+lnterest

Rejected! ! !
S: . I a .ou
Enter an artth~ettc expresslon: t. '
suM+avg•tnterest

Accepted!!!
' I
'_ -
I
.
I I.
I'. J ' ) -
\ ·.
~ .j t .
l I .,..I
·1 (.
.. V
l •
e
I I
~

~~.
[ I / I ~

~.
~

. I
~J ~ I I ' I I

-
I

A 1. ,~ 1~
~

0 -
i1./ lo .•r-·.. L-4- t(L {f (>( l \ l\.J ~ - ~, ,
~\ )l }
pr;. ' )<_ ~ ~ ~ Ftf4'\
,II
°'
~ N ~ ,n~~
'
~ ~ ~ ~ {:, ~'T (x ) •
• ~f-
,~
~

~

~
I

~ ")(. '~ ½ ,} "1 2 ~- . ~ ~ i... q ~ l , J-...


K 0.. N" )
~...,
'
I I
. I

y ~
ttA.o\ ft(4 T ('-H) I ~ f l l--<'1 (_>-.J ft 11\.-(1 ~'Y 'ffD Q~

~ h ~(J ( h_ rLf 1 <I• I


"
I
I
' I I I

OF; wt--'t
rvuu.l--• L,\ ~ ~~ .
~ I , ., - •
.,k .
~ ~u .J- u I

' I
I I 'I I

I I
I
I ·/

I I
'I
'' I

I '
I I i
( I
I
I I
I
I

I
t
I (
I

( Jr . _
I
t
'

I It it
I

I •/
- I

- '
SOURCE CODE-FIRSI
#include <stdio.h>
#include <ctype.h>
#include <string.h>
c);
void FIRST(char res0, char
val);
void result(char resD, char
int nop;
char prod[10][1 OJ;
int mainO
{
int i;
char choice;
char c;
char res[20];
productions?: ");
printf(''How many number of
scanf{"%d", &nop);
strings like E->E+ T\n");
printf("Enter the production
{
for(i = O; i < nop;·i++)
er %d: i + 1);
11

printf("Enter production Numb


,

. scanf{"%s", prod[i]); }
do { 11

printf("\nFind the FIRST of:


);

scanf(" %c", &c);


memset(res, '\O', sizeof{res));
FIRST{res, c);
printf("\nFIRST{%c) = { c);
11
,

for{i = O; res[i) != '\O'; i++) ·


printf(" }\n");
");
printf{"Press 'y' to continue:
scant(" %c", &choice);
}
= 'Y');
while (choice== 'y' II choice=
return O;
}
void FIRST(char res □, char c)
{
int i, j, k;
char subres[20);

I
· int eps = O;
if (!isupper{c)) {
result(res, c);
return; }

You might also like