Untitled
unknown
plain_text
2 years ago
4.2 kB
4
Indexable
--------------------------------------------INDEX---------------------------------------------------------------------------
EXP3 - 15
EXP 4 - 46
EXP 5 - 83
EXP 6 - 109
EXP 7 - 178
EXP 8 - 242
--------------------------------------------------------------------------- experiment 3 --------------------------------------------------------------------------------------
%{
#include<stdio.h>
int chars = 0;
int word = 0;
int space = 0;
int lines = 0;
%}
%%
[^ \n\t]+ {word++,chars+=yyleng;}
\t {space++;}
" " {space++;}
\n {lines++;}
%%
int yywrap(){ return 1; }
void main(){
yylex();
printf("char: %d\nword: %d\nspace: %d\nlines: %d\n", chars,word,space,lines);
}
---------------------------------------------------------------------------------------exp4-----------------------------------------------------------------------------
%{
#include<stdio.h>
#include<string.h>
int i;
%}
%%
[a-z A-Z]* {
for(i=0;i<=yyleng;i++){
if((yytext[i]=='a')&&(yytext[i+1]=='b')&&(yytext[i+2]=='c')){
yytext[i]='A';
yytext[i+1]='B';
yytext[i+2]='C';}}
printf("%s",yytext);
}
[\t]* return 0;
.* {ECHO;}
\n {printf("%s",yytext);}
%%
void main(){
yylex();}
int yywrap(){
return 1;}
---------------------------------------------------------------------------------------------exp5------------------------------------------------------------------------------------
%{
#include<stdio.h>
int vow=0,con=0;
%}
%%
[aeiouAEIOU] {vow++;}
[a-zA-Z] {con++;}
%%
int main(){
printf("Enter input string... at end press ^d :\n");
yylex();
printf("Number of vowels=%d\n",vow);
printf("Number of consonants=%d\n",con);
}
int yywrap() {return 1;}
------------------------------------------------------------------------------------------------exp6-------------------------------------------------------------------------------------------------
-----lex-----
%{
#include "y.tab.h"
%}
%%
[a-zA-Z_][a-zA-Z_0-9]* return id;
[0-9]+(\.[0-9]*)? return num;
[+/*] return op;
. return yytext[0];
\n return 0;
%%
int yywrap()
{
return 1;
}
-----yacc-------
%{
#include<stdio.h>
int valid = 1;
%}
%token num id op
%%
start : id '=' s ';'
s: id x
| num x
| '-' num x
| '(' s ')' x
;
x: op s
| '-'s
|
;
%%
int yyerror(){
valid=0;
printf("\nInvalid expression!\n");
return 0;
}
int main(){
printf("\nEnter the expression:\n");
yyparse();
if(valid){
printf("\nValid expression!\n");}
}
------------------------------------------------------------------------------------------------------exp7-----------------------------------------------------------------------------------
-----------lex------------------
%{
#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-----------
%{
#include<stdio.h>
int valid = 1;
%}
%token digit letter
%%
start : letter s
s : letter s
| digit s
|
;
%%
int yyerror()
{
printf("\nIts not an identifier!\n");
valid=0;
return 0;
}
int main()
{
printf("\nEnter a string: ");
yyparse();
if(valid)
{
printf("\nIt is an identifier!\n");
}
}
-----------------------------------------------------------------------------------exp8-------------------------------------------------------------------------------------------------------------
-------lex------------
%{
#include<stdio.h>
#include "y.tab.h";
extern int yylval;
%}
%%
[0-9]+ {
yylval=atoi(yytext);
return NUMBER;
}
[\t];
[\n] return 0;
. return yytext[0];
%%
int yywrap(){
return 1;
}
-----yacc-------
%{
#include<stdio.h>
int flag=0;
%}
%token NUMBER
%left '+''-'
%left '*' '/' '%'
%left '(' ')'
%%
ArithemeticExpression: E{
printf("\nResult=%d\n",$$);
return 0;
};
E:E'+'E{$$=$1+$3;}
E:E'-'E{$$=$1-$3;}
E:E'*'E{$$=$1*$3;}
E:E'/'E{$$=$1/$3;}
E:E'%'E{$$=$1%$3;}
|'('E')' {$$=$2;}
| NUMBER {$$=$1;}
;
%%
void yyerror()
{
printf("\n ENTER THE ARITHEMATIC EXPRESSION IS INVALID\n\n");
flag=1;
}
void main()
{
printf("\n ENTER THE ARITHEMATIC EXPRESSION:\n");
yyparse();
if(flag==0)
printf("\n ENTER THE ARITHEMATIC EXPRESSION IS VALID\n\n");
}
Editor is loading...
Leave a Comment