0% found this document useful (0 votes)
17 views6 pages

CD Practical5

The document discusses the structure of a YACC program with an example. It describes the files, compiling process, structure including declarations, rules, and programs sections. It also includes code for the lex and yacc files to implement a calculator.

Uploaded by

hatpatel9696
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)
17 views6 pages

CD Practical5

The document discusses the structure of a YACC program with an example. It describes the files, compiling process, structure including declarations, rules, and programs sections. It also includes code for the lex and yacc files to implement a calculator.

Uploaded by

hatpatel9696
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/ 6

2CEIT701: COMPILER DESIGN PRACTICAL-5

Practical-5

AIM: Understand the structure of the YACC program with example.

Theory:
File Content
calc.lex Specifies the lex command specification file that defines the lexical
analysis rules.
calc.yacc Specifies the yacc command grammar file that defines the parsing
rules, and calls the yylex subroutine created by the lex command to
provide input.

Compiling the program:


yacc -d calc.yacc Process the yacc grammar file using the -d optional flag (which
informs the yacc command to create a file that defines the tokens
used in addition to the C language source code).
y.tab.c The C language source file that the yacc command created for the
parser.
y.tab.h A header file containing define statements for the tokens used by
the parser.
lex filename.lex

lex.yy.c The C language source file that the lex command created for the
lexical analyzer.
cc y.tab.c lex.yy.c Compile and link the two C language source files

y.tab.o The object file for the y.tab.c source file

lex.yy.o The object file for the lex.yy.c source file

a.out The executable program file

Structure:
The file contains the following sections:

• Declarations section. This section contains entries that:


o Include standard I/O header file
o Define global variables
o Define the list rule as the place to start processing
o Define the tokens used by the parser
o Define the operators and their precedence
o Ex:-

%{
code_segment
%}

21012022019 Page 12 of 17 JOSHI KARAN: 7B-5


2CEIT701: COMPILER DESIGN PRACTICAL-5

• Rules section. The rules section defines the rules that parse the input stream.

Keyword Description
%left Identifies tokens that are left-associative with other tokens.
Ex: -
%left '+' '-'
%left '*' '/'
%nonassoc Identifies tokens that are not associative with other tokens.
%right Identifies tokens that are right-associative with other tokens.
%start Identifies a nonterminal name for the start symbol.
%token Identifies the token names that the yacc command accepts. Declares all token
names in the declarations section.
Ex:- %token [<Tag>] Name [Number] [Name [Number]]...
%type Identifies the type of nonterminals. Type-checking is performed when this
construct is present.
%union Identifies the yacc value stack as the union of the various type of values
desired. By default, the values returned are integers. The effect of this construct
is to provide the declaration of YYSTYPE directly from the input.
% start Specifies that the whole input should match stat.
%%
code
Write your grammar rule and action of it.
segment
%%

• Programs section. The programs section contains the following subroutines. Because
these subroutines are included in this file, you do not need to use the yacc library when
processing this file.

Subroutine Description
main The required main program that calls the yyparse subroutine to
start the program.
ex:- main()
{
code_segment
}
yyerror(s) This error-handling subroutine only prints a syntax error message.
yywrap The wrap-up subroutine that returns a value of 1 when the end of
input occurs.

21012022019 Page 13 of 17 JOSHI KARAN: 7B-5


2CEIT701: COMPILER DESIGN PRACTICAL-5

Code:
calc.lex
%{
#include <stdio.h>
#include "y.tab.h"
int c;
%}
%%
"";
[a-z] {c = yytext[0];yylval.a = c - 'a';return(LETTER);}
[0-9] {c = yytext[0];yylval.a = c - '0';return(DIGIT);}
[^a-z0-9\b] {c = yytext[0];return(c);}
%%

calc.yacc
%{
#include<stdio.h>
int regs[26];
int base;
%}

%start list

%union { int a; }

%token DIGIT LETTER

%left '|'
%left '&'
%left '+' '-'
%left '*' '/' '%'
%left UMINUS /*supplies precedence for unary minus */

%% /* beginning of rules section */

list: /*empty */
|
list stat '\n'
|
list error '\n'
{
yyerrok;
}
;

21012022019 Page 14 of 17 JOSHI KARAN: 7B-5


2CEIT701: COMPILER DESIGN PRACTICAL-5

stat: expr
{
printf("%d\n",$1);
}
|
LETTER '=' expr
{
regs[$1.a] = $3.a;
}
;

expr: '(' expr ')'


{
$$ = $2;
}
|
expr '*' expr
{

$$.a = $1.a * $3.a;


}
|
expr '/' expr
{
$$.a = $1.a / $3.a;
}
|
expr '%' expr
{
$$.a = $1.a % $3.a;
}
|
expr '+' expr
{
$$.a = $1.a + $3.a;
}
|
expr '-' expr
{
$$.a = $1.a - $3.a;
}
|
expr '&' expr
{
$$.a = $1.a & $3.a;
}
|

21012022019 Page 15 of 17 JOSHI KARAN: 7B-5


2CEIT701: COMPILER DESIGN PRACTICAL-5

expr '|' expr


{
$$.a = $1.a | $3.a;
}
|

'-' expr %prec UMINUS


{
$$.a = -$2.a;
}
|
LETTER
{
$$.a = regs[$1.a];
}

|
number
;

number: DIGIT
{
$$ = $1;
base = ($1.a==0) ? 8 : 10;
} |
number DIGIT
{
$$.a = base * $1.a + $2.a;
}
;

%%
int main()
{
return(yyparse());
}

int yyerror(s)
char *s;
{
fprintf(stderr, "%s\n",s);
}

int yywrap()
{
return(1);
}

21012022019 Page 16 of 17 JOSHI KARAN: 7B-5


2CEIT701: COMPILER DESIGN PRACTICAL-5

Output:

21012022019 Page 17 of 17 JOSHI KARAN: 7B-5

You might also like