SlideShare ist ein Scribd-Unternehmen logo
1 von 11
COMPILER DESIGN
YACC:Yet Another Compiler -Compiler
YACC: Yet Another Compiler-Compiler
• Yacc generates C code for syntax analyzer, of parser.
• Yacc uses grammar rules that allow it to analyze tokens from LEX
and create a syntax tree.
• Yacc provides a general tool for describing the input to a
computer program.
• The Yacc user specifies the structures of his input, together with
code to be invoked as each such structure is recognized.
• Yacc is written in portable C.
• The class of specifications accepted is a very general one: LALR
grammars with disambiguating rules.
• In addition to compilers for C, APL, Pascal, RATFOR, etc., Yacc
has also been used for less conventional languages, including a
phototypesetter language, several desk calculator languages, a
document retrieval system, and a Fortran debugging system.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
2
Yacc contd…….
• Yacc is a computer program for the Unix operating system.
• It is a Look Ahead Left-to-Right (LALR) parser generator,
generating a parser.
• The part of a compiler that tries to make syntactic sense of
the source code, specifically a LALR parser, based on an analytic
grammar written in a notation similar to Backus–Naur Form
(BNF).
• Yacc was originally developed in the early 1970s by Stephen C.
Johnson at AT&T Corporation and written in the B programming
language, but soon rewritten in C.
• Yacc has also been rewritten for other languages, including
OCaml, Ratfor, ML, Ada, Pascal, Java, Python, Ruby, Go and Com
mon Lisp.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
3
4
yacc
Generate a new parser code from grammar
YACC source (*.y)
y.tab.h
y.tab.c
C compiler/linker
Compile a new parser
y.tab.c a.out
a.out
Parse source code
Token stream
Abstract
Syntax
Tree
y.output
How Does YACC Work?
PLLab, NTHU,Cs2403 Programming
Languages
5
Lex with Yacc
Lex Yacc
yylex() yyparse()
Lex source
(Lexical Rules)
Yacc source
(Grammar Rules)
Input
Parsed
Input
Lex Source
(Lexical Rules)
Yacc Source
(Grammar Rules)
y.tab.clex.yy.c call
Parsed
Input
Input
Return token
LALR PARSER GENERATOR-yacc
• The tool yacc can be used to generate automatically
an LALR parser.
• Input of yacc is divided into three sections:
………..definitions………..
%%
…..rules…..
%%
…….subroutines………..
The def section consists of token declarations & C code
bracketed by % { and % }.
The grammar is placed in the rules section, &
User subroutines are added in subroutines section.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
6
Example- small calculator
• The def section for the yacc input file:
%token INTEGER
This def declares an INTEGER token. When we
run yacc, it generates a parser file y.tab.c & also
creates an include file y.tab.h:
#ifndef YYSTYPE
#define YYSTYPE int
#endif
#define INTEGER 258
extern YYSTYPE yylval;
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
7
Contd……
• Lex includes this file & utilizes the definitions for token values.
• To obtain tokens, yacc calls yylex.
• Function yylex has a return type of int & returns the token value.
• Attributes associated with the token are returned by lex in variable
yylval.
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.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
8
Free APL Compilers and Interpreters
• ELI
ELI is an array programming language that has most of the
features of APL with additional ones not present in the language.
• NGN APL
This is an APL written in JavaScript. (Actually, it is written in
Coffee Script, which compiles to JavaScript.)
• NARS2000
NARS2000 (where NARS stands for Nested Arrays Research
System) is an experimental APL interpreter used as a test bed for
new ideas in the language.
• J
J is a programming language in the APL language family. Unlike
APL, it uses ASCII characters.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
9
Contd…….
• A+
According to its website, "A+ is a descendent of the language 'A'". Unlike
APL, A+ is "geared" to business, supporting large capacity and high
performance, with the "+" referring to the "electric graphical user
interface". It is released under the GNU General Public License.
Supported platforms include Linux, FreeBSD, AIX, IRIX, Mac OS X,
NetBSD, Solaris, and Tru64.
• OpenAPL
This package, for Linux, FreeBSD, OpenBSD, Mac OS X, and POSIX/Unix-
like systems, includes the APL font as well as the interpreter for APL11.
We will have to compile it from the sources provided.
• APL Special Edition
APLSE is a freeware version of APL*Plus PC, a compiler for the APL
programming language, a highly symbolic language (ie, it uses pictorial
symbols rather than the traditional English words) and compact
language.
1/31/2017
ANKUR SRIVASTAVA ASSISTANT PROFESSOR
JIT
10
THANKS

Weitere ähnliche Inhalte

Was ist angesagt?

Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal languageRabia Khalid
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler DesignKuppusamy P
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternBharat Rathore
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTanzeela_Hussain
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environmentIffat Anjum
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lexAnusuya123
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc pptpssraikar
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphsTilakpoudel2
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFAMaulik Togadiya
 

Was ist angesagt? (20)

Theory of automata and formal language
Theory of automata and formal languageTheory of automata and formal language
Theory of automata and formal language
 
Parsing
ParsingParsing
Parsing
 
Code generation in Compiler Design
Code generation in Compiler DesignCode generation in Compiler Design
Code generation in Compiler Design
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Relationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & PatternRelationship Among Token, Lexeme & Pattern
Relationship Among Token, Lexeme & Pattern
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
Lecture 14 run time environment
Lecture 14 run time environmentLecture 14 run time environment
Lecture 14 run time environment
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Lexical analyzer generator lex
Lexical analyzer generator lexLexical analyzer generator lex
Lexical analyzer generator lex
 
Analysis of the source program
Analysis of the source programAnalysis of the source program
Analysis of the source program
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 
Phases of compiler
Phases of compilerPhases of compiler
Phases of compiler
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Basic blocks and control flow graphs
Basic blocks and control flow graphsBasic blocks and control flow graphs
Basic blocks and control flow graphs
 
Type Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLikeType Checking(Compiler Design) #ShareThisIfYouLike
Type Checking(Compiler Design) #ShareThisIfYouLike
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Regular expression with DFA
Regular expression with DFARegular expression with DFA
Regular expression with DFA
 
Lex
LexLex
Lex
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 

Andere mochten auch

Andere mochten auch (20)

Yacc lex
Yacc lexYacc lex
Yacc lex
 
Passescd
PassescdPassescd
Passescd
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Yacc
YaccYacc
Yacc
 
Yacc topic beyond syllabus
Yacc   topic beyond syllabusYacc   topic beyond syllabus
Yacc topic beyond syllabus
 
Cimplementation
CimplementationCimplementation
Cimplementation
 
Ch4c
Ch4cCh4c
Ch4c
 
Flex y Bison
Flex y BisonFlex y Bison
Flex y Bison
 
Parallel Algorithms
Parallel AlgorithmsParallel Algorithms
Parallel Algorithms
 
Classification of Compilers
Classification of CompilersClassification of Compilers
Classification of Compilers
 
Chapter Five(2)
Chapter Five(2)Chapter Five(2)
Chapter Five(2)
 
Lexyacc
LexyaccLexyacc
Lexyacc
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Meta Languages Bnf Ebnf Student Version
Meta Languages Bnf Ebnf Student VersionMeta Languages Bnf Ebnf Student Version
Meta Languages Bnf Ebnf Student Version
 
Minimization of dfa
Minimization of dfaMinimization of dfa
Minimization of dfa
 
System Programming Unit IV
System Programming Unit IVSystem Programming Unit IV
System Programming Unit IV
 
compiler and their types
compiler and their typescompiler and their types
compiler and their types
 
Parsing example
Parsing exampleParsing example
Parsing example
 
Compiler Design Tutorial
Compiler Design Tutorial Compiler Design Tutorial
Compiler Design Tutorial
 
Validation based protocol
Validation based protocolValidation based protocol
Validation based protocol
 

Ähnlich wie Yacc

Ähnlich wie Yacc (20)

Structure-Compiler-phases information about basics of compiler. Pdfpdf
Structure-Compiler-phases information  about basics of compiler. PdfpdfStructure-Compiler-phases information  about basics of compiler. Pdfpdf
Structure-Compiler-phases information about basics of compiler. Pdfpdf
 
LANGUAGE TRANSLATOR
LANGUAGE TRANSLATORLANGUAGE TRANSLATOR
LANGUAGE TRANSLATOR
 
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
 
Oracle sun studio
Oracle sun studioOracle sun studio
Oracle sun studio
 
Using ANTLR on real example - convert "string combined" queries into paramete...
Using ANTLR on real example - convert "string combined" queries into paramete...Using ANTLR on real example - convert "string combined" queries into paramete...
Using ANTLR on real example - convert "string combined" queries into paramete...
 
Ch1 (1).ppt
Ch1 (1).pptCh1 (1).ppt
Ch1 (1).ppt
 
Compiler1
Compiler1Compiler1
Compiler1
 
Viva
VivaViva
Viva
 
Viva
VivaViva
Viva
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compier Design_Unit I.ppt
Compier Design_Unit I.pptCompier Design_Unit I.ppt
Compier Design_Unit I.ppt
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
Compier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.pptCompier Design_Unit I_SRM.ppt
Compier Design_Unit I_SRM.ppt
 
Group 19 CD project
Group 19 CD projectGroup 19 CD project
Group 19 CD project
 
Introduction to Scala
Introduction to ScalaIntroduction to Scala
Introduction to Scala
 
11700220036.pdf
11700220036.pdf11700220036.pdf
11700220036.pdf
 
CD U1-5.pptx
CD U1-5.pptxCD U1-5.pptx
CD U1-5.pptx
 
COMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptxCOMPILER CONSTRUCTION KU 1.pptx
COMPILER CONSTRUCTION KU 1.pptx
 
1._Introduction_.pptx
1._Introduction_.pptx1._Introduction_.pptx
1._Introduction_.pptx
 
Introduction
IntroductionIntroduction
Introduction
 

Mehr von BBDITM LUCKNOW (14)

Unit 5 cspc
Unit 5 cspcUnit 5 cspc
Unit 5 cspc
 
Unit 4 cspc
Unit 4 cspcUnit 4 cspc
Unit 4 cspc
 
Unit3 cspc
Unit3 cspcUnit3 cspc
Unit3 cspc
 
Cse ppt 2018
Cse ppt 2018Cse ppt 2018
Cse ppt 2018
 
Binary system ppt
Binary system pptBinary system ppt
Binary system ppt
 
Unit 4 ca-input-output
Unit 4 ca-input-outputUnit 4 ca-input-output
Unit 4 ca-input-output
 
Unit 3 ca-memory
Unit 3 ca-memoryUnit 3 ca-memory
Unit 3 ca-memory
 
Unit 2 ca- control unit
Unit 2 ca- control unitUnit 2 ca- control unit
Unit 2 ca- control unit
 
Unit 1 ca-introduction
Unit 1 ca-introductionUnit 1 ca-introduction
Unit 1 ca-introduction
 
Bnf and ambiquity
Bnf and ambiquityBnf and ambiquity
Bnf and ambiquity
 
Compiler unit 4
Compiler unit 4Compiler unit 4
Compiler unit 4
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Compiler unit 5
Compiler  unit 5Compiler  unit 5
Compiler unit 5
 
Cspc final
Cspc finalCspc final
Cspc final
 

Kürzlich hochgeladen

Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991RKavithamani
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...RKavithamani
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 

Kürzlich hochgeladen (20)

Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
Industrial Policy - 1948, 1956, 1973, 1977, 1980, 1991
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
Privatization and Disinvestment - Meaning, Objectives, Advantages and Disadva...
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

Yacc

  • 1. COMPILER DESIGN YACC:Yet Another Compiler -Compiler
  • 2. YACC: Yet Another Compiler-Compiler • Yacc generates C code for syntax analyzer, of parser. • Yacc uses grammar rules that allow it to analyze tokens from LEX and create a syntax tree. • Yacc provides a general tool for describing the input to a computer program. • The Yacc user specifies the structures of his input, together with code to be invoked as each such structure is recognized. • Yacc is written in portable C. • The class of specifications accepted is a very general one: LALR grammars with disambiguating rules. • In addition to compilers for C, APL, Pascal, RATFOR, etc., Yacc has also been used for less conventional languages, including a phototypesetter language, several desk calculator languages, a document retrieval system, and a Fortran debugging system. 1/31/2017 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JIT 2
  • 3. Yacc contd……. • Yacc is a computer program for the Unix operating system. • It is a Look Ahead Left-to-Right (LALR) parser generator, generating a parser. • The part of a compiler that tries to make syntactic sense of the source code, specifically a LALR parser, based on an analytic grammar written in a notation similar to Backus–Naur Form (BNF). • Yacc was originally developed in the early 1970s by Stephen C. Johnson at AT&T Corporation and written in the B programming language, but soon rewritten in C. • Yacc has also been rewritten for other languages, including OCaml, Ratfor, ML, Ada, Pascal, Java, Python, Ruby, Go and Com mon Lisp. 1/31/2017 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JIT 3
  • 4. 4 yacc Generate a new parser code from grammar YACC source (*.y) y.tab.h y.tab.c C compiler/linker Compile a new parser y.tab.c a.out a.out Parse source code Token stream Abstract Syntax Tree y.output How Does YACC Work?
  • 5. PLLab, NTHU,Cs2403 Programming Languages 5 Lex with Yacc Lex Yacc yylex() yyparse() Lex source (Lexical Rules) Yacc source (Grammar Rules) Input Parsed Input Lex Source (Lexical Rules) Yacc Source (Grammar Rules) y.tab.clex.yy.c call Parsed Input Input Return token
  • 6. LALR PARSER GENERATOR-yacc • The tool yacc can be used to generate automatically an LALR parser. • Input of yacc is divided into three sections: ………..definitions……….. %% …..rules….. %% …….subroutines……….. The def section consists of token declarations & C code bracketed by % { and % }. The grammar is placed in the rules section, & User subroutines are added in subroutines section. 1/31/2017 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JIT 6
  • 7. Example- small calculator • The def section for the yacc input file: %token INTEGER This def declares an INTEGER token. When we run yacc, it generates a parser file y.tab.c & also creates an include file y.tab.h: #ifndef YYSTYPE #define YYSTYPE int #endif #define INTEGER 258 extern YYSTYPE yylval; 1/31/2017 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JIT 7
  • 8. Contd…… • Lex includes this file & utilizes the definitions for token values. • To obtain tokens, yacc calls yylex. • Function yylex has a return type of int & returns the token value. • Attributes associated with the token are returned by lex in variable yylval. 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. 1/31/2017 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JIT 8
  • 9. Free APL Compilers and Interpreters • ELI ELI is an array programming language that has most of the features of APL with additional ones not present in the language. • NGN APL This is an APL written in JavaScript. (Actually, it is written in Coffee Script, which compiles to JavaScript.) • NARS2000 NARS2000 (where NARS stands for Nested Arrays Research System) is an experimental APL interpreter used as a test bed for new ideas in the language. • J J is a programming language in the APL language family. Unlike APL, it uses ASCII characters. 1/31/2017 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JIT 9
  • 10. Contd……. • A+ According to its website, "A+ is a descendent of the language 'A'". Unlike APL, A+ is "geared" to business, supporting large capacity and high performance, with the "+" referring to the "electric graphical user interface". It is released under the GNU General Public License. Supported platforms include Linux, FreeBSD, AIX, IRIX, Mac OS X, NetBSD, Solaris, and Tru64. • OpenAPL This package, for Linux, FreeBSD, OpenBSD, Mac OS X, and POSIX/Unix- like systems, includes the APL font as well as the interpreter for APL11. We will have to compile it from the sources provided. • APL Special Edition APLSE is a freeware version of APL*Plus PC, a compiler for the APL programming language, a highly symbolic language (ie, it uses pictorial symbols rather than the traditional English words) and compact language. 1/31/2017 ANKUR SRIVASTAVA ASSISTANT PROFESSOR JIT 10