Sie sind auf Seite 1von 10

SIMPLE CALCULATOR

%{
int op = 0,i;
float a, b;
%}

dig [0-9]+|([0-9]*)"."([0-9]+)
add "+"
sub "-"
mul "*"
div "/"
pow "^"
ln \n
%%

/* digi() is a user defined function */


{dig} {digi();}
{add} {op=1;}
{sub} {op=2;}
{mul} {op=3;}
{div} {op=4;}
{pow} {op=5;}
{ln} {printf("\n The Answer :%f\n\n",a);}

%%
digi()
{
if(op==0)

/* atof() is used to convert


- the ASCII input to float */
a=atof(yytext);

else
{
b=atof(yytext);

switch(op)
{
case 1:a=a+b;
break;

case 2:a=a-b;
break;

case 3:a=a*b;
break;
case 4:a=a/b;
break;

case 5:for(i=a;b>1;b--)
a=a*i;
break;
}
op=0;
}
}

main(int argv,char *argc[])


{
yylex();
}

yywrap()
{
return 1;
}

PALLIANDROME OR NOT
%
{
int i, j, flag;
%
}

/* Rule Section */
%%
[a - z A - z 0 - 9]*
{
for (i = 0, j = yyleng - 1; i <= j; i++, j--) {
if (yytext[i] == yytext[j]) {
flag = 1;
}
else {
flag = 0;
break;
}
}
if (flag == 1)
printf("Given string is Palindrome");
else
printf("Given string is not Palindrome");
}
%%

// driver code
int main()
{
printf("Enter a string :");
yylex();
return 0;
}

int yywrap()
{
return 1;
}

SIMPLE CALCULATOR USING YACC


%{
/* 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;
}
Parser Source Code :

%{
/* 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 which
can have operations Addition,
Subtraction, Multiplication, Divison,
Modulus and Round brackets:\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;
}
PROGRAM TO RECOGNIZE A VALID
VARIABLE WHICH STARTS WITH A LETTER
FOLLOWED BY ANY NUMBER OF LETTERS
OR DIGITS

LEX PART:

%{

#include "y.tab.h"

%}

%%

[a-zA-Z_][a-zA-Z_0-9]* return letter;

[0-9] return digit;

. return yytext[0];

\n return 0;

%%

int yywrap()

return 1;

YACC PART:

%{

#include<stdio.h>

int valid=1;

%}

%token digit letter


%%

start : letter s

s: letter s

| digit s

%%

int yyerror()

printf("\nIts not a identifier!\n");

valid=0;

return 0;

int main()

printf("\nEnter a name to tested for identifier ");

yyparse();

if(valid)

printf("\nIt is a identifier!\n");

%{
/* 4a.l Yacc Program to check the validity of an arithmetic Expression that uses operators +, -, *, /

*/

#include "y.tab.h"

%}

%%

[0-9]+(\.[0-9]+)? { return NUM;}

[a-zA-Z_][_a-zA-Z0-9]* { return ID; }

[\t] ;

\n return 0;

. return yytext[0];

%%

yywrap()

{}

view rawssos4a.l hosted with ❤ by GitHub

ARITHMETIC EXPRESSION VALID OR NOT


YACC:

%{

/* 4a.y Yacc Program to check the validity of an arithmetic

Expression that uses operators +, -, *, /

*/

#include<stdio.h>

#include<stdlib.h>

%}

%token NUM ID

%left '+' '-'

%left '*' '/'

%%

e : e '+' e
| e '-' e

| e '*' e

| e '/' e

| '('e')'

| NUM

| ID ;

%%

main()

printf(" Type the Expression & Press Enter key\n");

yyparse();

printf(" Valid Expression \n");

yyerror()

printf(" Invalid Expresion!!!!\n"); exit(0);

EVALUATE ARITHMETIC EXP:


%
{
/* Definition section*/
#include "y.tab.h"
extern yylval;
%
}

%%
[0 - 9]
+
{
yylval = atoi(yytext);
return NUMBER;
}

[a - zA - Z] + { return ID; }
[\t] + ;
\n { return 0; }
. { return yytext[0]; }

%%
Parser Source Code:

%{

/* Definition section */
#include
%

% token NUMBER ID
// setting the precedence
// and associativity of operators
% left '+' '-'
% left '*' '/'

/* Rule Section */
%
%E:T
{

printf("Result = %d\n", $$);


return 0;

T : T '+' T { $$ = $1 + $3; }
| T '-' T { $$ = $1 - $3; }
| T '*' T { $$ = $1 * $3; }
| T '/' T { $$ = $1 / $3; }
| '-' NUMBER { $$ = -$2; }
| '-' ID { $$ = -$2; }
| '(' T ')' { $$ = $2; }
| NUMBER { $$ = $1; }
| ID { $$ = $1; };
%%

int main()
{
printf("Enter the expression\n");
yyparse();
}

/* For printing error messages */


int yyerror(char* s)
{
printf("\nExpression is invalid\n");
}
/*Lex program to take check whether
the given number is even or odd */

%{
#include<stdio.h>
int i;
%}

%%

[0-9]+ {i=atoi(yytext);
if(i%2==0)
printf("Even");
else
printf("Odd");}
%%

int yywrap(){}

/* Driver code */
int main()
{

yylex();
return 0;
}

Das könnte Ihnen auch gefallen