SlideShare a Scribd company logo
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

Getting Started with C++
Getting Started with C++Getting Started with C++
Getting Started with C++
Praveen M Jigajinni
 
Assignment10
Assignment10Assignment10
Assignment10
Sunita Milind Dol
 
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
Rai University
 
datatypes and variables in c language
 datatypes and variables in c language datatypes and variables in c language
datatypes and variables in c language
Rai University
 
Overview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya JyothiOverview of C Mrs Sowmya Jyothi
Overview of C Mrs Sowmya Jyothi
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
MashaelQ
 
Declaration of variables
Declaration of variablesDeclaration of variables
Declaration of variables
Maria Stella Solon
 
Handout#07
Handout#07Handout#07
Handout#07
Sunita Milind Dol
 
Handout#05
Handout#05Handout#05
Handout#05
Sunita Milind Dol
 
Introduction to Basic C programming 01
Introduction to Basic C programming 01Introduction to Basic C programming 01
Introduction to Basic C programming 01
Wingston
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
programming9
 
C programming | Class 8 | III Term
C programming  | Class 8  | III TermC programming  | Class 8  | III Term
C programming | Class 8 | III Term
Andrew Raj
 
Cnotes
CnotesCnotes
Basic c programming and explanation PPT1
Basic c programming and explanation PPT1Basic c programming and explanation PPT1
Basic c programming and explanation PPT1
Rumman 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_output
Anil Dutt
 
Python Programming
Python ProgrammingPython Programming
Python Programming
Saravanan T.M
 
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 Course
Vivek chan
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori 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

Handout#03
Handout#03Handout#03
Handout#03
Sunita Milind Dol
 
module 4.pptx
module 4.pptxmodule 4.pptx
module 4.pptx
ShwetaNirmanik
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
JK Knowledge
 
Handout#02
Handout#02Handout#02
Handout#02
Sunita Milind Dol
 
Yacc lex
Yacc lexYacc lex
Yacc lex
915086731
 
Lex programming
Lex programmingLex programming
Lex programming
sureshmoharana2013
 
Lexyacc
LexyaccLexyacc
Lexyacc
LexyaccLexyacc
Lexyacc
unifesptk
 
unit 1 cpds.pptx
unit 1 cpds.pptxunit 1 cpds.pptx
unit 1 cpds.pptx
madhurij54
 
Lex tool manual
Lex tool manualLex tool manual
Lex tool manual
Sami Said
 
1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf1588147798Begining_ABUAD1.pdf
1588147798Begining_ABUAD1.pdf
SemsemSameer1
 
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
rajkumar490591
 
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
faithxdunce63732
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
omercomail
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
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.Sivakumar
Sivakumar 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 answers
debarghyamukherjee60
 
Slides chapters 28-32
Slides chapters 28-32Slides chapters 28-32
Slides chapters 28-32
Priyanka Shetty
 
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
wkyra78
 

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)
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
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
 

More from Sunita Milind Dol

Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
Sunita Milind Dol
 
Unit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writingUnit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writing
Sunita Milind Dol
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
Sunita Milind Dol
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
Sunita Milind Dol
 
Unit Number 1 - Introduction to Research
Unit Number 1 - Introduction to ResearchUnit Number 1 - Introduction to Research
Unit Number 1 - Introduction to Research
Sunita Milind Dol
 
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
Sunita Milind Dol
 
Unit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writingUnit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writing
Sunita Milind Dol
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
Sunita Milind Dol
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
Sunita Milind Dol
 
Unit Number 1 : Introduction to Research
Unit Number 1 : Introduction to ResearchUnit Number 1 : Introduction to Research
Unit Number 1 : Introduction to Research
Sunita Milind Dol
 
9.Joins.pdf
9.Joins.pdf9.Joins.pdf
9.Joins.pdf
Sunita Milind Dol
 
8.Views.pdf
8.Views.pdf8.Views.pdf
8.Views.pdf
Sunita Milind Dol
 
7. Nested Subqueries.pdf
7. Nested Subqueries.pdf7. Nested Subqueries.pdf
7. Nested Subqueries.pdf
Sunita Milind Dol
 
6. Aggregate Functions.pdf
6. Aggregate Functions.pdf6. Aggregate Functions.pdf
6. Aggregate Functions.pdf
Sunita Milind Dol
 
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
Sunita Milind Dol
 
4. DML.pdf
4. DML.pdf4. DML.pdf
4. DML.pdf
Sunita Milind Dol
 
3. DDL.pdf
3. DDL.pdf3. DDL.pdf
3. DDL.pdf
Sunita Milind Dol
 
2. SQL Introduction.pdf
2. SQL Introduction.pdf2. SQL Introduction.pdf
2. SQL Introduction.pdf
Sunita Milind Dol
 
1. University Example.pdf
1. University Example.pdf1. University Example.pdf
1. University Example.pdf
Sunita Milind Dol
 
Assignment12
Assignment12Assignment12
Assignment12
Sunita Milind Dol
 

More from Sunita Milind Dol (20)

Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
 
Unit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writingUnit Number 4 - Research reports and Thesis writing
Unit Number 4 - Research reports and Thesis writing
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
 
Unit Number 1 - Introduction to Research
Unit Number 1 - Introduction to ResearchUnit Number 1 - Introduction to Research
Unit Number 1 - Introduction to Research
 
Unit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and PublishingUnit Number 5 - Research Ethics, IPR and Publishing
Unit Number 5 - Research Ethics, IPR and Publishing
 
Unit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writingUnit Number 5 - Research reports and Thesis writing
Unit Number 5 - Research reports and Thesis writing
 
Unit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical AnalysisUnit Number 3 - Data collection and Statistical Analysis
Unit Number 3 - Data collection and Statistical Analysis
 
Unit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and MethodsUnit Number 2 - Research Problem Formulation and Methods
Unit Number 2 - Research Problem Formulation and Methods
 
Unit Number 1 : Introduction to Research
Unit Number 1 : Introduction to ResearchUnit Number 1 : Introduction to Research
Unit Number 1 : Introduction to Research
 
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
 

Recently uploaded

一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
ecqow
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
sachin chaurasia
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
bijceesjournal
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
shadow0702a
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
Victor Morales
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Sinan KOZAK
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
co23btech11018
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
Madan Karki
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
Yasser Mahgoub
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
shivani5543
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
171ticu
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
SUTEJAS
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
Rahul
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
LAXMAREDDY22
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
RamonNovais6
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
IJECEIAES
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
mamamaam477
 

Recently uploaded (20)

一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
一比一原版(CalArts毕业证)加利福尼亚艺术学院毕业证如何办理
 
The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.The Python for beginners. This is an advance computer language.
The Python for beginners. This is an advance computer language.
 
Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...Comparative analysis between traditional aquaponics and reconstructed aquapon...
Comparative analysis between traditional aquaponics and reconstructed aquapon...
 
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
Use PyCharm for remote debugging of WSL on a Windo cf5c162d672e4e58b4dde5d797...
 
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressionsKuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
KuberTENes Birthday Bash Guadalajara - K8sGPT first impressions
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
Optimizing Gradle Builds - Gradle DPE Tour Berlin 2024
 
Computational Engineering IITH Presentation
Computational Engineering IITH PresentationComputational Engineering IITH Presentation
Computational Engineering IITH Presentation
 
spirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptxspirit beverages ppt without graphics.pptx
spirit beverages ppt without graphics.pptx
 
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
2008 BUILDING CONSTRUCTION Illustrated - Ching Chapter 02 The Building.pdf
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))gray level transformation unit 3(image processing))
gray level transformation unit 3(image processing))
 
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样官方认证美国密歇根州立大学毕业证学位证书原版一模一样
官方认证美国密歇根州立大学毕业证学位证书原版一模一样
 
Understanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine LearningUnderstanding Inductive Bias in Machine Learning
Understanding Inductive Bias in Machine Learning
 
ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024ACEP Magazine edition 4th launched on 05.06.2024
ACEP Magazine edition 4th launched on 05.06.2024
 
BRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdfBRAIN TUMOR DETECTION for seminar ppt.pdf
BRAIN TUMOR DETECTION for seminar ppt.pdf
 
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURSCompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
CompEx~Manual~1210 (2).pdf COMPEX GAS AND VAPOURS
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
Redefining brain tumor segmentation: a cutting-edge convolutional neural netw...
 
Engine Lubrication performance System.pdf
Engine Lubrication performance System.pdfEngine Lubrication performance System.pdf
Engine Lubrication performance System.pdf
 

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