SlideShare uma empresa Scribd logo
1 de 25
L A B # ٧ : F L E X & B I S O N
COMPILER
ENGINEERING
TOKEN VALUES
• When a flex scanner returns a stream of tokens, each
token actually has two parts:
• the token
• and the token’s value
• The token is a small integer.
• The token numbers are arbitrary, except that token zero
always means end-of-file.
• When bison creates a parser:
• Bison assigns the token numbers automatically starting at 258
• And creates a .h with definitions of the tokens numbers.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
2
REMINDER
• The . pattern is the catchall to match anything the other
patterns didn’t.
• Any quoted strings tell flex to use the strings as is, rather
than interpreting them as regular expressions.
• Ex.
“+” {printf (“PLUS“);}
• Important Rule:
• In a flex file,
• If an action code returns  scanning resumes on the next call to
yylex();
• if it doesn’t return  scanning resumes immediately.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
3
YYLVAL
• YYlval is a variable that stores the token value.
• The value is usually defined as a UNION  so that
different kinds of tokens will have different types of
values (ex. Integer value, floating point value, or a
pointer to an entry in the symbol table).
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
4
YYWRAP()
1. YYWrap Option:
When the scanner receives an end-of-file indication from
YY_INPUT, it then checks the `yywrap()' function:
• If `yywrap()' returns false (zero)  it is assumed that the function has
gone ahead and set up yyin to point to another input file, and scanning
continues.
• If it returns true (non-zero) the scanner terminates, returning 0 to
its caller. Note that in either case, the start condition remains
unchanged; it does not revert to INITIAL.
• If you do not supply your own version of `yywrap()‘:
• then you must either use `%option noyywrap' (in which case the
scanner behaves as though `yywrap()' returned 1),
• or you must link with `-lfl' to obtain the default version of the routine,
which always returns 1.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
5
BISON PARSING
• Any bison parser makes a parse tree as it parses its
input:
• In some applications, it creates the tree as a data structure in
memory for later use.
• In others, the tree is just implicit in the sequence of operations
the parser does.
• In order to write a parser, we need some way to describe
the rules the parser uses to turn a sequence of tokens
into a parse tree.
• The most common kind of language that computer parsers
handle is a context-free grammar (CFG).
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
6
GRAMMARS & PARSING
• The parser’s job is to figure out the relationship among
the input tokens.
• A common way to display such relationships is a parse
tree. (Ex. arithmetic expression 1 * 2 + 3 * 4 + 5)
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
7
GRAMMARS & PARSING
• Multiplication has higher precedence than addition, so :
• the first two expressions are 1 * 2 and 3 * 4.
• Then those two expressions are added together, and that sum is
then added to 5.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
8
GRAMMARS & PARSING
• BNF for simple arithmetic expressions :
• <exp> ::= <factor>
| <exp> + <factor>
• <factor> ::= NUMBER
| <factor> * NUMBER
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
9
GRAMMARS & PARSING
• Each line is a rule that says how to create a branch of
the parse tree.
• In BNF, ::= can be read “is a” or “becomes”
• and | is “or,”
• The name on the left side of a rule is a symbol or term.
• By convention, all tokens are considered to be symbols,
but there are also symbols that are not tokens.
• Useful BNF is invariably quite recursive, with rules that
refer to themselves directly or indirectly.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
10
BISON’S RULE INPUT LANGUAGE
• Bison rules are basically BNF, with the punctuation
simplified a little to make them easier to type.
• Bison programs have three-part structure as flex
programs, with declarations, rules, and C code.
1. The Declarations Section:
• Includes C code to be copied to the beginning of the generated C
parser, again enclosed in %{ and %}.
• Following that are %token token declarations, telling bison the names
of the symbols in the parser that are tokens.
• By convention, tokens have uppercase names, although bison doesn’t
require it.
• Any symbols not declared as tokens have to appear on the left side of
at least one rule in the program
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
11
BISON’S RULE INPUT LANGUAGE
2. The Rules Section:
• Includes rules in simplified BNF
• Bison uses a single colon : rather than ::=
• A semicolon marks the end of a rule.
• like flex, the C action code goes in braces at the end of each rule.
• The symbol on the left side of the first rule is the start symbol, the one that
the entire input has to match.
• There can be, and usually are, other rules with the same start symbol on
the left.
• Note:
• Bison automatically does the parsing for you, remembering what rules
have been matched, so the action code maintains the values associated
with each symbol.
• Bison parsers also perform side effects such as creating data structures
for later use or, for example printing out results.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
12
BISON’S RULE INPUT LANGUAGE
2. The Rules Section:
• Each symbol in a bison rule has a value; the value of the target
symbol (the one to the left of the colon) is called $$ in the action code
• and the values on the right are numbered $1, $2, and so forth, up to the
number of symbols in the rule.
• The values of tokens are whatever was in yylval when the scanner
returned the token
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
13
EXAMPLE #1: BISON CALCUALTOR.Y
FILE
• Example # 1:
Write Bison code, including the BNF, for the first version
of our calculator.
• Ensure to handle errors in case of syntax-error appears.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
14
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
15
/* simplest version of calculator */
%{
#include <stdio.h>
%}
/* declare tokens */
%token NUMBER
%token ADD SUB MUL DIV ABS
%token EOL
%%
calclist: /* nothing */
| calclist exp EOL { printf("= %dn", $2); } EOL is end of an expression ;
exp: factor default $$ = $1
| exp ADD factor { $$ = $1 + $3; } | exp SUB factor { $$ = $1 - $3; } ;
factor: term default $$ = $1
| factor MUL term { $$ = $1 * $3; } | factor DIV term { $$ = $1 / $3; } ;
term: NUMBER default $$ = $1
| ABS term { $$ = $2 >= 0? $2 : - $2; } ;
%%
main(int argc, char **argv)
{
…..
EXAMPLE#1: SIMPLE
CALCULATOR BISON FILE
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
16
/* simplest version of calculator */
…..
%%
….
%%
main(int argc, char **argv)
{
yyparse();
}
yyerror(char *s)
{
printf(stderr, "error: %sn", s);
}
EXAMPLE#1: SIMPLE
CALCULATOR BISON FILE
(CONTINUED)
EXERCISES : BISON.Y FILE(S)
• E2: Alter example#1 to accept one-line comment (ex. //
this is a comment )
• "//".* /* ignore comments */
• E3: Allow the calculator to recognize hexadecimal
numbers
• 0x[a-f0-9]+
• Use strtol function (Stdlib.h) to convert the string in c to long
number
• E3: Allow the calculator to understand the mod (%)
operator - a%b where a and b are integers gives you the
remainder when a is divided by b.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
17
COMPILING FLEX AND BISON
PROGRAMS TOGETHER
1. Include a header file that bison will create for us, which
includes both definitions of the token numbers and a
definition of yylval.
2. Include Bison’s header file in Flex’s file.
%{
# include ”cal.tab.h"
%}
%% same rules as before, and no code in the third
section
3. Delete the testing main routine in the third section of
the scanner, since the parser will now call the scanner.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
18
EXAMPLE #1 : FLEX AND BISON
TOGETHER FOR CALCULATOR
• The build process is now complex enough to be worth
putting into a Makefile:
• bison -d cal.y
flex cal.l
cc cal.tab.c lex.yy.c -lfl
• Explanation:
• First it runs bison with the -d (for “definitions” file) flag, which
creates cal.tab.c and cal.tab.h,
• and it runs flex to create lex.yy.c.
• Then it compiles them together, along with the flex library.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
19
EXAMPLE #1 : FLEX AND BISON
TOGETHER FOR CALCULATOR
• In this parser, the first two rules, which define the symbol
calcset, implement a loop that reads an expression
terminated by a newline and prints its value.
• The definition of calclist uses a common two-rule
recursive idiom to implement a sequence or list:
1. the first rule is empty and matches nothing;
2. the second adds an item to the list. The action in the second
rule prints the value of the exp in $2.
• In the absence of an explicit action on a rule, the parser
assigns $1 to $$.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
20
AMBIGUOUS GRAMMARS
• Why shouldn’t we just write ?
• exp: exp ADD exp
| exp SUB exp
| exp MUL exp
| exp DIV exp
| ABS exp
| NUMBER ;
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
21
There are two answers:
Precedence and Ambiguity
AMBIGUOUS GRAMMARS
• The separate symbols for term, factor, and exp tell bison
to handle ABS, then MUL and DIV, then ADD and SUB.
• In general, whenever a grammar has multiple levels of
precedence where one operator binds “tighter” than
another.
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
22
AMBIGUOUS GRAMMARS
• What about this?
exp: exp ADD exp
| exp SUB exp
| factor ;
• So, the example mentioned here is ambiguous as the
expression 1 + 2 – 3 could be parsed (1+2)-3 or 1+(2-3)
 two different expression with two different values
• Bison will not parse an ambiguous grammar and report
conflicts  Ambiguity is an error
• That is, any parse that Bison creates has exactly only
one way to parse any input
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
23
SEMANTIC ANALYSIS (PARSING)
• Validate expressions and taking appropriate actions
• Generate code
• Checks the source program for semantic errors
• Gathers type information for the subsequent code-generation
phase.
• It uses the hierarchical structure determine by the Syntax-
Analysis phase  to identify operators + operands of
expressions and statements
• Important Component: Type Checking
• Ex. Using Real number as an index for an Array
• If an Arithmetic operation is applied to two numbers, one is Real and
the other is Integer  type conversion must be applied (intToReal)
by adding an extra node
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
24
QUESTIONS?
Thank you for listening 
14-18/4/12
Department of Computer Science -
Compiler Engineering Lab
25

Mais conteúdo relacionado

Mais procurados

Different phases of a compiler
Different phases of a compilerDifferent phases of a compiler
Different phases of a compiler
Sumit Sinha
 
4 compiler lab - Syntax Ana
4 compiler lab - Syntax Ana4 compiler lab - Syntax Ana
4 compiler lab - Syntax Ana
MashaelQ
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
rawan_z
 
Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilers
Vasim Pathan
 

Mais procurados (20)

Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
 
Different phases of a compiler
Different phases of a compilerDifferent phases of a compiler
Different phases of a compiler
 
Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2Lecture 15 run timeenvironment_2
Lecture 15 run timeenvironment_2
 
Lex and Yacc ppt
Lex and Yacc pptLex and Yacc ppt
Lex and Yacc ppt
 
Compiler Construction
Compiler ConstructionCompiler Construction
Compiler Construction
 
4 compiler lab - Syntax Ana
4 compiler lab - Syntax Ana4 compiler lab - Syntax Ana
4 compiler lab - Syntax Ana
 
Compilers Design
Compilers DesignCompilers Design
Compilers Design
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)Yacc (yet another compiler compiler)
Yacc (yet another compiler compiler)
 
Compiler unit 1
Compiler unit 1Compiler unit 1
Compiler unit 1
 
Intermediate code- generation
Intermediate code- generationIntermediate code- generation
Intermediate code- generation
 
what is compiler and five phases of compiler
what is compiler and five phases of compilerwhat is compiler and five phases of compiler
what is compiler and five phases of compiler
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
 
Cd lab manual
Cd lab manualCd lab manual
Cd lab manual
 
Spr ch-05-compilers
Spr ch-05-compilersSpr ch-05-compilers
Spr ch-05-compilers
 
Lexical Analysis
Lexical AnalysisLexical Analysis
Lexical Analysis
 
Cd unit i
Cd unit iCd unit i
Cd unit i
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Compiler1
Compiler1Compiler1
Compiler1
 

Destaque

Critical Security And Compliance Issues In Internet Banking
Critical Security And Compliance Issues In Internet BankingCritical Security And Compliance Issues In Internet Banking
Critical Security And Compliance Issues In Internet Banking
Thomas Donofrio
 
Bruteforce basic presentation_file - linx
Bruteforce basic presentation_file - linxBruteforce basic presentation_file - linx
Bruteforce basic presentation_file - linx
idsecconf
 
Original image (Unification Thought)
Original image (Unification Thought)Original image (Unification Thought)
Original image (Unification Thought)
derek dey
 

Destaque (20)

Compiler design lab programs
Compiler design lab programs Compiler design lab programs
Compiler design lab programs
 
Compiler Design Lab File
Compiler Design Lab FileCompiler Design Lab File
Compiler Design Lab File
 
Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)Compiler Design(NANTHU NOTES)
Compiler Design(NANTHU NOTES)
 
Cs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer KeyCs6660 compiler design may june 2016 Answer Key
Cs6660 compiler design may june 2016 Answer Key
 
Cs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer keyCs6660 compiler design november december 2016 Answer key
Cs6660 compiler design november december 2016 Answer key
 
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
 
Critical Security And Compliance Issues In Internet Banking
Critical Security And Compliance Issues In Internet BankingCritical Security And Compliance Issues In Internet Banking
Critical Security And Compliance Issues In Internet Banking
 
DISSERTATION_40096050
DISSERTATION_40096050DISSERTATION_40096050
DISSERTATION_40096050
 
MATERI KRIPTOGRAFI
MATERI KRIPTOGRAFIMATERI KRIPTOGRAFI
MATERI KRIPTOGRAFI
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
 
An Analytical Approach To Analyze The Impact Of Gray Hole Attacks In Manet
An Analytical Approach To Analyze The Impact Of Gray Hole Attacks In ManetAn Analytical Approach To Analyze The Impact Of Gray Hole Attacks In Manet
An Analytical Approach To Analyze The Impact Of Gray Hole Attacks In Manet
 
introduction to cryptography and its role in information technology era
introduction to cryptography and its role in information technology eraintroduction to cryptography and its role in information technology era
introduction to cryptography and its role in information technology era
 
Compiler Construction Course - Introduction
Compiler Construction Course - IntroductionCompiler Construction Course - Introduction
Compiler Construction Course - Introduction
 
Bruteforce basic presentation_file - linx
Bruteforce basic presentation_file - linxBruteforce basic presentation_file - linx
Bruteforce basic presentation_file - linx
 
Original image (Unification Thought)
Original image (Unification Thought)Original image (Unification Thought)
Original image (Unification Thought)
 
Plagiarisme
PlagiarismePlagiarisme
Plagiarisme
 
Introduction to Compiler Construction
Introduction to Compiler Construction Introduction to Compiler Construction
Introduction to Compiler Construction
 
Pembelajaran Berbasis Riset (Hukum Gossen I)
Pembelajaran Berbasis Riset (Hukum Gossen I)Pembelajaran Berbasis Riset (Hukum Gossen I)
Pembelajaran Berbasis Riset (Hukum Gossen I)
 
Sosdarkam SMKN 1 cibinong 13 April 2016
Sosdarkam SMKN 1 cibinong 13 April 2016Sosdarkam SMKN 1 cibinong 13 April 2016
Sosdarkam SMKN 1 cibinong 13 April 2016
 
Compiler construction
Compiler constructionCompiler construction
Compiler construction
 

Semelhante a 7 compiler lab

Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
Rabia Khalid
 
Assembler design option
Assembler design optionAssembler design option
Assembler design option
Mohd Arif
 

Semelhante a 7 compiler lab (20)

Lecture 1.pptx
Lecture 1.pptxLecture 1.pptx
Lecture 1.pptx
 
6 compiler lab - Flex
6 compiler lab - Flex6 compiler lab - Flex
6 compiler lab - Flex
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
Simplified instructional computer
Simplified instructional computerSimplified instructional computer
Simplified instructional computer
 
Kirby, Fabro
Kirby, FabroKirby, Fabro
Kirby, Fabro
 
Unit1.ppt
Unit1.pptUnit1.ppt
Unit1.ppt
 
Lecture 2 coal sping12
Lecture 2 coal sping12Lecture 2 coal sping12
Lecture 2 coal sping12
 
Programming the basic computer
Programming the basic computerProgramming the basic computer
Programming the basic computer
 
Uc 2(vii)
Uc 2(vii)Uc 2(vii)
Uc 2(vii)
 
Chapter 1.pptx
Chapter 1.pptxChapter 1.pptx
Chapter 1.pptx
 
Flex
FlexFlex
Flex
 
Porting is a Delicate Matter: Checking Far Manager under Linux
Porting is a Delicate Matter: Checking Far Manager under LinuxPorting is a Delicate Matter: Checking Far Manager under Linux
Porting is a Delicate Matter: Checking Far Manager under Linux
 
Csd01
Csd01Csd01
Csd01
 
Unit 3 sp assembler
Unit 3 sp assemblerUnit 3 sp assembler
Unit 3 sp assembler
 
Report on c and c++
Report on c and c++Report on c and c++
Report on c and c++
 
Assembler design option
Assembler design optionAssembler design option
Assembler design option
 
basic computer programming and micro programmed control
basic computer programming and micro programmed controlbasic computer programming and micro programmed control
basic computer programming and micro programmed control
 
Mca i-u-3-basic computer programming and micro programmed control
Mca i-u-3-basic computer programming and micro programmed controlMca i-u-3-basic computer programming and micro programmed control
Mca i-u-3-basic computer programming and micro programmed control
 
10600122065_Animesh mani (CD).pdf
10600122065_Animesh mani (CD).pdf10600122065_Animesh mani (CD).pdf
10600122065_Animesh mani (CD).pdf
 
Computer Architecture Assignment Help
Computer Architecture Assignment HelpComputer Architecture Assignment Help
Computer Architecture Assignment Help
 

Último

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Introduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDMIntroduction to use of FHIR Documents in ABDM
Introduction to use of FHIR Documents in ABDM
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 

7 compiler lab

  • 1. L A B # ٧ : F L E X & B I S O N COMPILER ENGINEERING
  • 2. TOKEN VALUES • When a flex scanner returns a stream of tokens, each token actually has two parts: • the token • and the token’s value • The token is a small integer. • The token numbers are arbitrary, except that token zero always means end-of-file. • When bison creates a parser: • Bison assigns the token numbers automatically starting at 258 • And creates a .h with definitions of the tokens numbers. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 2
  • 3. REMINDER • The . pattern is the catchall to match anything the other patterns didn’t. • Any quoted strings tell flex to use the strings as is, rather than interpreting them as regular expressions. • Ex. “+” {printf (“PLUS“);} • Important Rule: • In a flex file, • If an action code returns  scanning resumes on the next call to yylex(); • if it doesn’t return  scanning resumes immediately. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 3
  • 4. YYLVAL • YYlval is a variable that stores the token value. • The value is usually defined as a UNION  so that different kinds of tokens will have different types of values (ex. Integer value, floating point value, or a pointer to an entry in the symbol table). 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 4
  • 5. YYWRAP() 1. YYWrap Option: When the scanner receives an end-of-file indication from YY_INPUT, it then checks the `yywrap()' function: • If `yywrap()' returns false (zero)  it is assumed that the function has gone ahead and set up yyin to point to another input file, and scanning continues. • If it returns true (non-zero) the scanner terminates, returning 0 to its caller. Note that in either case, the start condition remains unchanged; it does not revert to INITIAL. • If you do not supply your own version of `yywrap()‘: • then you must either use `%option noyywrap' (in which case the scanner behaves as though `yywrap()' returned 1), • or you must link with `-lfl' to obtain the default version of the routine, which always returns 1. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 5
  • 6. BISON PARSING • Any bison parser makes a parse tree as it parses its input: • In some applications, it creates the tree as a data structure in memory for later use. • In others, the tree is just implicit in the sequence of operations the parser does. • In order to write a parser, we need some way to describe the rules the parser uses to turn a sequence of tokens into a parse tree. • The most common kind of language that computer parsers handle is a context-free grammar (CFG). 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 6
  • 7. GRAMMARS & PARSING • The parser’s job is to figure out the relationship among the input tokens. • A common way to display such relationships is a parse tree. (Ex. arithmetic expression 1 * 2 + 3 * 4 + 5) 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 7
  • 8. GRAMMARS & PARSING • Multiplication has higher precedence than addition, so : • the first two expressions are 1 * 2 and 3 * 4. • Then those two expressions are added together, and that sum is then added to 5. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 8
  • 9. GRAMMARS & PARSING • BNF for simple arithmetic expressions : • <exp> ::= <factor> | <exp> + <factor> • <factor> ::= NUMBER | <factor> * NUMBER 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 9
  • 10. GRAMMARS & PARSING • Each line is a rule that says how to create a branch of the parse tree. • In BNF, ::= can be read “is a” or “becomes” • and | is “or,” • The name on the left side of a rule is a symbol or term. • By convention, all tokens are considered to be symbols, but there are also symbols that are not tokens. • Useful BNF is invariably quite recursive, with rules that refer to themselves directly or indirectly. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 10
  • 11. BISON’S RULE INPUT LANGUAGE • Bison rules are basically BNF, with the punctuation simplified a little to make them easier to type. • Bison programs have three-part structure as flex programs, with declarations, rules, and C code. 1. The Declarations Section: • Includes C code to be copied to the beginning of the generated C parser, again enclosed in %{ and %}. • Following that are %token token declarations, telling bison the names of the symbols in the parser that are tokens. • By convention, tokens have uppercase names, although bison doesn’t require it. • Any symbols not declared as tokens have to appear on the left side of at least one rule in the program 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 11
  • 12. BISON’S RULE INPUT LANGUAGE 2. The Rules Section: • Includes rules in simplified BNF • Bison uses a single colon : rather than ::= • A semicolon marks the end of a rule. • like flex, the C action code goes in braces at the end of each rule. • The symbol on the left side of the first rule is the start symbol, the one that the entire input has to match. • There can be, and usually are, other rules with the same start symbol on the left. • Note: • Bison automatically does the parsing for you, remembering what rules have been matched, so the action code maintains the values associated with each symbol. • Bison parsers also perform side effects such as creating data structures for later use or, for example printing out results. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 12
  • 13. BISON’S RULE INPUT LANGUAGE 2. The Rules Section: • Each symbol in a bison rule has a value; the value of the target symbol (the one to the left of the colon) is called $$ in the action code • and the values on the right are numbered $1, $2, and so forth, up to the number of symbols in the rule. • The values of tokens are whatever was in yylval when the scanner returned the token 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 13
  • 14. EXAMPLE #1: BISON CALCUALTOR.Y FILE • Example # 1: Write Bison code, including the BNF, for the first version of our calculator. • Ensure to handle errors in case of syntax-error appears. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 14
  • 15. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 15 /* simplest version of calculator */ %{ #include <stdio.h> %} /* declare tokens */ %token NUMBER %token ADD SUB MUL DIV ABS %token EOL %% calclist: /* nothing */ | calclist exp EOL { printf("= %dn", $2); } EOL is end of an expression ; exp: factor default $$ = $1 | exp ADD factor { $$ = $1 + $3; } | exp SUB factor { $$ = $1 - $3; } ; factor: term default $$ = $1 | factor MUL term { $$ = $1 * $3; } | factor DIV term { $$ = $1 / $3; } ; term: NUMBER default $$ = $1 | ABS term { $$ = $2 >= 0? $2 : - $2; } ; %% main(int argc, char **argv) { ….. EXAMPLE#1: SIMPLE CALCULATOR BISON FILE
  • 16. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 16 /* simplest version of calculator */ ….. %% …. %% main(int argc, char **argv) { yyparse(); } yyerror(char *s) { printf(stderr, "error: %sn", s); } EXAMPLE#1: SIMPLE CALCULATOR BISON FILE (CONTINUED)
  • 17. EXERCISES : BISON.Y FILE(S) • E2: Alter example#1 to accept one-line comment (ex. // this is a comment ) • "//".* /* ignore comments */ • E3: Allow the calculator to recognize hexadecimal numbers • 0x[a-f0-9]+ • Use strtol function (Stdlib.h) to convert the string in c to long number • E3: Allow the calculator to understand the mod (%) operator - a%b where a and b are integers gives you the remainder when a is divided by b. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 17
  • 18. COMPILING FLEX AND BISON PROGRAMS TOGETHER 1. Include a header file that bison will create for us, which includes both definitions of the token numbers and a definition of yylval. 2. Include Bison’s header file in Flex’s file. %{ # include ”cal.tab.h" %} %% same rules as before, and no code in the third section 3. Delete the testing main routine in the third section of the scanner, since the parser will now call the scanner. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 18
  • 19. EXAMPLE #1 : FLEX AND BISON TOGETHER FOR CALCULATOR • The build process is now complex enough to be worth putting into a Makefile: • bison -d cal.y flex cal.l cc cal.tab.c lex.yy.c -lfl • Explanation: • First it runs bison with the -d (for “definitions” file) flag, which creates cal.tab.c and cal.tab.h, • and it runs flex to create lex.yy.c. • Then it compiles them together, along with the flex library. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 19
  • 20. EXAMPLE #1 : FLEX AND BISON TOGETHER FOR CALCULATOR • In this parser, the first two rules, which define the symbol calcset, implement a loop that reads an expression terminated by a newline and prints its value. • The definition of calclist uses a common two-rule recursive idiom to implement a sequence or list: 1. the first rule is empty and matches nothing; 2. the second adds an item to the list. The action in the second rule prints the value of the exp in $2. • In the absence of an explicit action on a rule, the parser assigns $1 to $$. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 20
  • 21. AMBIGUOUS GRAMMARS • Why shouldn’t we just write ? • exp: exp ADD exp | exp SUB exp | exp MUL exp | exp DIV exp | ABS exp | NUMBER ; 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 21 There are two answers: Precedence and Ambiguity
  • 22. AMBIGUOUS GRAMMARS • The separate symbols for term, factor, and exp tell bison to handle ABS, then MUL and DIV, then ADD and SUB. • In general, whenever a grammar has multiple levels of precedence where one operator binds “tighter” than another. 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 22
  • 23. AMBIGUOUS GRAMMARS • What about this? exp: exp ADD exp | exp SUB exp | factor ; • So, the example mentioned here is ambiguous as the expression 1 + 2 – 3 could be parsed (1+2)-3 or 1+(2-3)  two different expression with two different values • Bison will not parse an ambiguous grammar and report conflicts  Ambiguity is an error • That is, any parse that Bison creates has exactly only one way to parse any input 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 23
  • 24. SEMANTIC ANALYSIS (PARSING) • Validate expressions and taking appropriate actions • Generate code • Checks the source program for semantic errors • Gathers type information for the subsequent code-generation phase. • It uses the hierarchical structure determine by the Syntax- Analysis phase  to identify operators + operands of expressions and statements • Important Component: Type Checking • Ex. Using Real number as an index for an Array • If an Arithmetic operation is applied to two numbers, one is Real and the other is Integer  type conversion must be applied (intToReal) by adding an extra node 14-18/4/12 Department of Computer Science - Compiler Engineering Lab 24
  • 25. QUESTIONS? Thank you for listening  14-18/4/12 Department of Computer Science - Compiler Engineering Lab 25

Notas do Editor

  1. If a symbol neither is a token nor appears on the left side of a rule, it’s like an unreferenced variable in a C program. It doesn’t hurt anything, but it probably means the programmer made a mistake.