SlideShare a Scribd company logo
1 of 20
1
Syntax Analyzer
• Syntax Analyzer creates the syntactic structure of the given source program.
• This syntactic structure is mostly a parse tree.
• Syntax Analyzer is also known as parser.
• The syntax of a programming is described by a context-free grammar (CFG). We will
use BNF (Backus-Naur Form) notation in the description of CFGs.
• The syntax analyzer (parser) checks whether a given source program satisfies the rules
implied by a context-free grammar or not.
– If it satisfies, the parser creates the parse tree of that program.
– Otherwise the parser gives the error messages.
• A context-free grammar
– gives a precise syntactic specification of a programming language.
– the design of the grammar is an initial phase of the design of a compiler.
– a grammar can be directly converted into a parser by some tools.
2
Parser
Lexical
Analyzer
Parsersource
program
token
get next token
parse tree
• Parser works on a stream of tokens.
• The smallest item is a token.
3
Parsers (cont.)
• We categorize the parsers into two groups:
1. Top-Down Parser
– the parse tree is created top to bottom, starting from the root.
1. Bottom-Up Parser
– the parse is created bottom to top; starting from the leaves
• Both top-down and bottom-up parsers scan the input from left to right
(one symbol at a time).
• Efficient top-down and bottom-up parsers can be implemented only for
sub-classes of context-free grammars.
– LL for top-down parsing
– LR for bottom-up parsing
4
Context-Free Grammars
• Inherently recursive structures of a programming language are defined
by a context-free grammar.
• In a context-free grammar, we have:
– A finite set of terminals (in our case, this will be the set of tokens)
– A finite set of non-terminals (syntactic-variables)
– A finite set of productions rules in the following form
• A → α where A is a non-terminal and
α is a string of terminals and non-terminals (including the empty string)
– A start symbol (one of the non-terminal symbol)
• Example:
E → E + E | E – E | E * E | E / E | - E
E → ( E )
E → id
5
Derivations
E ⇒ E+E
• E+E derives from E
– we can replace E by E+E
– to able to do this, we have to have a production rule E→E+E in our grammar.
E ⇒ E+E ⇒ id+E ⇒ id+id
• A sequence of replacements of non-terminal symbols is called a derivation of id+id from E.
• In general a derivation step is
αAβ ⇒ αγβ if there is a production rule A→γ in our grammar
where α and β are arbitrary strings of terminal and non-terminal symbols
α1⇒ α2⇒ ... ⇒ αn (αn derives from α1 or α1 derives αn)
⇒ : derives in one step
⇒ : derives in zero or more steps
⇒ : derives in one or more steps
*
+
6
CFG - Terminology
• L(G) is the language of G (the language generated by G) which is a set
of sentences.
• A sentence of L(G) is a string of terminal symbols of G.
• If S is the start symbol of G then
ω is a sentence of L(G) iff S ⇒ ω where ω is a string of terminals of G.
• If G is a context-free grammar, L(G) is a context-free language.
• Two grammars are equivalent if they produce the same language.
• S ⇒ α - If α contains non-terminals, it is called as a sentential form of G.
- If α does not contain non-terminals, it is called as a sentence of G.
+
*
7
Derivation Example
E ⇒ -E ⇒ -(E) ⇒ -(E+E) ⇒ -(id+E) ⇒ -(id+id)
OR
E ⇒ -E ⇒ -(E) ⇒ -(E+E) ⇒ -(E+id) ⇒ -(id+id)
• At each derivation step, we can choose any of the non-terminal in the sentential form
of G for the replacement.
• If we always choose the left-most non-terminal in each derivation step, this derivation
is called as left-most derivation.
• If we always choose the right-most non-terminal in each derivation step, this
derivation is called as right-most derivation.
8
Left-Most and Right-Most Derivations
Left-Most Derivation
E ⇒ -E ⇒ -(E) ⇒ -(E+E) ⇒ -(id+E) ⇒ -(id+id)
Right-Most Derivation
E ⇒ -E ⇒ -(E) ⇒ -(E+E) ⇒ -(E+id) ⇒ -(id+id)
• We will see that the top-down parsers try to find the left-most
derivation of the given source program.
• We will see that the bottom-up parsers try to find the right-most
derivation of the given source program in the reverse order.
lmlmlmlmlm
rmrmrmrmrm
9
Parse Tree
• Inner nodes of a parse tree are non-terminal symbols.
• The leaves of a parse tree are terminal symbols.
• A parse tree can be seen as a graphical representation of a derivation.
E ⇒ -E E
E-
E
E
EE
E
+
-
( )
E
E
E-
( )
E
E
id
E
E
E +
-
( )
id
E
E
E
EE +
-
( )
id
⇒ -(E) ⇒ -(E+E)
⇒ -(id+E) ⇒ -(id+id)
10
Ambiguity
• A grammar produces more than one parse tree for a sentence is
called as an ambiguous grammar.
E ⇒ E+E ⇒ id+E ⇒ id+E*E
⇒ id+id*E ⇒ id+id*id
E ⇒ E*E ⇒ E+E*E ⇒ id+E*E
⇒ id+id*E ⇒ id+id*id
E
id
E +
id
id
E
E
* E
E
E +
id E
E
* E
id id
11
Ambiguity (cont.)
• For the most parsers, the grammar must be unambiguous.
• unambiguous grammar
 unique selection of the parse tree for a sentence
• We should eliminate the ambiguity in the grammar during the design
phase of the compiler.
• An unambiguous grammar should be written to eliminate the ambiguity.
• We have to prefer one of the parse trees of a sentence (generated by an
ambiguous grammar) to disambiguate that grammar to restrict to this
choice.
12
Ambiguity (cont.)
stmt → if expr then stmt |
if expr then stmt else stmt | otherstmts
if E1 then if E2 then S1 else S2
stmt
if expr then stmt else stmt
E1 if expr then stmt S2
E2 S1
stmt
if expr then stmt
E1 if expr then stmt else stmt
E2 S1 S2
1 2
13
Ambiguity (cont.)
• We prefer the second parse tree (else matches with closest if).
• So, we have to disambiguate our grammar to reflect this choice.
• The unambiguous grammar will be:
stmt → matchedstmt | unmatchedstmt
matchedstmt → if expr then matchedstmt else matchedstmt | otherstmts
unmatchedstmt → if expr then stmt |
if expr then matchedstmt else unmatchedstmt
14
Ambiguity – Operator Precedence
• Ambiguous grammars (because of ambiguous operators) can be
disambiguated according to the precedence and associativity rules.
E → E+E | E*E | E^E | id | (E)
disambiguate the grammar
precedence: ^ (right to left)
* (left to right)
+ (left to right)
E → E+T | T
T → T*F | F
F → G^F | G
G → id | (E)
⇓
15
Left Recursion
• A grammar is left recursive if it has a non-terminal A such that there is
a derivation.
A ⇒ Aα for some string α
• Top-down parsing techniques cannot handle left-recursive grammars.
• So, we have to convert our left-recursive grammar into an equivalent
grammar which is not left-recursive.
• The left-recursion may appear in a single step of the derivation
(immediate left-recursion), or may appear in more than one step of
the derivation.
+
16
Immediate Left-Recursion
A → A α | β where β does not start with A
⇓ eliminate immediate left recursion
A → β A’
A’
→ α A’
| ε an equivalent grammar
A → A α1 | ... | A αm | β1 | ... | βn where β1 ... βn do not start with A
⇓ eliminate immediate left recursion
A → β1 A’
| ... | βn A’
A’
→ α1 A’
| ... | αm A’
| ε an equivalent grammar
In general,
17
Immediate Left-Recursion -- Example
E → E+T | T
T → T*F | F
F → id | (E)
E → T E’
E’
→ +T E’
| ε
T → F T’
T’
→ *F T’
| ε
F → id | (E)
⇓ eliminate immediate left recursion
18
Left-Recursion -- Problem
• A grammar cannot be immediately left-recursive, but it still can be
left-recursive.
• By just eliminating the immediate left-recursion, we may not get
a grammar which is not left-recursive.
S → Aa | b
A → Sc | d This grammar is not immediately left-recursive,
but it is still left-recursive.
S ⇒ Aa ⇒ Sca or
A ⇒ Sc ⇒ Aac causes to a left-recursion
• So, we have to eliminate all left-recursions from our grammar
19
Eliminate Left-Recursion -- Example
S → Aa | b
A → Ac | Sd | f
- Order of non-terminals: S, A
for S:
- we do not enter the inner loop.
- there is no immediate left recursion in S.
for A:
- Replace A → Sd with A → Aad | bd
So, we will have A → Ac | Aad | bd | f
- Eliminate the immediate left-recursion in A
A → bdA’
| fA’
A’
→ cA’
| adA’
| ε
So, the resulting equivalent grammar which is not left-recursive is:
S → Aa | b
A → bdA’
| fA’
A’
→ cA’
| adA’
| ε
20
Eliminate Left-Recursion – Example2
S → Aa | b
A → Ac | Sd | f
- Order of non-terminals: A, S
for A:
- we do not enter the inner loop.
- Eliminate the immediate left-recursion in A
A → SdA’
| fA’
A’
→ cA’
| ε
for S:
- Replace S → Aa with S → SdA’
a | fA’
a
So, we will have S → SdA’
a | fA’
a | b
- Eliminate the immediate left-recursion in S
S → fA’aS’ | bS’
S’
→ dA’
aS’ | ε
So, the resulting equivalent grammar which is not left-recursive is:
S → fA’aS’ | bS’
S’
→ dA’
aS’ | ε
A → SdA’
| fA’
A’
→ cA’
| ε

More Related Content

What's hot

CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR Zahid Parvez
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysisDattatray Gandhmal
 
context free language
context free languagecontext free language
context free languagekhush_boo31
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauagesdanhumble
 
Context free langauges
Context free langaugesContext free langauges
Context free langaugessudhir sharma
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler DesignAkhil Kaushik
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxDrIsikoIsaac
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal formsAkila Krishnamoorthy
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computationBipul Roy Bpl
 
Implementation of lexical analyser
Implementation of lexical analyserImplementation of lexical analyser
Implementation of lexical analyserArchana Gopinath
 
Introduction to fa and dfa
Introduction to fa  and dfaIntroduction to fa  and dfa
Introduction to fa and dfadeepinderbedi
 
Automata
AutomataAutomata
AutomataGaditek
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsEran Zimbler
 

What's hot (20)

CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR CONTEXT FREE GRAMMAR
CONTEXT FREE GRAMMAR
 
Context free grammar
Context free grammarContext free grammar
Context free grammar
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Finite automata-for-lexical-analysis
Finite automata-for-lexical-analysisFinite automata-for-lexical-analysis
Finite automata-for-lexical-analysis
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Optimization of dfa
Optimization of dfaOptimization of dfa
Optimization of dfa
 
context free language
context free languagecontext free language
context free language
 
2. context free langauages
2. context free langauages2. context free langauages
2. context free langauages
 
Context free langauges
Context free langaugesContext free langauges
Context free langauges
 
Parsing in Compiler Design
Parsing in Compiler DesignParsing in Compiler Design
Parsing in Compiler Design
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
 
Automata theory - CFG and normal forms
Automata theory - CFG and normal formsAutomata theory - CFG and normal forms
Automata theory - CFG and normal forms
 
Regular expressions-Theory of computation
Regular expressions-Theory of computationRegular expressions-Theory of computation
Regular expressions-Theory of computation
 
Implementation of lexical analyser
Implementation of lexical analyserImplementation of lexical analyser
Implementation of lexical analyser
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Introduction to fa and dfa
Introduction to fa  and dfaIntroduction to fa  and dfa
Introduction to fa and dfa
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Automata
AutomataAutomata
Automata
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 

Viewers also liked

Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machinesVlad Patryshev
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Iffat Anjum
 
Syntax and semantics of propositional logic
Syntax and semantics of propositional logicSyntax and semantics of propositional logic
Syntax and semantics of propositional logicJanet Stemwedel
 
Summary - Transformational-Generative Theory
Summary - Transformational-Generative TheorySummary - Transformational-Generative Theory
Summary - Transformational-Generative TheoryMarielis VI
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysisRicha Sharma
 
grammaticality, deep & surface structure, and ambiguity
grammaticality, deep & surface structure, and ambiguitygrammaticality, deep & surface structure, and ambiguity
grammaticality, deep & surface structure, and ambiguityDedew Deviarini
 

Viewers also liked (18)

Parsing
ParsingParsing
Parsing
 
Module 11
Module 11Module 11
Module 11
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machines
 
Ch4b
Ch4bCh4b
Ch4b
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Lesson2
Lesson2Lesson2
Lesson2
 
Pushdown automata
Pushdown automataPushdown automata
Pushdown automata
 
Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3Lecture 06 syntax analysis 3
Lecture 06 syntax analysis 3
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Parsing
ParsingParsing
Parsing
 
Su 2012 ss syntax(1)
Su 2012 ss syntax(1)Su 2012 ss syntax(1)
Su 2012 ss syntax(1)
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Ch5a
Ch5aCh5a
Ch5a
 
Syntax and semantics of propositional logic
Syntax and semantics of propositional logicSyntax and semantics of propositional logic
Syntax and semantics of propositional logic
 
Summary - Transformational-Generative Theory
Summary - Transformational-Generative TheorySummary - Transformational-Generative Theory
Summary - Transformational-Generative Theory
 
pushdown automata
pushdown automatapushdown automata
pushdown automata
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
 
grammaticality, deep & surface structure, and ambiguity
grammaticality, deep & surface structure, and ambiguitygrammaticality, deep & surface structure, and ambiguity
grammaticality, deep & surface structure, and ambiguity
 

Similar to Lexical 2

Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptFamiDan
 
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
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdfwigewej294
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
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
 
04 Syntax Analysis.pdf
04 Syntax Analysis.pdf04 Syntax Analysis.pdf
04 Syntax Analysis.pdfmovamag594
 
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
 
Syntax Analysis.pdf
Syntax Analysis.pdfSyntax Analysis.pdf
Syntax Analysis.pdfShivang70701
 
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
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysisIffat Anjum
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdfkenilpatel65
 
Natural Language Processing - Writing Grammar
Natural Language Processing - Writing GrammarNatural Language Processing - Writing Grammar
Natural Language Processing - Writing GrammarJasmine Peniel
 

Similar to Lexical 2 (20)

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
 
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
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdf
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
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-...
 
04 Syntax Analysis.pdf
04 Syntax Analysis.pdf04 Syntax Analysis.pdf
04 Syntax Analysis.pdf
 
Parsing
ParsingParsing
Parsing
 
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
 
Syntax Analysis.pdf
Syntax Analysis.pdfSyntax Analysis.pdf
Syntax Analysis.pdf
 
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
 
3b. LMD & RMD.pdf
3b. LMD & RMD.pdf3b. LMD & RMD.pdf
3b. LMD & RMD.pdf
 
Lecture 04 syntax analysis
Lecture 04 syntax analysisLecture 04 syntax analysis
Lecture 04 syntax analysis
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Syntax Analyzer.pdf
Syntax Analyzer.pdfSyntax Analyzer.pdf
Syntax Analyzer.pdf
 
Ch06
Ch06Ch06
Ch06
 
Natural Language Processing - Writing Grammar
Natural Language Processing - Writing GrammarNatural Language Processing - Writing Grammar
Natural Language Processing - Writing Grammar
 

Recently uploaded

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operationalssuser3e220a
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxRosabel UA
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Seán Kennedy
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4JOYLYNSAMANIEGO
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsRommel Regala
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management SystemChristalin Nelson
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxJanEmmanBrigoli
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationRosabel UA
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSJoshuaGantuangco2
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSMae Pangan
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxVanesaIglesias10
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 

Recently uploaded (20)

Expanded definition: technical and operational
Expanded definition: technical and operationalExpanded definition: technical and operational
Expanded definition: technical and operational
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Presentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptxPresentation Activity 2. Unit 3 transv.pptx
Presentation Activity 2. Unit 3 transv.pptx
 
Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...Student Profile Sample - We help schools to connect the data they have, with ...
Student Profile Sample - We help schools to connect the data they have, with ...
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4Daily Lesson Plan in Mathematics Quarter 4
Daily Lesson Plan in Mathematics Quarter 4
 
The Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World PoliticsThe Contemporary World: The Globalization of World Politics
The Contemporary World: The Globalization of World Politics
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Transaction Management in Database Management System
Transaction Management in Database Management SystemTransaction Management in Database Management System
Transaction Management in Database Management System
 
Millenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptxMillenials and Fillennials (Ethical Challenge and Responses).pptx
Millenials and Fillennials (Ethical Challenge and Responses).pptx
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
Activity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translationActivity 2-unit 2-update 2024. English translation
Activity 2-unit 2-update 2024. English translation
 
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTSGRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
GRADE 4 - SUMMATIVE TEST QUARTER 4 ALL SUBJECTS
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
Textual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHSTextual Evidence in Reading and Writing of SHS
Textual Evidence in Reading and Writing of SHS
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
ROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptxROLES IN A STAGE PRODUCTION in arts.pptx
ROLES IN A STAGE PRODUCTION in arts.pptx
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 

Lexical 2

  • 1. 1 Syntax Analyzer • Syntax Analyzer creates the syntactic structure of the given source program. • This syntactic structure is mostly a parse tree. • Syntax Analyzer is also known as parser. • The syntax of a programming is described by a context-free grammar (CFG). We will use BNF (Backus-Naur Form) notation in the description of CFGs. • The syntax analyzer (parser) checks whether a given source program satisfies the rules implied by a context-free grammar or not. – If it satisfies, the parser creates the parse tree of that program. – Otherwise the parser gives the error messages. • A context-free grammar – gives a precise syntactic specification of a programming language. – the design of the grammar is an initial phase of the design of a compiler. – a grammar can be directly converted into a parser by some tools.
  • 2. 2 Parser Lexical Analyzer Parsersource program token get next token parse tree • Parser works on a stream of tokens. • The smallest item is a token.
  • 3. 3 Parsers (cont.) • We categorize the parsers into two groups: 1. Top-Down Parser – the parse tree is created top to bottom, starting from the root. 1. Bottom-Up Parser – the parse is created bottom to top; starting from the leaves • Both top-down and bottom-up parsers scan the input from left to right (one symbol at a time). • Efficient top-down and bottom-up parsers can be implemented only for sub-classes of context-free grammars. – LL for top-down parsing – LR for bottom-up parsing
  • 4. 4 Context-Free Grammars • Inherently recursive structures of a programming language are defined by a context-free grammar. • In a context-free grammar, we have: – A finite set of terminals (in our case, this will be the set of tokens) – A finite set of non-terminals (syntactic-variables) – A finite set of productions rules in the following form • A → α where A is a non-terminal and α is a string of terminals and non-terminals (including the empty string) – A start symbol (one of the non-terminal symbol) • Example: E → E + E | E – E | E * E | E / E | - E E → ( E ) E → id
  • 5. 5 Derivations E ⇒ E+E • E+E derives from E – we can replace E by E+E – to able to do this, we have to have a production rule E→E+E in our grammar. E ⇒ E+E ⇒ id+E ⇒ id+id • A sequence of replacements of non-terminal symbols is called a derivation of id+id from E. • In general a derivation step is αAβ ⇒ αγβ if there is a production rule A→γ in our grammar where α and β are arbitrary strings of terminal and non-terminal symbols α1⇒ α2⇒ ... ⇒ αn (αn derives from α1 or α1 derives αn) ⇒ : derives in one step ⇒ : derives in zero or more steps ⇒ : derives in one or more steps * +
  • 6. 6 CFG - Terminology • L(G) is the language of G (the language generated by G) which is a set of sentences. • A sentence of L(G) is a string of terminal symbols of G. • If S is the start symbol of G then ω is a sentence of L(G) iff S ⇒ ω where ω is a string of terminals of G. • If G is a context-free grammar, L(G) is a context-free language. • Two grammars are equivalent if they produce the same language. • S ⇒ α - If α contains non-terminals, it is called as a sentential form of G. - If α does not contain non-terminals, it is called as a sentence of G. + *
  • 7. 7 Derivation Example E ⇒ -E ⇒ -(E) ⇒ -(E+E) ⇒ -(id+E) ⇒ -(id+id) OR E ⇒ -E ⇒ -(E) ⇒ -(E+E) ⇒ -(E+id) ⇒ -(id+id) • At each derivation step, we can choose any of the non-terminal in the sentential form of G for the replacement. • If we always choose the left-most non-terminal in each derivation step, this derivation is called as left-most derivation. • If we always choose the right-most non-terminal in each derivation step, this derivation is called as right-most derivation.
  • 8. 8 Left-Most and Right-Most Derivations Left-Most Derivation E ⇒ -E ⇒ -(E) ⇒ -(E+E) ⇒ -(id+E) ⇒ -(id+id) Right-Most Derivation E ⇒ -E ⇒ -(E) ⇒ -(E+E) ⇒ -(E+id) ⇒ -(id+id) • We will see that the top-down parsers try to find the left-most derivation of the given source program. • We will see that the bottom-up parsers try to find the right-most derivation of the given source program in the reverse order. lmlmlmlmlm rmrmrmrmrm
  • 9. 9 Parse Tree • Inner nodes of a parse tree are non-terminal symbols. • The leaves of a parse tree are terminal symbols. • A parse tree can be seen as a graphical representation of a derivation. E ⇒ -E E E- E E EE E + - ( ) E E E- ( ) E E id E E E + - ( ) id E E E EE + - ( ) id ⇒ -(E) ⇒ -(E+E) ⇒ -(id+E) ⇒ -(id+id)
  • 10. 10 Ambiguity • A grammar produces more than one parse tree for a sentence is called as an ambiguous grammar. E ⇒ E+E ⇒ id+E ⇒ id+E*E ⇒ id+id*E ⇒ id+id*id E ⇒ E*E ⇒ E+E*E ⇒ id+E*E ⇒ id+id*E ⇒ id+id*id E id E + id id E E * E E E + id E E * E id id
  • 11. 11 Ambiguity (cont.) • For the most parsers, the grammar must be unambiguous. • unambiguous grammar  unique selection of the parse tree for a sentence • We should eliminate the ambiguity in the grammar during the design phase of the compiler. • An unambiguous grammar should be written to eliminate the ambiguity. • We have to prefer one of the parse trees of a sentence (generated by an ambiguous grammar) to disambiguate that grammar to restrict to this choice.
  • 12. 12 Ambiguity (cont.) stmt → if expr then stmt | if expr then stmt else stmt | otherstmts if E1 then if E2 then S1 else S2 stmt if expr then stmt else stmt E1 if expr then stmt S2 E2 S1 stmt if expr then stmt E1 if expr then stmt else stmt E2 S1 S2 1 2
  • 13. 13 Ambiguity (cont.) • We prefer the second parse tree (else matches with closest if). • So, we have to disambiguate our grammar to reflect this choice. • The unambiguous grammar will be: stmt → matchedstmt | unmatchedstmt matchedstmt → if expr then matchedstmt else matchedstmt | otherstmts unmatchedstmt → if expr then stmt | if expr then matchedstmt else unmatchedstmt
  • 14. 14 Ambiguity – Operator Precedence • Ambiguous grammars (because of ambiguous operators) can be disambiguated according to the precedence and associativity rules. E → E+E | E*E | E^E | id | (E) disambiguate the grammar precedence: ^ (right to left) * (left to right) + (left to right) E → E+T | T T → T*F | F F → G^F | G G → id | (E) ⇓
  • 15. 15 Left Recursion • A grammar is left recursive if it has a non-terminal A such that there is a derivation. A ⇒ Aα for some string α • Top-down parsing techniques cannot handle left-recursive grammars. • So, we have to convert our left-recursive grammar into an equivalent grammar which is not left-recursive. • The left-recursion may appear in a single step of the derivation (immediate left-recursion), or may appear in more than one step of the derivation. +
  • 16. 16 Immediate Left-Recursion A → A α | β where β does not start with A ⇓ eliminate immediate left recursion A → β A’ A’ → α A’ | ε an equivalent grammar A → A α1 | ... | A αm | β1 | ... | βn where β1 ... βn do not start with A ⇓ eliminate immediate left recursion A → β1 A’ | ... | βn A’ A’ → α1 A’ | ... | αm A’ | ε an equivalent grammar In general,
  • 17. 17 Immediate Left-Recursion -- Example E → E+T | T T → T*F | F F → id | (E) E → T E’ E’ → +T E’ | ε T → F T’ T’ → *F T’ | ε F → id | (E) ⇓ eliminate immediate left recursion
  • 18. 18 Left-Recursion -- Problem • A grammar cannot be immediately left-recursive, but it still can be left-recursive. • By just eliminating the immediate left-recursion, we may not get a grammar which is not left-recursive. S → Aa | b A → Sc | d This grammar is not immediately left-recursive, but it is still left-recursive. S ⇒ Aa ⇒ Sca or A ⇒ Sc ⇒ Aac causes to a left-recursion • So, we have to eliminate all left-recursions from our grammar
  • 19. 19 Eliminate Left-Recursion -- Example S → Aa | b A → Ac | Sd | f - Order of non-terminals: S, A for S: - we do not enter the inner loop. - there is no immediate left recursion in S. for A: - Replace A → Sd with A → Aad | bd So, we will have A → Ac | Aad | bd | f - Eliminate the immediate left-recursion in A A → bdA’ | fA’ A’ → cA’ | adA’ | ε So, the resulting equivalent grammar which is not left-recursive is: S → Aa | b A → bdA’ | fA’ A’ → cA’ | adA’ | ε
  • 20. 20 Eliminate Left-Recursion – Example2 S → Aa | b A → Ac | Sd | f - Order of non-terminals: A, S for A: - we do not enter the inner loop. - Eliminate the immediate left-recursion in A A → SdA’ | fA’ A’ → cA’ | ε for S: - Replace S → Aa with S → SdA’ a | fA’ a So, we will have S → SdA’ a | fA’ a | b - Eliminate the immediate left-recursion in S S → fA’aS’ | bS’ S’ → dA’ aS’ | ε So, the resulting equivalent grammar which is not left-recursive is: S → fA’aS’ | bS’ S’ → dA’ aS’ | ε A → SdA’ | fA’ A’ → cA’ | ε