0% found this document useful (0 votes)
92 views3 pages

YACC Program To Evaluate An Express

The document describes writing a YACC program to evaluate simple mathematical expressions. It provides details on the procedure, sample lex and yacc programs, and examples of running the program and its output for different expressions.

Uploaded by

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

YACC Program To Evaluate An Express

The document describes writing a YACC program to evaluate simple mathematical expressions. It provides details on the procedure, sample lex and yacc programs, and examples of running the program and its output for different expressions.

Uploaded by

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

8.

Problem Statement: YACC Program to evaluate an expression (Simple


calculator)

AIM: To write a ‘YACC’ Program to evaluate an expression

DESCRIPTION:

Each translation rule input to YACC has a string specification that resembles a
production of a grammar-it has a nonterminal on the LHS and a few alternatives on
the RHS. For simplicity, we will refer to a string specification as a production. YACC
generates an LALR(1) parser for language L from the productions, which is a bottom-
up parser. The parser would operate as follows: For a shift action, it would invoke the
scanner to obtain the next token and continue the parse by using that token. While
performing a reduced action in accordance with production, it would perform the
semantic action associated with that production.
The semantic actions associated with productions achieve the building of an
intermediate representation or target code as follows:

 Every nonterminal symbol in the parser has an attribute.


 The semantic action associated with a production can access attributes of
nonterminal symbols used in that production–a symbol “$n’ in the semantic
action, where n is an integer, designates the attribute of the nonterminal symbol
in the RHS of the production and the symbol ‘$$’ designates the attribute of the
LHS nonterminal symbol of the production.
 The semantic action uses the values of these attributes for building the
intermediate representation or target code.

A parser generator is a program that takes as input a specification of a syntax and


produces as output a procedure for recognizing that language. Historically, they are also
called compiler compilers. YACC (yet another compiler-compiler) is an LALR(1)
(LookAhead, Left-to-right, Rightmost derivation producer with 1 lookahead token)
parser generator. YACC was originally designed for being complemented by Lex.
PROCEDURE:

Write lex program in a file file.l and yacc in a file file.y


1. Open Terminal and Navigate to the Directory where you have saved the files.
2. type lex file.l
3. type yacc file.y
4. type cc lex.yy.c y.tab.h -ll
5. type ./a.out

SOURCE CODE :

Lex program “ wk-8.l ” :

%{
/* Definition section*/
#include "y.tab.h"
extern yylval;
%}

%%
[0-9]+ {
yylval = atoi(yytext);
return NUMBER;
}

[a-zA-Z]+ { return ID; }


[ \t]+ ; /*For skipping whitespaces*/

\n { return 0; }
. { return yytext[0]; }

%%

YACC program “ wk-8.y ”:


%{
/* Definition section */
#include <stdio.h>
%}

%token NUMBER ID
%left '+''-'
%left '*''/'

%%
E:T {
printf("Result = %d\n", $$);
return 0;
}

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();
}
int yyerror(char* s)
{
printf("\n Expression is invalid\n");
}
Input-Output:
$ lex wk-8.l
$ yacc -d wk-8.y
$ cc lex.yy.c y.tab.c -ll
$ ./a.out
Enter the expression
2*(5+3)/2
Result = 8

$ ./a.out
Enter the expression
12/((5-2)*(-8+6))
Result = -2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy