Compiler DesignYacc Example"Yet Another Compiler Compiler"Kanat BolazarCompiler Design Notes | EduRev

: Compiler DesignYacc Example"Yet Another Compiler Compiler"Kanat BolazarCompiler Design Notes | EduRev

 Page 1


Compiler Design
Yacc Example
"Yet Another Compiler Compiler"
Kanat Bolazar
Page 2


Compiler Design
Yacc Example
"Yet Another Compiler Compiler"
Kanat Bolazar
2
Lex and Yacc
• Two classical tools for compilers:
– Lex: A Lexical Analyzer Generator
– Yacc: “Yet Another Compiler Compiler” (Parser Generator)
• Lex creates programs that scan your tokens one by one.
• Yacc takes a grammar (sentence structure) and generates a 
parser.
Lex Yacc
yylex() yyparse()
Lexical Rules Grammar Rules
Input Parsed Input
Page 3


Compiler Design
Yacc Example
"Yet Another Compiler Compiler"
Kanat Bolazar
2
Lex and Yacc
• Two classical tools for compilers:
– Lex: A Lexical Analyzer Generator
– Yacc: “Yet Another Compiler Compiler” (Parser Generator)
• Lex creates programs that scan your tokens one by one.
• Yacc takes a grammar (sentence structure) and generates a 
parser.
Lex Yacc
yylex() yyparse()
Lexical Rules Grammar Rules
Input Parsed Input
3
Lex and Yacc
• Lex and Yacc generate C code for your analyzer & parser.
Lex Yacc
yylex() yyparse()
Lexical Rules Grammar Rules
Input
Parsed
Input
C code C code
C code C code
Lexical Analyzer
(Tokenizer)
Parser
char
stream
token
stream
Page 4


Compiler Design
Yacc Example
"Yet Another Compiler Compiler"
Kanat Bolazar
2
Lex and Yacc
• Two classical tools for compilers:
– Lex: A Lexical Analyzer Generator
– Yacc: “Yet Another Compiler Compiler” (Parser Generator)
• Lex creates programs that scan your tokens one by one.
• Yacc takes a grammar (sentence structure) and generates a 
parser.
Lex Yacc
yylex() yyparse()
Lexical Rules Grammar Rules
Input Parsed Input
3
Lex and Yacc
• Lex and Yacc generate C code for your analyzer & parser.
Lex Yacc
yylex() yyparse()
Lexical Rules Grammar Rules
Input
Parsed
Input
C code C code
C code C code
Lexical Analyzer
(Tokenizer)
Parser
char
stream
token
stream
4
Flex, Yacc, Bison, Byacc
• Often, instead of the standard Lex and Yacc, Flex and 
Bison are used:
– Flex: A fast lexical analyzer
– (GNU) Bison: A drop-in replacement for (backwards compatible 
with) Yacc
• Byacc is Berkeley implementation of Yacc (so it is Yacc).
• Resources:
http://en.wikipedia.org/wiki/Flex_lexical_analyser
http://en.wikipedia.org/wiki/GNU_Bison
• The Lex & Yacc Page (manuals, links):
http://dinosaur.compilertools.net/
Page 5


Compiler Design
Yacc Example
"Yet Another Compiler Compiler"
Kanat Bolazar
2
Lex and Yacc
• Two classical tools for compilers:
– Lex: A Lexical Analyzer Generator
– Yacc: “Yet Another Compiler Compiler” (Parser Generator)
• Lex creates programs that scan your tokens one by one.
• Yacc takes a grammar (sentence structure) and generates a 
parser.
Lex Yacc
yylex() yyparse()
Lexical Rules Grammar Rules
Input Parsed Input
3
Lex and Yacc
• Lex and Yacc generate C code for your analyzer & parser.
Lex Yacc
yylex() yyparse()
Lexical Rules Grammar Rules
Input
Parsed
Input
C code C code
C code C code
Lexical Analyzer
(Tokenizer)
Parser
char
stream
token
stream
4
Flex, Yacc, Bison, Byacc
• Often, instead of the standard Lex and Yacc, Flex and 
Bison are used:
– Flex: A fast lexical analyzer
– (GNU) Bison: A drop-in replacement for (backwards compatible 
with) Yacc
• Byacc is Berkeley implementation of Yacc (so it is Yacc).
• Resources:
http://en.wikipedia.org/wiki/Flex_lexical_analyser
http://en.wikipedia.org/wiki/GNU_Bison
• The Lex & Yacc Page (manuals, links):
http://dinosaur.compilertools.net/
5
Yacc: A Standard Parser Generator
• Yacc is not a new tool, and yet, it is still used in many projects.
• Yacc syntax is similar to Lex/Flex at the top level.
• Lex/Flex rules were regular expression – action pairs.
• Yacc rules are grammar rule – action pairs.
declarations
%%
rules
%%
programs
Read More
Offer running on EduRev: Apply code STAYHOME200 to get INR 200 off on our premium plan EduRev Infinity!