0% found this document useful (0 votes)
69 views32 pages

Yacc

Yacc is a tool that generates a parser given a context-free grammar specification. It uses production rules to define a grammar and actions to perform when grammar rules are recognized. An example grammar is provided that recognizes arithmetic expressions using production rules to define expressions, terms, and factors with semantic values ($1, $3, etc.) to pass values between the different rules. Lex and Yacc are used together in an example calculator program, with Lex handling lexical analysis and Yacc generating a parser from a grammar specification to recognize arithmetic expressions.

Uploaded by

Mohammed Hezam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views32 pages

Yacc

Yacc is a tool that generates a parser given a context-free grammar specification. It uses production rules to define a grammar and actions to perform when grammar rules are recognized. An example grammar is provided that recognizes arithmetic expressions using production rules to define expressions, terms, and factors with semantic values ($1, $3, etc.) to pass values between the different rules. Lex and Yacc are used together in an example calculator program, with Lex handling lexical analysis and Yacc generating a parser from a grammar specification to recognize arithmetic expressions.

Uploaded by

Mohammed Hezam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Yacc

yacc
a tool for automatically generating a parser given a grammar
• Production Rules:
%%
production1 : symbol1 symbol2 … { action }
symbol3 symbol4 … { action } |
…|
Production2 : symbol1 symbol2 { action }
%%
Example

%%
statement : expression { printf (" = %g\n", $1); }
expression : expression '+' expression { $$ = $1 + $3; }
| expression '-' expression { $$ = $1 - $3; }
| NUMBER { $$ = $1; }
%%
According these two productions,
5 + 4 – 3 + 2 is parsed into:
Defining Values
expr : expr '+' term { $$ = $1 + $3; }
| term { $$ = $1; }
;
term : term '*' factor { $$ = $1 * $3; }
| factor { $$ = $1; }
;
factor : '(' expr ')' { $$ = $2; }
| ID
| NUM
;
• $1
• expr : expr '+' term { $$ = $1 + $3; }
• | term { $$ = $1; }
• ;
• term : term '*' factor { $$ = $1 * $3; }
• | factor { $$ = $1; }
• ;
• factor : '(' expr ')' { $$ = $2; }
• | ID
• | NUM
• ;
Example 1:

Write a Program to recognize the grammar (anb,


n>=10)
Lex part:- file : first.l

{%
"include"y.tab.h#
}%
%%
};return A{ ]aA[
};return B{ ]bB[
n {return NL;}\
};return yytext[0]{ .
%%
Yacc part : file second.y
{%
>include<stdio.h#
}%
token A B NL%
%%
stmt : A A A A A A A A A A s B NL
{
;printf("Valid\n")
;exit(0)
}
;
s:sA
|
;
%%
Yacc part : file second.y
int yyerror(char *msg)
{
;printf("Invalid String\n")
;exit(0)
}
int main(void)
{
;printf("Enter the String\n")
;)(yyparse
int yywrap(void) }
{
;return 0
}
Compilation :
• Compiling & running the yacc program

lex first.l
yacc −d second.y
cc lex.yy.c y.tab.c
a.out/.
Example 2
Implementation of calculator using LEX and YACC
{%

>include<stdio.h#

"include "y.tab.h#

;extern int yylval

}%
%%

{ +]0-9[

;yylval=atoi(yytext)

;return NUMBER

; ]t\[

;return 0 ]n\[

;return yytext[0] .

%%
int yywrap()

return 1;

}
YACC PART:
%{

#include<stdio.h>

int flag=0;
%}

%token NUMBER
%%

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;}

%%
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;

You might also like