8
Accept a n b n LEX PROGRAM %{ #include "y.tab.h" %} %% { "\n" {return E; } a {return A;} b {return B;} } %% int yywrap() { } YACC PROGRAM %{ #include<stdio.h> #include<stdlib.h> void yyerror(char*); %} %token A B E %% P:S E { printf("matching"); exit(0); } ; S:A S B |A B ; %% main() { printf("enter the string:"); yyparse(); } void yyerror(char *p) { printf("mismatch"); }

Lex and Yacc

Embed Size (px)

DESCRIPTION

hai

Citation preview

Accept anb

n

LEX PROGRAM

%{

#include "y.tab.h"

%}

%%

{

"\n" {return E; }

a {return A;}

b {return B;}

}

%%

int yywrap()

{

}

YACC PROGRAM

%{

#include<stdio.h>

#include<stdlib.h>

void yyerror(char*);

%}

%token A B E

%%

P:S E { printf("matching"); exit(0); }

;

S:A S B |A B

;

%%

main()

{

printf("enter the string:");

yyparse();

}

void yyerror(char *p)

{

printf("mismatch");

}

Syntax of for loop

LEX PROGRAM

id[a-zA-Z0-9]

op "<"|">"|"<="|"<="|">="|"=="|"!="

i "++"|"--"|"+"|"-"|"*"|"/"

eq "="

ob "("

cb ")"

sem ";"

keyword "for"

%{

#include "y.tab.h"

%}

%%

{id} { return ID; }

{op} { return OP; }

{i} { return I; }

{ob} { return OB; }

{cb} { return CB; }

{sem} { return SEM; }

{keyword} { return F; }

{eq} { return EQ; }

"\n" { return E; }

%%

yywrap()

{

}

YACC PROGRAM

%{

#include<stdio.h>

#include<stdlib.h>

void yyerror(char *);

%}

%token OP I CB OB SEM ID F E EQ

%%

P:S E {printf("matching\n"); exit(0);}

;

S:F OB ID EQ ID SEM ID OP ID SEM ID I CB|

F OB ID EQ ID SEM ID OP ID SEM I ID CB|

F OB SEM ID OP ID SEM ID I CB|

F OB SEM ID OP ID SEM I ID CB

;

%%

main()

{

printf("enter the expression:\n");

yyparse();

}

void yyerror(char *p)

{

printf("mismatch\n");

}

Accept anb

nc

md

m

LEX PROGRAM

%{

#include"y.tab.h"

%}

%%

{

a { return A; }

b { return B; }

c { return C; }

d { return D; }

\n { return E; }

}

%%

int yywrap()

{

}

YACC PROGRAM

%{

#include<stdio.h>

#include<stdlib.h>

void yyerror(char *);

%}

%token A B C D E

%%

P: S E { printf("accept\n"); exit(0); }

;

S: M N|M|N

;

M: A M B|A|B

;

N: C N D|C|D

;

%%

main()

{

printf("enter the expression:");

yyparse();

}

void yyerror(char *p)

{

printf("mismatch");

}

Relational Expression

LEX PROGRAM

letters [a-z A-Z 0-9]

op “<”| “>”| “=”

%{

#include “y.tab.h”

%}

%%

{

{letters}* { return ID; }

{op} { return OP; }

}

%%

int yywrap()

{

}

YACC PROGRAM

%{

#include<stdio.h>

void yyerror( char *);

%}

% token ID OP

%%

S : ID OP ID { printf(“correct”);

;

%%

main()

{

printf(“enter the expression:”);

yyparse();

}

void yyerror( char *p)

{

printf(“mismatch”);

}

Accept anc

md

mb

n

LEX PROGRAM

%{

#include"y.tab.h"

%}

%%

{

a { return A; }

b { return B; }

c { return C; }

d { return D; }

\n { return E; }

}

%%

int yywrap()

{

}

YACC PROGRAM

%{

#include<stdio.h>

#include<stdlib.h>

void yyerror(char *);

%}

%token A B C D E

%%

P: S E { printf("accept\n"); exit(0); }

;

S: A M B| A S B| A B| M

;

M: C M D| C D

;

%%

main()

{

printf("enter the expression:");

yyparse();

}

void yyerror(char *p)

{

printf("mismatch");

}

Integer calculator

LEX PROGRAM

op "+"|"-"|"*"|"/"

%{

#include <stdio.h>

#include "y.tab.h"

%}

%%

{op} { return *yytext; }

[0-9]* { yylval=atoi(yytext); return NUM; }

\n { return N; }

"(" { }

")" { }

%%

int yywrap()

{

}

YACC PROGRAM

%{

#include<stdio.h>

#include<stdlib.h>

void yyerror(char *);

%}

%token NUM N

%left '+' '-'

%left '*' '/'

%left '(' ')'

%%

S:E N { printf("result:%d\n",$1); exit(0); }

;

E:E '+' E { $$=$1+$3; }

|E '-' E { $$=$1-$3; }

|E '*' E { $$=$1*$3; }

|E '/' E { $$=$1/$3; }

|'(' E ')' { $$=$2; }

|NUM { $$=$1; }

;

%%

main()

{

printf("enter the expression:\n");

yyparse();

}

void yyerror(char *p)

{

printf("invalid\n");

}