Beruflich Dokumente
Kultur Dokumente
%{
int op = 0,i;
float a, b;
%}
dig [0-9]+|([0-9]*)"."([0-9]+)
add "+"
sub "-"
mul "*"
div "/"
pow "^"
ln \n
%%
%%
digi()
{
if(op==0)
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;
}
}
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;
}
/* 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
/* 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"
%}
%%
. return yytext[0];
\n return 0;
%%
int yywrap()
return 1;
YACC PART:
%{
#include<stdio.h>
int valid=1;
%}
start : letter s
s: letter s
| digit s
%%
int yyerror()
valid=0;
return 0;
int main()
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"
%}
%%
[\t] ;
\n return 0;
. return yytext[0];
%%
yywrap()
{}
%{
*/
#include<stdio.h>
#include<stdlib.h>
%}
%token NUM ID
%%
e : e '+' e
| e '-' e
| e '*' e
| e '/' e
| '('e')'
| NUM
| ID ;
%%
main()
yyparse();
yyerror()
%%
[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
{
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();
}
%{
#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;
}