SlideShare a Scribd company logo
1 of 11
Download to read offline
Compiler Construction Sunita M. Dol, CSE Dept
Page 1
Assignment No.2
AIM:
Parser generator using YACC (Yet Another Compiler Compiler)
THEORY:
YACC
Each string specification in the input to YACC resembles a grammar production.
The parser generated by YACC performs reductions according to this grammar.
The actions associated with a string specification are executed when a reduction is
made according to the specification. An attribute is associated with every
nonterminal symbol. The value of this attribute can be manipulated during parsing.
The attribute can be given any user-designed structure. A symbol ‘$n’ in the action
part of a translation rule refers to the attribute of the nth
symbol in the RHS of the
string specification. ‘$$’ represents the attribute of the LHS symbol of the string
specification.
Example
Figure 3 shows sample input to YACC. The input consists of four components, of
which only two are shown. The routine gendesc builds a descriptor containing the
name and type of an id or constant. The routine gencode takes an operator and the
attributes of two operands, generates code and returns with the attribute
Compiler Construction
Figure 3: A sample YACC specification
Parsing of the* string b+c*d where b, c and d are
generated by YACC from the input
C routines:
Gendesc (id#1);
Gendesc (id#2);
Gendesc (id#3);
Gencode (*, [c,real], [d, real]);
Gencode (+, [b, real], [t, real]);
where an attribute has the f
used to store the result of c*d in the code generated
Compiling the Example Program
To create the desk calculator example program, do the following:
1. Process the yacc grammar file using the
yacc command to create a
C language source code):
yacc -d calc.yacc
Sunita M. Dol, CSE Dept
Figure 3: A sample YACC specification
the* string b+c*d where b, c and d are of type real, using the parser
YACC from the input of above Figure leads to following
Gencode (*, [c,real], [d, real]);
Gencode (+, [b, real], [t, real]);
where an attribute has the form <name>, <type> and t is the name
c*d in the code generated by the first call on gencode.
Compiling the Example Program
To create the desk calculator example program, do the following:
grammar file using the -d optional flag (which informs the
command to create a file that defines the tokens used in addition to the
C language source code):
Sunita M. Dol, CSE Dept
Page 2
type real, using the parser
following-calls on the
and t is the name of a location
the first call on gencode.
optional flag (which informs the
file that defines the tokens used in addition to the
Compiler Construction Sunita M. Dol, CSE Dept
Page 3
2. Use the ls command to verify that the following files were created:
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
3. Process the lex specification file:
lex calc.lex
4. Use the ls command to verify that the following file was created:
lex.yy.c
The C language source file that the lex command created for the lexical
analyzer
5. Compile and link the two C language source files:
cc y.tab.c lex.yy.c
6. Use the ls command to verify that the following files were created:
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
To run the program directly from the a.out file, type:
$ a.out
PROGRAM:
YACC program
Compiler Construction Sunita M. Dol, CSE Dept
Page 4
The following example shows the contents of the calc.yacc file. This file has
entries in all three sections of a yacc grammar file: declarations, rules, and
programs.
%{
#include <stdio.h>
int regs[26];
int base;
%}
%start list
%union { int a; }
%type <a> expr number
%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'
Compiler Construction Sunita M. Dol, CSE Dept
Page 5
{
yyerrok;
} ;
stat: expr
{
printf("%dn",$1);
}
|
LETTER '=' expr
{
regs[$1] = $3;
} ;
expr: '(' expr ')'
{
$$ = $2;
}
|
expr '*' expr
{
$$ = $1 * $3;
}
|
expr '/' expr
{
$$ = $1 / $3;
}
Compiler Construction Sunita M. Dol, CSE Dept
Page 6
|
expr '%' expr
{
$$ = $1 % $3;
}
|
expr '+' expr
{
$$ = $1 + $3;
}
|
expr '-' expr
{
$$ = $1 - $3;
}
|
expr '&' expr
{
$$ = $1 & $3;
}
|
expr '|' expr
{
$$ = $1 | $3;
}
|
'-' expr %prec UMINUS
Compiler Construction Sunita M. Dol, CSE Dept
Page 7
{
$$ = -$2;
}
|
LETTER
{
$$ = regs[$1];
}
|
number
;
number: DIGIT
{
$$ = $1;
base = ($1==0) ? 8 : 10;
} |
number DIGIT
{
$$ = base * $1 + $2;
} ;
%%
main()
{
return(yyparse());
}
Compiler Construction Sunita M. Dol, CSE Dept
Page 8
yyerror(s)
char *s;
{
fprintf(stderr, "%sn",s);
}
yywrap()
{
return(1);
}
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
 Rules Section. The rules section defines the rules that parse the input
stream.
o %start - Specifies that the whole input should match stat.
o %union - By default, the values returned by actions and the lexical
analyzer are integers. yacc can also support values of other types,
including structures. In addition, yacc keeps track of the types, and
inserts appropriate union member names so that the resulting parser
will be strictly type checked. The yacc value stack is declared to be a
union of the various types of values desired. The user declares the
union, and associates union member names to each token and
Compiler Construction Sunita M. Dol, CSE Dept
Page 9
nonterminal symbol having a value. When the value is referenced
through a $$ or $n construction, yacc will automatically insert the
appropriate union name, so that no unwanted conversions will take
place.
o %type - Makes use of the members of the %union declaration and
gives an individual type for the values associated with each part of the
grammar.
o %token - Lists the tokens which come from lex tool with their type.
 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.
main
The required main program that calls the yyparse subroutine to
start the program.
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.
LEX program
This file contains include statements for standard input and output, as well as for
the y.tab.h file. If you use the -d flag with the yacc command, the yacc program
generates that file from the yacc grammar file information. The y.tab.h file
contains definitions for the tokens that the parser program uses. In addition, the
Compiler Construction Sunita M. Dol, CSE Dept
Page 10
calc.lex file contains the rules to generate these tokens from the input stream. The
following are the contents of the calc.lex file.
%{
#include <stdio.h>
#include "y.tab.h"
int c;
extern int yylval;
%}
%%
" " ;
[a-z] {
c = yytext[0];
yylval = c - 'a';
return(LETTER);
}
[0-9] {
c = yytext[0];
yylval = c - '0';
return(DIGIT);
}
[^a-z0-9b] {
c = yytext[0];
return(c);
}
INPUT:
Compiler Construction Sunita M. Dol, CSE Dept
Page 11
2+3
OUTPUT:
5
CONCLUSION:
 YACC file has entries in three sections of a yacc grammar file: declarations,
rules, and programs.
 The lexical analyzer file contains include statements for standard input and
output, as well as for the y.tab.h file. If you use the -d flag with the yacc
command, the yacc program generates that file from the yacc grammar file
information.
 The y.tab.h file contains definitions for the tokens that the parser program
uses. In addition, the lexical analyzer file contains the rules to generate
these tokens from the input stream.
 Thus the parser is implemented using YACC
REFERENCES:
 Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D.
Ullman (Pearson Education)
 System Programming and operating systems – 2nd Edition D.M.Dhamdhere
(TMGH)
 https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/com.ibm.aix.
genprogc/ie_prog_4lex_yacc.htm

More Related Content

What's hot

pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c languageRai University
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiSowmya Jyothi
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolMashaelQ
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01Wingston
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programmingprogramming9
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III TermAndrew Raj
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Rumman Ansari
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_outputAnil Dutt
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++K Durga Prasad
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language CourseVivek chan
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to cHattori Sidek
 

What's hot (19)

Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
 
Assignment10
Assignment10Assignment10
Assignment10
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
 
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex ToolCompiler Engineering Lab#5 : Symbol Table, Flex Tool
Compiler Engineering Lab#5 : Symbol Table, Flex Tool
 
Declaration of variables
Declaration of variablesDeclaration of variables
Declaration of variables
 
Handout#07
Handout#07Handout#07
Handout#07
 
Handout#05
Handout#05Handout#05
Handout#05
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
 
Cnotes
CnotesCnotes
Cnotes
 
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1
 
Structure of c_program_to_input_output
Structure of c_program_to_input_outputStructure of c_program_to_input_output
Structure of c_program_to_input_output
 
Python Programming
Python ProgrammingPython Programming
Python Programming
 
Getting started with c++
Getting started with c++Getting started with c++
Getting started with c++
 
Complete C programming Language Course
Complete C programming Language CourseComplete C programming Language Course
Complete C programming Language Course
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 

Similar to Assignment2

Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabusJK Knowledge
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptxmadhurij54
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manualSami Said
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdfSemsemSameer1
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxrajkumar490591
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxfaithxdunce63732
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)omercomail
 
system software
system software system software
system software randhirlpu
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSivakumar R D .
 
Class 12 computer sample paper with answers
Class 12 computer sample paper with answersClass 12 computer sample paper with answers
Class 12 computer sample paper with answersdebarghyamukherjee60
 
ProjectCompilers.pdfPage 1 of 6 Project Con.docx
ProjectCompilers.pdfPage 1 of 6 Project Con.docxProjectCompilers.pdfPage 1 of 6 Project Con.docx
ProjectCompilers.pdfPage 1 of 6 Project Con.docxwkyra78
 
data.txtInternational Business Management l2 Cons.docx
data.txtInternational Business Management       l2        Cons.docxdata.txtInternational Business Management       l2        Cons.docx
data.txtInternational Business Management l2 Cons.docxtheodorelove43763
 

Similar to Assignment2 (20)

Handout#03
Handout#03Handout#03
Handout#03
 
module 4.pptx
module 4.pptxmodule 4.pptx
module 4.pptx
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
 
Handout#02
Handout#02Handout#02
Handout#02
 
Yacc lex
Yacc lexYacc lex
Yacc lex
 
Lex programming
Lex programmingLex programming
Lex programming
 
Lexyacc
LexyaccLexyacc
Lexyacc
 
Lexyacc
LexyaccLexyacc
Lexyacc
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
 
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptxIIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
IIM.Com-FIT-Unit2(14.9.2021 TO 30.9.2021).pptx
 
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docxCS 23001 Computer Science II Data Structures & AbstractionPro.docx
CS 23001 Computer Science II Data Structures & AbstractionPro.docx
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
 
system software
system software system software
system software
 
Sample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.SivakumarSample for Simple C Program - R.D.Sivakumar
Sample for Simple C Program - R.D.Sivakumar
 
Class 12 computer sample paper with answers
Class 12 computer sample paper with answersClass 12 computer sample paper with answers
Class 12 computer sample paper with answers
 
Slides chapters 28-32
Slides chapters 28-32Slides chapters 28-32
Slides chapters 28-32
 
ProjectCompilers.pdfPage 1 of 6 Project Con.docx
ProjectCompilers.pdfPage 1 of 6 Project Con.docxProjectCompilers.pdfPage 1 of 6 Project Con.docx
ProjectCompilers.pdfPage 1 of 6 Project Con.docx
 
data.txtInternational Business Management l2 Cons.docx
data.txtInternational Business Management       l2        Cons.docxdata.txtInternational Business Management       l2        Cons.docx
data.txtInternational Business Management l2 Cons.docx
 

More from Sunita Milind Dol (19)

9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
 
5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf5. Basic Structure of SQL Queries.pdf
5. Basic Structure of SQL Queries.pdf
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
 
Assignment12
Assignment12Assignment12
Assignment12
 
Assignment11
Assignment11Assignment11
Assignment11
 
Assignment9
Assignment9Assignment9
Assignment9
 
Assignment8
Assignment8Assignment8
Assignment8
 
Assignment6
Assignment6Assignment6
Assignment6
 
Assignment3
Assignment3Assignment3
Assignment3
 
Handout#12
Handout#12Handout#12
Handout#12
 
Handout#11
Handout#11Handout#11
Handout#11
 
Handout#10
Handout#10Handout#10
Handout#10
 
Handout#06
Handout#06Handout#06
Handout#06
 

Recently uploaded

Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxPoojaBan
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx959SahilShah
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfAsst.prof M.Gokilavani
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineeringmalavadedarshan25
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 

Recently uploaded (20)

Heart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptxHeart Disease Prediction using machine learning.pptx
Heart Disease Prediction using machine learning.pptx
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
Application of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptxApplication of Residue Theorem to evaluate real integrations.pptx
Application of Residue Theorem to evaluate real integrations.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCRCall Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
Call Us -/9953056974- Call Girls In Vikaspuri-/- Delhi NCR
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdfCCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
CCS355 Neural Network & Deep Learning UNIT III notes and Question bank .pdf
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Internship report on mechanical engineering
Internship report on mechanical engineeringInternship report on mechanical engineering
Internship report on mechanical engineering
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 

Assignment2

  • 1. Compiler Construction Sunita M. Dol, CSE Dept Page 1 Assignment No.2 AIM: Parser generator using YACC (Yet Another Compiler Compiler) THEORY: YACC Each string specification in the input to YACC resembles a grammar production. The parser generated by YACC performs reductions according to this grammar. The actions associated with a string specification are executed when a reduction is made according to the specification. An attribute is associated with every nonterminal symbol. The value of this attribute can be manipulated during parsing. The attribute can be given any user-designed structure. A symbol ‘$n’ in the action part of a translation rule refers to the attribute of the nth symbol in the RHS of the string specification. ‘$$’ represents the attribute of the LHS symbol of the string specification. Example Figure 3 shows sample input to YACC. The input consists of four components, of which only two are shown. The routine gendesc builds a descriptor containing the name and type of an id or constant. The routine gencode takes an operator and the attributes of two operands, generates code and returns with the attribute
  • 2. Compiler Construction Figure 3: A sample YACC specification Parsing of the* string b+c*d where b, c and d are generated by YACC from the input C routines: Gendesc (id#1); Gendesc (id#2); Gendesc (id#3); Gencode (*, [c,real], [d, real]); Gencode (+, [b, real], [t, real]); where an attribute has the f used to store the result of c*d in the code generated Compiling the Example Program To create the desk calculator example program, do the following: 1. Process the yacc grammar file using the yacc command to create a C language source code): yacc -d calc.yacc Sunita M. Dol, CSE Dept Figure 3: A sample YACC specification the* string b+c*d where b, c and d are of type real, using the parser YACC from the input of above Figure leads to following Gencode (*, [c,real], [d, real]); Gencode (+, [b, real], [t, real]); where an attribute has the form <name>, <type> and t is the name c*d in the code generated by the first call on gencode. Compiling the Example Program To create the desk calculator example program, do the following: grammar file using the -d optional flag (which informs the command to create a file that defines the tokens used in addition to the C language source code): Sunita M. Dol, CSE Dept Page 2 type real, using the parser following-calls on the and t is the name of a location the first call on gencode. optional flag (which informs the file that defines the tokens used in addition to the
  • 3. Compiler Construction Sunita M. Dol, CSE Dept Page 3 2. Use the ls command to verify that the following files were created: 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 3. Process the lex specification file: lex calc.lex 4. Use the ls command to verify that the following file was created: lex.yy.c The C language source file that the lex command created for the lexical analyzer 5. Compile and link the two C language source files: cc y.tab.c lex.yy.c 6. Use the ls command to verify that the following files were created: 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 To run the program directly from the a.out file, type: $ a.out PROGRAM: YACC program
  • 4. Compiler Construction Sunita M. Dol, CSE Dept Page 4 The following example shows the contents of the calc.yacc file. This file has entries in all three sections of a yacc grammar file: declarations, rules, and programs. %{ #include <stdio.h> int regs[26]; int base; %} %start list %union { int a; } %type <a> expr number %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'
  • 5. Compiler Construction Sunita M. Dol, CSE Dept Page 5 { yyerrok; } ; stat: expr { printf("%dn",$1); } | LETTER '=' expr { regs[$1] = $3; } ; expr: '(' expr ')' { $$ = $2; } | expr '*' expr { $$ = $1 * $3; } | expr '/' expr { $$ = $1 / $3; }
  • 6. Compiler Construction Sunita M. Dol, CSE Dept Page 6 | expr '%' expr { $$ = $1 % $3; } | expr '+' expr { $$ = $1 + $3; } | expr '-' expr { $$ = $1 - $3; } | expr '&' expr { $$ = $1 & $3; } | expr '|' expr { $$ = $1 | $3; } | '-' expr %prec UMINUS
  • 7. Compiler Construction Sunita M. Dol, CSE Dept Page 7 { $$ = -$2; } | LETTER { $$ = regs[$1]; } | number ; number: DIGIT { $$ = $1; base = ($1==0) ? 8 : 10; } | number DIGIT { $$ = base * $1 + $2; } ; %% main() { return(yyparse()); }
  • 8. Compiler Construction Sunita M. Dol, CSE Dept Page 8 yyerror(s) char *s; { fprintf(stderr, "%sn",s); } yywrap() { return(1); } 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  Rules Section. The rules section defines the rules that parse the input stream. o %start - Specifies that the whole input should match stat. o %union - By default, the values returned by actions and the lexical analyzer are integers. yacc can also support values of other types, including structures. In addition, yacc keeps track of the types, and inserts appropriate union member names so that the resulting parser will be strictly type checked. The yacc value stack is declared to be a union of the various types of values desired. The user declares the union, and associates union member names to each token and
  • 9. Compiler Construction Sunita M. Dol, CSE Dept Page 9 nonterminal symbol having a value. When the value is referenced through a $$ or $n construction, yacc will automatically insert the appropriate union name, so that no unwanted conversions will take place. o %type - Makes use of the members of the %union declaration and gives an individual type for the values associated with each part of the grammar. o %token - Lists the tokens which come from lex tool with their type.  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. main The required main program that calls the yyparse subroutine to start the program. 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. LEX program This file contains include statements for standard input and output, as well as for the y.tab.h file. If you use the -d flag with the yacc command, the yacc program generates that file from the yacc grammar file information. The y.tab.h file contains definitions for the tokens that the parser program uses. In addition, the
  • 10. Compiler Construction Sunita M. Dol, CSE Dept Page 10 calc.lex file contains the rules to generate these tokens from the input stream. The following are the contents of the calc.lex file. %{ #include <stdio.h> #include "y.tab.h" int c; extern int yylval; %} %% " " ; [a-z] { c = yytext[0]; yylval = c - 'a'; return(LETTER); } [0-9] { c = yytext[0]; yylval = c - '0'; return(DIGIT); } [^a-z0-9b] { c = yytext[0]; return(c); } INPUT:
  • 11. Compiler Construction Sunita M. Dol, CSE Dept Page 11 2+3 OUTPUT: 5 CONCLUSION:  YACC file has entries in three sections of a yacc grammar file: declarations, rules, and programs.  The lexical analyzer file contains include statements for standard input and output, as well as for the y.tab.h file. If you use the -d flag with the yacc command, the yacc program generates that file from the yacc grammar file information.  The y.tab.h file contains definitions for the tokens that the parser program uses. In addition, the lexical analyzer file contains the rules to generate these tokens from the input stream.  Thus the parser is implemented using YACC REFERENCES:  Compilers - Principles, Techniques and Tools - A.V. Aho, R. Shethi and J. D. Ullman (Pearson Education)  System Programming and operating systems – 2nd Edition D.M.Dhamdhere (TMGH)  https://www.ibm.com/support/knowledgecenter/en/ssw_aix_72/com.ibm.aix. genprogc/ie_prog_4lex_yacc.htm