Lab Record
Lab Record
L----
\ g I \
.. >
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--
,--
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
'
' ' 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
int yywrapQ
{
I
return 1;
}
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",$$);}
%%
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 ,, \ •
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
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
y ~
ttA.o\ ft(4 T ('-H) I ~ f l l--<'1 (_>-.J ft 11\.-(1 ~'Y 'ffD Q~
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
. scanf{"%s", prod[i]); }
do { 11
I
· int eps = O;
if (!isupper{c)) {
result(res, c);
return; }