SlideShare una empresa de Scribd logo
1 de 22
By: 
admission.edhole.com
Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. 
Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these 
materials for their personal use. 
admission.edhole.com
Source 
code Scanner 
Parser 
 Checks the stream of words and their parts of speech 
(produced by the scanner) for grammatical correctness 
 Determines if the input is syntactically well formed 
 Guides checking at deeper levels than syntax 
 Builds an IR representation of the code 
Think of this as the mathematics of diagramming sentences 
IR 
Parser 
Errors 
tokens 
admission.edhole.com
The process of discovering a derivation for some 
sentence 
 Need a mathematical model of syntax — a grammar G 
 Need an algorithm for testing membership in L(G) 
 Need to keep in mind that our goal is building parsers, 
not studying the mathematics of arbitrary languages 
Roadmap 
1 Context-free grammars and derivations 
2 Top-down parsing 
◦ Hand-coded recursive descent parsers 
3 Bottom-up parsing 
◦ Generated LR(1) parsers 
admission.edhole.com
Context-free syntax is specified with a context-free grammar 
SheepNoise ® SheepNoise baa 
| baa 
This CFG defines the set of noises sheep normally make 
It is written in a variant of Backus–Naur form 
Formally, a grammar is a four tuple, G = (S,N,T,P) 
 S is the start symbol (set of strings in L(G)) 
 N is a set of non-terminal symbols (syntactic variables) 
 T is a set of terminal symbols (words) 
 P is a set of productions or rewrite rules (P : N ® (N È T)* ) 
Example due to Dr. Scott K. Warren 
admission.edhole.com
We can use the SheepNoise grammar to create 
sentences 
◦ use the productions as rewriting rules 
And so on ... 
While it is cute, this example quickly runs out of intellectual steam ... 
admission.edhole.com
To explore the uses of CFGs,we need a more complex grammar 
We denote this derivation: Expr Þ* id – num * id 
 Such a sequence of rewrites is called a derivation 
 Process of discovering a derivation is called parsing 
admission.edhole.com
 At each step, we choose a non-terminal to replace 
 Different choices can lead to different derivations 
Two derivations are of (particular) interest 
 Leftmost derivation — replace leftmost NT at each step 
 Rightmost derivation — replace rightmost NT at each 
step 
These are the two systematic derivations 
(We don’t care about randomly-ordered derivations!) 
The example on the preceding slide was a leftmost 
derivation 
 Of course, there is also a rightmost derivation 
 Interestingly, it turns out to be different 
admission.edhole.com
Leftmost derivation Rightmost derivation 
In both cases, Expr Þ* id – num * id 
 The two derivations produce different parse trees 
 The parse trees imply different evaluation orders! 
admission.edhole.com
Leftmost derivation 
G 
x 
E 
E Op 
– 
E 
2 
E 
E 
y 
Op 
* 
This evaluates as x – ( 2 * y ) 
admission.edhole.com
Rightmost derivation 
x 2 
G 
E 
E Op E 
E Op E y 
– 
* 
This evaluates as ( x – 2 ) * y 
admission.edhole.com
These two derivations point out a problem with the grammar: 
It has no notion of precedence, or implied order of evaluation 
To add precedence 
 Create a non-terminal for each level of precedence 
 Isolate the corresponding part of the grammar 
 Force the parser to recognize high precedence 
subexpressions first 
For algebraic expressions 
 Multiplication and division, first (level one) 
 Subtraction and addition, next (level two) 
admission.edhole.com
Adding the standard algebraic precedence 
produces: 
This grammar is slightly larger 
• Takes more rewriting to reach 
some of the terminal symbols 
• Encodes expected precedence 
• Produces same parse tree 
under leftmost & rightmost 
derivations 
Let’s see how it parses x - 2 * y 
level 
two 
level 
one 
admission.edhole.com
The rightmost derivation 
G 
E 
E – 
T 
F 
<id,x> 
T 
T 
F 
* F 
<num,2> 
<id,y> 
Its parse tree 
This produces x – ( 2 * y ), along with an appropriate parse tree. 
Both the leftmost and rightmost derivations give the same expression, 
because the grammar directly encodes the desired precedence. 
admission.edhole.com
Our original expression grammar had other problems 
 This grammar allows multiple leftmost derivations for x - 2 
* y 
 Hard to automate derivation if > 1 choice 
 The grammar is ambiguous choice different 
from the first time 
admission.edhole.com
The Difference: 
 Different productions chosen on the second step 
Original choice New choice 
 Both derivations succeed in producing x - 2 * y 
admission.edhole.com
Definitions 
 If a grammar has more than one leftmost derivation for 
a single sentential form, the grammar is ambiguous 
 If a grammar has more than one rightmost derivation 
for a single sentential form, the grammar is ambiguous 
 The leftmost and rightmost derivations for a sentential 
form may differ, even in an unambiguous grammar 
Classic example — the if-then-else problem 
Stmt ® if Expr then Stmt 
| if Expr then Stmt else Stmt 
| … other stmts … 
This ambiguity is entirely grammatical in nature 
admission.edhole.com
2 
1 
This sentential form has two derivations 
2 
ttrif Expr1 then if Exp then Stm else Stm if 
then 
then 
else 
if 
E1 
E2 
S2 
S1 
production 2, then 
production 1 
if 
then 
if 
then 
E1 
E2 
S1 
else 
S2 
production 1, then 
production 2 
admission.edhole.com
Removing the ambiguity 
 Must rewrite the grammar to avoid generating the 
problem 
 Match each else to innermost unmatched if (common sense 
rule) 
1 Stmt ® WithElse 
2 | NoElse 
3 WithElse ® if Expr then WithElse else WithElse 
4 | OtherStmt 
5 NoElse ® if Expr then Stmt 
6 | if Expr then WithElse else NoElse 
Intuition: a NoElse always has no else on its 
last cascaded else if statement 
With this grammar, the example has only one derivation 
admission.edhole.com
if Expr1 then if Expr2 then Stmt1 else Stmt2 
Rule Sentential Form 
— Stmt 
2 NoElse 
5 if Expr then Stmt 
? if E1 then Stmt 
1 if E1 then WithElse 
3 if E1 then if Expr then WithElse else WithElse 
? if E1 then if E2 then WithElse else WithElse 
4 if E1 then if E2 then S1 else WithElse 
4 if E1 then if E2 then S1 else S2 
admission.edhole.com 
This binds the else controlling S2 to the inner if
Ambiguity usually refers to confusion in the CFG 
Overloading can create deeper ambiguity 
a = f(17) 
In many Algol-like languages, f could be either a function or 
a subscripted variable 
Disambiguating this one requires context 
 Need values of declarations 
 Really an issue of type, not context-free syntax 
 Requires an extra-grammatical solution (not in CFG) 
 Must handle these with a different mechanism 
◦ Step outside grammar rather than use a more complex 
grammar 
admission.edhole.com
Ambiguity arises from two distinct sources 
 Confusion in the context-free syntax (if-then-else) 
 Confusion that requires context to resolve (overloading) 
Resolving ambiguity 
 To remove context-free ambiguity, rewrite the grammar 
 To handle context-sensitive ambiguity takes cooperation 
◦ Knowledge of declarations, types, … 
◦ Accept a superset of L(G) & check it by other means† 
◦ This is a language design problem 
Sometimes, the compiler writer accepts an ambiguous 
grammar 
◦ Parsing techniques that “do the right thing” 
◦ i.e., always select the same derivation 
†See Chapter 4 admission.edhole.com

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Syntax analyzer
Syntax analyzerSyntax analyzer
Syntax analyzer
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Context Free Grammar
Context Free GrammarContext Free Grammar
Context Free Grammar
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Introduction to fa and dfa
Introduction to fa  and dfaIntroduction to fa  and dfa
Introduction to fa and dfa
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Parsing
ParsingParsing
Parsing
 
Dfa basics
Dfa basicsDfa basics
Dfa basics
 
A simple approach of lexical analyzers
A simple approach of lexical analyzersA simple approach of lexical analyzers
A simple approach of lexical analyzers
 
Ch3
Ch3Ch3
Ch3
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
Theory of Computation Lecture Notes
Theory of Computation Lecture NotesTheory of Computation Lecture Notes
Theory of Computation Lecture Notes
 
Automata Theory
Automata TheoryAutomata Theory
Automata Theory
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lecture 02 lexical analysis
Lecture 02 lexical analysisLecture 02 lexical analysis
Lecture 02 lexical analysis
 
Language for specifying lexical Analyzer
Language for specifying lexical AnalyzerLanguage for specifying lexical Analyzer
Language for specifying lexical Analyzer
 
Ch3 4 regular expression and grammar
Ch3 4 regular expression and grammarCh3 4 regular expression and grammar
Ch3 4 regular expression and grammar
 
Compiler Designs
Compiler DesignsCompiler Designs
Compiler Designs
 
Lexical analyzer
Lexical analyzerLexical analyzer
Lexical analyzer
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 

Similar a B.tech admission in india

Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysisIffat Anjum
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docxvenkatapranaykumarGa
 
Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxTameneTamire
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsgadisaAdamu
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...movocode
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptFamiDan
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfeliasabdi2024
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsMarkus Voelter
 
The Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxThe Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxssuser039bf6
 
05-transformers.pdf
05-transformers.pdf05-transformers.pdf
05-transformers.pdfChaoYang81
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfarpitaeron555
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptxvenkatapranaykumarGa
 
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 Keyappasami
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01riddhi viradiya
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.pptAmrita Sharma
 

Similar a B.tech admission in india (20)

Module 11
Module 11Module 11
Module 11
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx8-Practice problems on operator precedence parser-24-05-2023.docx
8-Practice problems on operator precedence parser-24-05-2023.docx
 
Ch3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptxCh3_Syntax Analysis.pptx
Ch3_Syntax Analysis.pptx
 
Chapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materialsChapter-3 compiler.pptx course materials
Chapter-3 compiler.pptx course materials
 
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
6-Role of Parser, Construction of Parse Tree and Elimination of Ambiguity-06-...
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx3. Syntax Analyzer.pptx
3. Syntax Analyzer.pptx
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
New compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdfNew compiler design 101 April 13 2024.pdf
New compiler design 101 April 13 2024.pdf
 
The future of DSLs - functions and formal methods
The future of DSLs - functions and formal methodsThe future of DSLs - functions and formal methods
The future of DSLs - functions and formal methods
 
Syntax part1
Syntax part1Syntax part1
Syntax part1
 
The Theory of Finite Automata.pptx
The Theory of Finite Automata.pptxThe Theory of Finite Automata.pptx
The Theory of Finite Automata.pptx
 
05-transformers.pdf
05-transformers.pdf05-transformers.pdf
05-transformers.pdf
 
match the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdfmatch the following attributes to the parts of a compilerstrips ou.pdf
match the following attributes to the parts of a compilerstrips ou.pdf
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
 
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
 
Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01Unitiv 111206005201-phpapp01
Unitiv 111206005201-phpapp01
 
Programming_Language_Syntax.ppt
Programming_Language_Syntax.pptProgramming_Language_Syntax.ppt
Programming_Language_Syntax.ppt
 

Más de Edhole.com

Chartered accountant in dwarka
Chartered accountant in dwarkaChartered accountant in dwarka
Chartered accountant in dwarkaEdhole.com
 
Ca firm in dwarka
Ca firm in dwarkaCa firm in dwarka
Ca firm in dwarkaEdhole.com
 
Website development company surat
Website development company suratWebsite development company surat
Website development company suratEdhole.com
 
Website designing company in surat
Website designing company in suratWebsite designing company in surat
Website designing company in suratEdhole.com
 
Website dsigning company in india
Website dsigning company in indiaWebsite dsigning company in india
Website dsigning company in indiaEdhole.com
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhiEdhole.com
 
Chartered accountant in dwarka
Chartered accountant in dwarkaChartered accountant in dwarka
Chartered accountant in dwarkaEdhole.com
 
Ca firm in dwarka
Ca firm in dwarkaCa firm in dwarka
Ca firm in dwarkaEdhole.com
 
Website development company surat
Website development company suratWebsite development company surat
Website development company suratEdhole.com
 
Website designing company in surat
Website designing company in suratWebsite designing company in surat
Website designing company in suratEdhole.com
 
Website designing company in india
Website designing company in indiaWebsite designing company in india
Website designing company in indiaEdhole.com
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhiEdhole.com
 
Website designing company in mumbai
Website designing company in mumbaiWebsite designing company in mumbai
Website designing company in mumbaiEdhole.com
 
Website development company surat
Website development company suratWebsite development company surat
Website development company suratEdhole.com
 
Website desinging company in surat
Website desinging company in suratWebsite desinging company in surat
Website desinging company in suratEdhole.com
 
Website designing company in india
Website designing company in indiaWebsite designing company in india
Website designing company in indiaEdhole.com
 

Más de Edhole.com (20)

Ca in patna
Ca in patnaCa in patna
Ca in patna
 
Chartered accountant in dwarka
Chartered accountant in dwarkaChartered accountant in dwarka
Chartered accountant in dwarka
 
Ca in dwarka
Ca in dwarkaCa in dwarka
Ca in dwarka
 
Ca firm in dwarka
Ca firm in dwarkaCa firm in dwarka
Ca firm in dwarka
 
Website development company surat
Website development company suratWebsite development company surat
Website development company surat
 
Website designing company in surat
Website designing company in suratWebsite designing company in surat
Website designing company in surat
 
Website dsigning company in india
Website dsigning company in indiaWebsite dsigning company in india
Website dsigning company in india
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhi
 
Ca in patna
Ca in patnaCa in patna
Ca in patna
 
Chartered accountant in dwarka
Chartered accountant in dwarkaChartered accountant in dwarka
Chartered accountant in dwarka
 
Ca firm in dwarka
Ca firm in dwarkaCa firm in dwarka
Ca firm in dwarka
 
Ca in dwarka
Ca in dwarkaCa in dwarka
Ca in dwarka
 
Website development company surat
Website development company suratWebsite development company surat
Website development company surat
 
Website designing company in surat
Website designing company in suratWebsite designing company in surat
Website designing company in surat
 
Website designing company in india
Website designing company in indiaWebsite designing company in india
Website designing company in india
 
Website designing company in delhi
Website designing company in delhiWebsite designing company in delhi
Website designing company in delhi
 
Website designing company in mumbai
Website designing company in mumbaiWebsite designing company in mumbai
Website designing company in mumbai
 
Website development company surat
Website development company suratWebsite development company surat
Website development company surat
 
Website desinging company in surat
Website desinging company in suratWebsite desinging company in surat
Website desinging company in surat
 
Website designing company in india
Website designing company in indiaWebsite designing company in india
Website designing company in india
 

Último

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
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
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 

Último (20)

Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
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
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
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
 
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
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
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
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 

B.tech admission in india

  • 2. Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. admission.edhole.com
  • 3. Source code Scanner Parser  Checks the stream of words and their parts of speech (produced by the scanner) for grammatical correctness  Determines if the input is syntactically well formed  Guides checking at deeper levels than syntax  Builds an IR representation of the code Think of this as the mathematics of diagramming sentences IR Parser Errors tokens admission.edhole.com
  • 4. The process of discovering a derivation for some sentence  Need a mathematical model of syntax — a grammar G  Need an algorithm for testing membership in L(G)  Need to keep in mind that our goal is building parsers, not studying the mathematics of arbitrary languages Roadmap 1 Context-free grammars and derivations 2 Top-down parsing ◦ Hand-coded recursive descent parsers 3 Bottom-up parsing ◦ Generated LR(1) parsers admission.edhole.com
  • 5. Context-free syntax is specified with a context-free grammar SheepNoise ® SheepNoise baa | baa This CFG defines the set of noises sheep normally make It is written in a variant of Backus–Naur form Formally, a grammar is a four tuple, G = (S,N,T,P)  S is the start symbol (set of strings in L(G))  N is a set of non-terminal symbols (syntactic variables)  T is a set of terminal symbols (words)  P is a set of productions or rewrite rules (P : N ® (N È T)* ) Example due to Dr. Scott K. Warren admission.edhole.com
  • 6. We can use the SheepNoise grammar to create sentences ◦ use the productions as rewriting rules And so on ... While it is cute, this example quickly runs out of intellectual steam ... admission.edhole.com
  • 7. To explore the uses of CFGs,we need a more complex grammar We denote this derivation: Expr Þ* id – num * id  Such a sequence of rewrites is called a derivation  Process of discovering a derivation is called parsing admission.edhole.com
  • 8.  At each step, we choose a non-terminal to replace  Different choices can lead to different derivations Two derivations are of (particular) interest  Leftmost derivation — replace leftmost NT at each step  Rightmost derivation — replace rightmost NT at each step These are the two systematic derivations (We don’t care about randomly-ordered derivations!) The example on the preceding slide was a leftmost derivation  Of course, there is also a rightmost derivation  Interestingly, it turns out to be different admission.edhole.com
  • 9. Leftmost derivation Rightmost derivation In both cases, Expr Þ* id – num * id  The two derivations produce different parse trees  The parse trees imply different evaluation orders! admission.edhole.com
  • 10. Leftmost derivation G x E E Op – E 2 E E y Op * This evaluates as x – ( 2 * y ) admission.edhole.com
  • 11. Rightmost derivation x 2 G E E Op E E Op E y – * This evaluates as ( x – 2 ) * y admission.edhole.com
  • 12. These two derivations point out a problem with the grammar: It has no notion of precedence, or implied order of evaluation To add precedence  Create a non-terminal for each level of precedence  Isolate the corresponding part of the grammar  Force the parser to recognize high precedence subexpressions first For algebraic expressions  Multiplication and division, first (level one)  Subtraction and addition, next (level two) admission.edhole.com
  • 13. Adding the standard algebraic precedence produces: This grammar is slightly larger • Takes more rewriting to reach some of the terminal symbols • Encodes expected precedence • Produces same parse tree under leftmost & rightmost derivations Let’s see how it parses x - 2 * y level two level one admission.edhole.com
  • 14. The rightmost derivation G E E – T F <id,x> T T F * F <num,2> <id,y> Its parse tree This produces x – ( 2 * y ), along with an appropriate parse tree. Both the leftmost and rightmost derivations give the same expression, because the grammar directly encodes the desired precedence. admission.edhole.com
  • 15. Our original expression grammar had other problems  This grammar allows multiple leftmost derivations for x - 2 * y  Hard to automate derivation if > 1 choice  The grammar is ambiguous choice different from the first time admission.edhole.com
  • 16. The Difference:  Different productions chosen on the second step Original choice New choice  Both derivations succeed in producing x - 2 * y admission.edhole.com
  • 17. Definitions  If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous  If a grammar has more than one rightmost derivation for a single sentential form, the grammar is ambiguous  The leftmost and rightmost derivations for a sentential form may differ, even in an unambiguous grammar Classic example — the if-then-else problem Stmt ® if Expr then Stmt | if Expr then Stmt else Stmt | … other stmts … This ambiguity is entirely grammatical in nature admission.edhole.com
  • 18. 2 1 This sentential form has two derivations 2 ttrif Expr1 then if Exp then Stm else Stm if then then else if E1 E2 S2 S1 production 2, then production 1 if then if then E1 E2 S1 else S2 production 1, then production 2 admission.edhole.com
  • 19. Removing the ambiguity  Must rewrite the grammar to avoid generating the problem  Match each else to innermost unmatched if (common sense rule) 1 Stmt ® WithElse 2 | NoElse 3 WithElse ® if Expr then WithElse else WithElse 4 | OtherStmt 5 NoElse ® if Expr then Stmt 6 | if Expr then WithElse else NoElse Intuition: a NoElse always has no else on its last cascaded else if statement With this grammar, the example has only one derivation admission.edhole.com
  • 20. if Expr1 then if Expr2 then Stmt1 else Stmt2 Rule Sentential Form — Stmt 2 NoElse 5 if Expr then Stmt ? if E1 then Stmt 1 if E1 then WithElse 3 if E1 then if Expr then WithElse else WithElse ? if E1 then if E2 then WithElse else WithElse 4 if E1 then if E2 then S1 else WithElse 4 if E1 then if E2 then S1 else S2 admission.edhole.com This binds the else controlling S2 to the inner if
  • 21. Ambiguity usually refers to confusion in the CFG Overloading can create deeper ambiguity a = f(17) In many Algol-like languages, f could be either a function or a subscripted variable Disambiguating this one requires context  Need values of declarations  Really an issue of type, not context-free syntax  Requires an extra-grammatical solution (not in CFG)  Must handle these with a different mechanism ◦ Step outside grammar rather than use a more complex grammar admission.edhole.com
  • 22. Ambiguity arises from two distinct sources  Confusion in the context-free syntax (if-then-else)  Confusion that requires context to resolve (overloading) Resolving ambiguity  To remove context-free ambiguity, rewrite the grammar  To handle context-sensitive ambiguity takes cooperation ◦ Knowledge of declarations, types, … ◦ Accept a superset of L(G) & check it by other means† ◦ This is a language design problem Sometimes, the compiler writer accepts an ambiguous grammar ◦ Parsing techniques that “do the right thing” ◦ i.e., always select the same derivation †See Chapter 4 admission.edhole.com