Sie sind auf Seite 1von 11

Program to find vowel or consonant

%{
#include<stdio.h>
%}
%%
[aeiou] printf("%s is a vowel",yytext);
[qwertypsdfghjklmnbvcxz] printf("%s is a consonant",yytext);
%%
LEXEMES
%{
#include<stdio.h>
%}
%%
[a-zA-Z_]+ printf(" \n%s is a lexeme:", yytext);
[0-9]+ printf(" \n%s is a lexeme ", yytext);
[a-zA-Z0-9_]+ printf("\n%s is a lexeme: ", yytext);
[+|-|*|/|!|=|<|>][=] printf("\n%s is a lexeme: ",yytext);
[!|@|$|^|&|*|(|)|{|}|+|-|=|\|~|:|?|<|>|'%'|'#'|"|/|.|,|;] printf("\n%s is a lexeme: ", yytext);
[+][+] printf("\n%s is a lexeme: ", yytext);
[-][-] printf("\n%s is a lexeme: ", yytext);
[>][>] printf("\n%s is a lexeme: ", yytext);
[<][<] printf("\n%s is a lexeme: ", yytext);
[|][|] printf("\n%s is a lexeme: ", yytext);
[<][>] printf("\n%s is a lexeme: ", yytext);
[&][&] printf("\n%s is a lexeme: ", yytext);
\n ;
%%

TOKENS
%{
#include<string.h>
int i1=0;
%}
%%
[/][/][a-zA-Z0-9|!|@|$|^|&|*|(|)|{|}|+|-|=|\|~|:|?|<|>|'%'|'#'|"|/|.|,|;]+ ;
[/][*][a-zA-Z0-9|!|@|$|^|&|*|(|)|{|}|+|-|=|\|~|:|?|<|>|'%'|'#'|"|/|.|,|;]+[*][/] ;
(int|float|return|double|char|char\*|cout|cin|while|for|if|else|do|main)
printf("\n%s\t<keyword,%s>",yytext,yytext);
["] printf("\n%s\t<quotes>",yytext);
["][a-zA-Z0-9|!|@|$|^|&|*|(|)|{|}|+|-|=|\|~|:|?|<|>|'%'|'#'|"|/|.|,|; ]*["]
printf("\n%s\t<literal,%s>",yytext,yytext);
[_a-zA-z][a-zA-z0-9_]* printf("\n%s\t<id,%d>",yytext,i1++);
[0-9]* printf("\n%s\t<id,%d>",yytext,i1++);
[,] printf("\n%s\t<comma>",yytext);
[+|-|*|/|!|=|<|>][=] printf("\n%s\t<operator,%s>",yytext,yytext);

[<>*/=+-:|&] printf("\n%s\t<operator,%s>",yytext,yytext);
[+][+] printf("\n%s\t<operator,%s>",yytext);
[-][-] printf("\n%s\t<operator,%s>",yytext);
[<][<] printf("\n%s\t<operator,%s>",yytext);
[>][>] printf("\n%s\t<operator,%s>",yytext);
[<][>] printf("\n%s\t<operator,%s>",yytext);
[|][|] printf("\n%s\t<operator,%s>",yytext);
[&][&] printf("\n%s\t<operator,%s>",yytext);
[()] printf("\n%s\t<parentheses>",yytext);
[{}] printf("\n%s\t<curlybraces>",yytext);
[;] printf("\n%s\t<punctuator>",yytext);
%%
SYMBOL TABLE
%{
#include<string.h>
int i1=0;
%}
%%
[/][/][a-zA-Z0-9|!|@|$|^|&|*|(|)|{|}|+|-|=|\|~|:|?|<|>|'%'|'#'|"|/|.|,|;]+ ;
[/][*][a-zA-Z0-9|!|@|$|^|&|*|(|)|{|}|+|-|=|\|~|:|?|<|>|'%'|'#'|"|/|.|,|;]+[*][/] ;
(int|float|return|double|char|char\*|cout|cin|while|for|if|else|do|main) {
printf("\n%s\t<keyword,%s>",yytext,yytext);
func1(yytext,"keyword");
}
["] {
printf("\n%s\t<quotes>",yytext);
func1(yytext,"quotes");
}
["][a-zA-Z0-9|!|@|$|^|&|*|(|)|{|}|+|-|=|\|~|:|?|<|>|'%'|'#'|"|/|.|,|; ]*["] {
printf("\n%s\t<literal,%s>",yytext,yytext);
func1(yytext,"literal");
}
[_a-zA-z][a-zA-z0-9_]* {
printf("\n%s\t<id,%d>",yytext,i1++);
func1(yytext,"id");
}
[0-9]* {printf("\n%s\t<id,%d>",yytext,i1++);
func1(yytext,"id");
}
[+|-|*|/|!|=|<|>][=] {
printf("\n%s\t<operator,%s>",yytext,yytext);
func1(yytext,"operator");
}
[+][+] {printf("\n%s\t<operator>",yytext);
func1(yytext,"operator");
}
[<>*/=+-:|&] {

printf("\n%s\t<operator,%s>",yytext,yytext);
func1(yytext,"operator");
}
[()] {
printf("\n%s\t<parentheses>",yytext);
func1(yytext,"parentheses");
}
[{}] {
printf("\n%s\t<curlybraces>",yytext);
func1(yytext,"curlybraces");
}
[;] {
printf("\n%s\t<semicolon>",yytext);
func1(yytext,"semicolon");
}
[,] {printf("\n%s\t<comma>",yytext);
func1(yytext,"comma");
}
\n func2();
%%
struct sym
{
char lex[20];
char tok[20];
}ob[20];
int i=0;
func1(char a[], char b[])
{
int j,flag=0;
for(j=0;j<i;j++)
{
if(strcmp(ob[j].lex,a)==0)
{
flag=1;
break;
}
}
if(flag==0)
{
strcpy(ob[i].lex,a);
strcpy(ob[i].tok,b);
i++;
}
}
func2()
{
int k;

printf("\n\n-------SYMBOL TABLE----");
for(k=0;k<i;k++)
{
printf("\n%s",ob[k].lex);
printf(" \t%s",ob[k].tok);
}
}

CALCULATOR
cal.l
%{
#define YYSTYPE double
#include "y.tab.h"
#include <stdlib.h>
%}
digit [0-9]
integer {digit}+
real {integer}("."{integer})?
%%
{real} { yylval=atof(yytext);
return NUMBER;
}
"+" return PLUS;
"-" return MINUS;
"*" return TIMES;
"/" return DIVIDE;
"^" return POWER;
"(" return LEFT;
")" return RIGHT;
"\n" return END;
Cal.y
%{
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define YYSTYPE double
%}
%token NUMBER
%token PLUS MINUS TIMES DIVIDE POWER
%token LEFT RIGHT
%token END

%left PLUS MINUS


%left TIMES DIVIDE
%left NEG
%right POWER
%start Input
%%
Input:
| Input Line
;
Line:
END
| Expression END { printf("Result: %f\n", $1); }
;
Expression:
NUMBER { $$=$1; }
| Expression PLUS Expression { $$=$1+$3; }
| Expression MINUS Expression { $$=$1-$3; }
| Expression TIMES Expression { $$=$1*$3; }
| Expression DIVIDE Expression { $$=$1/$3; }
| MINUS Expression %prec NEG { $$=-$2; }
| Expression POWER Expression { $$=pow($1,$3); }
| LEFT Expression RIGHT { $$=$2; }
;
%%
int yyerror(char *s) {
printf("%s\n", s);
}
int yywrap()
{
return 1;
}
int main() {
yyparse();
}

SHIFT REDUCE PARSING


#include<stdio.h>
#include<string.h>
int i,l,shft=0;
char str[10],stk[10];
void print()
{
int j;
printf("\nReduce\t%s\t",stk);
for(j=shft;j<l;j++)
printf("%c",str[j]);
}
int main()
{
int n=0,c,j,flag;
printf("\nEnter the input string");
scanf("%s", str);
l=strlen(str);
stk[n]='$';
str[l]='$';
l=l+1;
for(i=0;i<l-1;i++)
{
n++;
stk[n]=str[i];
stk[n+1]='\0';
printf("\nShift\t%s\t",stk);
shft+=1;
for(j=shft;j<l;j++)
printf("%c",str[j]);
c=0;
while(c<=i){
c++;
if(stk[n]=='d')
{
stk[n]='C';
print();
}
else if(stk[n-1]=='C' && stk[n]=='C')
{
stk[n-1]='S';
stk[n]='\0';
n--;
print();
}
else if(stk[n-1]=='c' && stk[n]=='C')
{

stk[n-1]='C';
stk[n]='\0';
n--;
print();
}
}}
if(stk[1]=='S'&& stk[2]=='\0')
printf("\nAccepted");
else
printf("\nNot Accepted");
}

FIRST
#include<stdio.h>
#include<ctype.h>
void FIRST(char);
int n=0;
struct s{
char prodn[10],first;
}ob[10];

int main()
{
int i;
char c;
printf("How many productions ? :");
scanf("%d",&n);
printf("Enter productions epsilon= $ :");
for(i=0;i<n ;i++)
scanf("%s",ob[i].prodn);
for(i=0;i<n ;i++)
{
c=ob[i].prodn[0];
FIRST(c);
}
for(i=0;i<n;i++)
printf("FIRST(%c) = {%c} ",ob[i].prodn[0],ob[i].first);
return 0;
}
void FIRST(char c)
{
static int m=0;
int j;
for(j=0;j<n;j++)

{
if(ob[j].prodn[0]==c)
{
if(ob[j].prodn[2]=='$')
ob[m++].first='$';
else if(!(isupper(ob[j].prodn[2])))
ob[m++].first=ob[j].prodn[2];
else FIRST(ob[j].prodn[2]);
}
}
}

3 address code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
main()
{
int n;
char a[10];
printf("enter the string");
scanf("%s",a);
n=strlen(a);
if(n==3)
{
if(a[1]=='=')
{
printf("t1= %c \n",a[2]);
printf("%c=t1\n",a[0]);
}
else printf("\n try again");
}
if(n==5)
{
if(a[3]=='*'||a[3]=='/'||a[3]=='-'||a[3]=='+')
{
if(a[1]=='=')
{
printf("t1=%c %c %c \n",a[2],a[3],a[4]);
printf("%c=t1 \n",a[0]);
}
else printf("\n try again");

}
}
if(n==6 && a[1]=='=')
{
if(a[2]=='-')
{
printf("t1= %c %c \n",a[2],a[3]);
printf("t2= t1 %c %c \n ",a[4],a[5]);
printf("%c=t2 \n",a[0]);
}
else if(a[4]=='-')
{
printf("t1=%c %c \n",a[4],a[5]);
printf("t2=%c %c t1 \n",a[2],a[3]);
printf("%c=t2 \n",a[0]);
}
}
if (n==7 && a[1]=='=')
{
if(a[5]=='*'||a[5]=='/')
{
printf("t1=%c %c %c \n",a[4],a[5],a[6]);
printf("t2=%c %c t1 \n",a[2],a[3]);
printf("%c=t2 \n",a[0]);
}
else if (a[3]=='*'||a[3]=='/'||a[3]=='-'||a[3]=='+')
{
printf("t1=%c %c %c \n",a[2],a[3],a[4]);
printf("t2=t1 %c %c \n",a[5],a[6]);
printf("%c=t2 \n",a[0]);
}
}
return 0;
}

FOLLOW
#include<stdio.h>
#include<string.h>
int n,m=0,p,i=0,j=0;
char a[10][10],f[10];
void follow(char c);
void first(char c);
int main()

{
int i,z;
char c,ch;
printf("Enter the no.of productions:");
scanf("%d",&n);
printf("Enter the productions(epsilon=$):\n");
for(i=0;i<n;i++)
scanf("%s%c",a[i],&ch);
do
{
m=0;
printf("Enter the element whose FOLLOW is to be found:");
scanf("%c",&c);
follow(c);
printf("FOLLOW(%c) = { ",c);
for(i=0;i<m;i++)
printf("%c ",f[i]);
printf(" }\n");
printf("Do you want to continue(0/1)?");
scanf("%d%c",&z,&ch);
}
while(z==1);
}
void follow(char c)
{
if(a[0][0]==c)f[m++]='$';
for(i=0;i<n;i++)
{
for(j=2;j<strlen(a[i]);j++)
{
if(a[i][j]==c)
{
if(a[i][j+1]!='\0')first(a[i][j+1]);
if(a[i][j+1]=='\0'&&c!=a[i][0])
follow(a[i][0]);
}
}
}
}
void first(char c)
{
int k;
if(!(isupper(c)))f[m++]=c;

for(k=0;k<n;k++)
{
if(a[k][0]==c)
{
if(a[k][2]=='$') follow(a[i][0]);
else if(islower(a[k][2]))f[m++]=a[k][2];
else first(a[k][2]);
}
}
}

Das könnte Ihnen auch gefallen