SlideShare a Scribd company logo
1 of 16
WRITING
GRAMMAR
Grammars
■ capable of describing most syntax of
programming languages
"Why use regular expressions to
define the lexical syntax of a
language?"
■ Separating the syntactic structure of a language into lexical and non lexical parts
provides a convenient way of modularizing the front end of a compiler into two
manageable-sized components.
■ The lexical rules of a language are frequently quite simple, and to describe them
we do not need a notation as powerful as grammars
■ Regular expressions generally provide a more concise and easier-to-understand
notation for tokens than grammars.
■ More efficient lexical analyzers can be constructed automatically from regular
expressions than from arbitrary grammars.
ELIMINATING
AMBIGUITY
A grammar that produces more than one parse tree for some
sentence is said to be ambiguous.
Example:
E → E or E |
E and E |
not E |
True |
False
For the string : True and False or True
E E
/ |  / | 
E and E E or E
| / |  / |  |
True E or E E and E
True
| | | |
False True True False
It has more than one parse trees. Therefore the
grammar is ambiguous.
Ambiguity Eliminated:
E → E or F | F
F→ F and G | G
G → Not G | True | False
E
/ | 
E or F
/ |  |
F and G G
| | |
G False True
|
True
ELIMINATION OF
LEFT RECURSION
A grammar is left recursive if it takes the
form A → Aα | β
Two Cases
■ Immediate Left Recursion - A → A α left hand side symbol is the same as first
right hand side symbol.
■ Indirect Left Recursion - A → B α → . . . → A β A extends via intermediate steps
into another derivation part that starts with A.
Example #1:
■ Determine which is left
recursive. Note: A grammar is
left recursive if the variable
appears as prefix on one or
more of its productions. (A →
Aα | β)
■ Change the LR production into
A→ βA’
A’→ αA’|ε
■ Continue until there are no LR
Productions left.
E → E or F | F
F→ F and G | G
G → Not G | True | False
E → FE’
E’ → orFE’ | ε
F→ F and G | G
G → Not G | True | False
E → FE’
E’ → or FE’ | ε
F→ GF’
F’ → and GF’ | ε
G → Not G | True | False
Algorithm
■ Arrange the nonterminals in some order Ai,Az,... ,An.
for ( each i from 1 to n ) {
for ( each j from 1 to i — 1 ) {
replace each production of the form
Ai→Ajγ by the productions
Ai→δ1γ| δ2γ|…| δkγ,
where Aj→ δ1| δ2|…| δk are all Aj – productions
}
eliminate the immediate left recursion among Ai-productions
}
Input → grammar G with no cycles or ε-productions
Output → an equivalent grammar with no left recursion
After the first
iteration, this block
will replace all found
Ai → Ajy productions
where i>j which
eliminates indirect
LR
Example #2
■ We first put the non terminals in order.
S1 → A2 B4
A2 → C3 B4 | b
C3 → S1 a
B4 → b
■ I = 1, J = 1: No left recursions found.
■ I = 2, J = 1: There is no A2 → S1 α.
■ I = 3, J = 1:
C3 → S1 α production found: C3 → S1 a
Replace S in the rhs of C with all S productions which gives
us:
C3 → A2 B4 a
for ( each i from 1 to n ) {
for ( each j from 1 to i — 1 ) {
replace each production of the form
Ai→Ajγ by the productions
Ai→δ1γ| δ2γ|…| δkγ,
where Aj→ δ1| δ2|…| δk are all Aj
productions
}
eliminate the immediate left recursion
among Ai-productions
}
S→ AB
A→ CB | b
C → Sa
B → b
derivation: S ⇒ A B ⇒ C B B ⇒ S a B B ⇒ A B a B B ⇒ C
B B a B B ⇒ S a B B a B B ⇒ A B a B B a B B ⇒ . . . ⇒
bb(abb)*
Cont. Example #2
■ I = 3, J = 2: C → A α rule found.
Apply same step.
C3 → C3 B4 B4 a | b B4 a
We get out of the inner loop where all immediate LRs are
discovered.
C is left recursive so eliminate.
New derived grammar:
C3 → b B4 a C3’
C3’ → B4 B4 a C3’ | ε
Current grammar:
S1 → A2 B4
A2 → C3 B4 | b
C3 → A2 B4 a
B4 → b
for ( each i from 1 to n ) {
for ( each j from 1 to i — 1 ) {
replace each production of the form
Ai→Ajγ by the productions
Ai→δ1γ| δ2γ|…| δkγ,
where Aj→ δ1| δ2|…| δk are all Aj productions
}
eliminate the immediate left recursion among
Ai-productions
}
Final Grammar:
S → A B
A → C B | b
C → bBaC’
C’ → BBaC’ | ε
LEFT FACTORING
"factoring out" prefixes which are common to two or more
productions
Method & Example:
■ For each non-terminal A find the longest prefix α to two or more alternatives
statement → identifier := exp | identifier ( exp-list ) | other
■ Replace A-productions A→ αβ1 | αβ2 |…| αβn | γ by
A→αA’ | γ
A’→ β1 | β2 |…| βn
■ Repeat if necessary.
final left factored grammar:
statement → identifier statement’ | other
statement’ → := exp | (exp-list)
statement → identifier := exp | identifier ( exp-list ) | other
Input ◦ grammar G
Output ◦ equivalent left-factored grammar
Example #2
■ Left factor S:
S → TS’
S ‘ → +S | ε
■ Left factor T:
T → UT’
T’ → * T | ε
S → T + S | T
T → U * T | U
U → (S) | V
V → 0 | 1 | ... | 9
Final Left Factored Grammar:
S → TS’
S ‘ → +S | ε
T → UT’
T’ → * T | ε
U → (S) | V
V → 0 | 1 | ... | 9
NON CONTEXT FREE
LANGUAGE
CONSTRUCTS
Declaration before Use
L1 = {wcw|w is {a,b}+}
where the first w is declaration and the second represents its use.
■ When a statement for use of variable is generated, it requires context or
knowledge of whether the variable used was defined before. If this Declaration
rule is satisfied, it is only then that the statement will be valid to the program.
This makes the language context sensitive.
Parameter No. Matching
L2 = {an bm cn dm| n,m >=1}
Here a and b could represent the formal-parameter lists of two functions declared while c and
d represent the actual-parameter lists in calls to these two functions.
■ The requirement to match the number of arguments of the calls to the
declarations for a generated language to be valid makes it non context free.

More Related Content

What's hot

Pre-Cal 40S Slides April 10, 2008
Pre-Cal 40S Slides April 10, 2008Pre-Cal 40S Slides April 10, 2008
Pre-Cal 40S Slides April 10, 2008Darren Kuropatwa
 
7.3 daqy 2
7.3 daqy 27.3 daqy 2
7.3 daqy 2leblance
 
Chapter 2 Review
Chapter 2 ReviewChapter 2 Review
Chapter 2 Reviewguest056fe2
 
GUASS’S ELIMINATION METHOD AND EXAMPLE
GUASS’S ELIMINATION METHOD AND EXAMPLEGUASS’S ELIMINATION METHOD AND EXAMPLE
GUASS’S ELIMINATION METHOD AND EXAMPLEBangulkhanbaloch
 
Python operators part2
Python operators part2Python operators part2
Python operators part2Vishal Dutt
 
February 10 2016
February 10 2016February 10 2016
February 10 2016khyps13
 
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingamArithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingamSaraswathiRamalingam
 
Properties of a Triangular Matrix
Properties of a Triangular MatrixProperties of a Triangular Matrix
Properties of a Triangular MatrixChristopher Gratton
 
Trace of Matrix - Linear Algebra
Trace of Matrix - Linear AlgebraTrace of Matrix - Linear Algebra
Trace of Matrix - Linear AlgebraSiddhantDixit6
 
Pre-Cal 40S Slides November 5, 2007
Pre-Cal 40S Slides November 5, 2007Pre-Cal 40S Slides November 5, 2007
Pre-Cal 40S Slides November 5, 2007Darren Kuropatwa
 
January 17, 2014
January 17, 2014January 17, 2014
January 17, 2014khyps13
 
Chapter 01 – Section 01
Chapter 01 – Section 01Chapter 01 – Section 01
Chapter 01 – Section 01WCalhoun
 
non-predective parser
non-predective parsernon-predective parser
non-predective parserTarjMehta1
 

What's hot (15)

Pre-Cal 40S Slides April 10, 2008
Pre-Cal 40S Slides April 10, 2008Pre-Cal 40S Slides April 10, 2008
Pre-Cal 40S Slides April 10, 2008
 
7.3 daqy 2
7.3 daqy 27.3 daqy 2
7.3 daqy 2
 
201506 CSE340 Lecture 16
201506 CSE340 Lecture 16201506 CSE340 Lecture 16
201506 CSE340 Lecture 16
 
Chapter 2 Review
Chapter 2 ReviewChapter 2 Review
Chapter 2 Review
 
GUASS’S ELIMINATION METHOD AND EXAMPLE
GUASS’S ELIMINATION METHOD AND EXAMPLEGUASS’S ELIMINATION METHOD AND EXAMPLE
GUASS’S ELIMINATION METHOD AND EXAMPLE
 
Python operators part2
Python operators part2Python operators part2
Python operators part2
 
February 10 2016
February 10 2016February 10 2016
February 10 2016
 
Matrices
MatricesMatrices
Matrices
 
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingamArithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
Arithmetic expression INFIX TO POSTFIX CONVERTION saraswathi ramalingam
 
Properties of a Triangular Matrix
Properties of a Triangular MatrixProperties of a Triangular Matrix
Properties of a Triangular Matrix
 
Trace of Matrix - Linear Algebra
Trace of Matrix - Linear AlgebraTrace of Matrix - Linear Algebra
Trace of Matrix - Linear Algebra
 
Pre-Cal 40S Slides November 5, 2007
Pre-Cal 40S Slides November 5, 2007Pre-Cal 40S Slides November 5, 2007
Pre-Cal 40S Slides November 5, 2007
 
January 17, 2014
January 17, 2014January 17, 2014
January 17, 2014
 
Chapter 01 – Section 01
Chapter 01 – Section 01Chapter 01 – Section 01
Chapter 01 – Section 01
 
non-predective parser
non-predective parsernon-predective parser
non-predective parser
 

Similar to Natural Language Processing - Writing Grammar

Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
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
 
Theory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptxTheory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptxJisock
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationNikhil Pandit
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammarlavishka_anuj
 
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
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
Theory of Computation FSM Grammar Minimisation and Normal Forms
Theory of Computation FSM Grammar Minimisation and Normal FormsTheory of Computation FSM Grammar Minimisation and Normal Forms
Theory of Computation FSM Grammar Minimisation and Normal FormsRushabh2428
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
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
 
Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Srimatre K
 

Similar to Natural Language Processing - Writing Grammar (20)

Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
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
 
Theory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptxTheory of competition topic simplification of cfg, normal form of FG.pptx
Theory of competition topic simplification of cfg, normal form of FG.pptx
 
Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Simplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of ComputationSimplifies and normal forms - Theory of Computation
Simplifies and normal forms - Theory of Computation
 
Simplifiaction of grammar
Simplifiaction of grammarSimplifiaction of grammar
Simplifiaction of grammar
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
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
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
Theory of Computation FSM Grammar Minimisation and Normal Forms
Theory of Computation FSM Grammar Minimisation and Normal FormsTheory of Computation FSM Grammar Minimisation and Normal Forms
Theory of Computation FSM Grammar Minimisation and Normal Forms
 
Ch06
Ch06Ch06
Ch06
 
Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Topdown parsing
Topdown parsingTopdown parsing
Topdown parsing
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
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
 
CFG to CNF
CFG to CNFCFG to CNF
CFG to CNF
 
Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4Formal Languages and Automata Theory unit 4
Formal Languages and Automata Theory unit 4
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
 

Recently uploaded

biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY1301aanya
 
LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.Silpa
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxMohamedFarag457087
 
Genome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptxGenome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptxSilpa
 
Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspectsmuralinath2
 
Zoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdfZoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdfSumit Kumar yadav
 
Genetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsGenetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsbassianu17
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxSilpa
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.Silpa
 
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry Areesha Ahmad
 
Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.Silpa
 
Phenolics: types, biosynthesis and functions.
Phenolics: types, biosynthesis and functions.Phenolics: types, biosynthesis and functions.
Phenolics: types, biosynthesis and functions.Silpa
 
300003-World Science Day For Peace And Development.pptx
300003-World Science Day For Peace And Development.pptx300003-World Science Day For Peace And Development.pptx
300003-World Science Day For Peace And Development.pptxryanrooker
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxSilpa
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learninglevieagacer
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceAlex Henderson
 
(May 9, 2024) Enhanced Ultrafast Vector Flow Imaging (VFI) Using Multi-Angle ...
(May 9, 2024) Enhanced Ultrafast Vector Flow Imaging (VFI) Using Multi-Angle ...(May 9, 2024) Enhanced Ultrafast Vector Flow Imaging (VFI) Using Multi-Angle ...
(May 9, 2024) Enhanced Ultrafast Vector Flow Imaging (VFI) Using Multi-Angle ...Scintica Instrumentation
 
Molecular markers- RFLP, RAPD, AFLP, SNP etc.
Molecular markers- RFLP, RAPD, AFLP, SNP etc.Molecular markers- RFLP, RAPD, AFLP, SNP etc.
Molecular markers- RFLP, RAPD, AFLP, SNP etc.Silpa
 

Recently uploaded (20)

biology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGYbiology HL practice questions IB BIOLOGY
biology HL practice questions IB BIOLOGY
 
LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.LUNULARIA -features, morphology, anatomy ,reproduction etc.
LUNULARIA -features, morphology, anatomy ,reproduction etc.
 
Digital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptxDigital Dentistry.Digital Dentistryvv.pptx
Digital Dentistry.Digital Dentistryvv.pptx
 
Genome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptxGenome sequencing,shotgun sequencing.pptx
Genome sequencing,shotgun sequencing.pptx
 
Dr. E. Muralinath_ Blood indices_clinical aspects
Dr. E. Muralinath_ Blood indices_clinical  aspectsDr. E. Muralinath_ Blood indices_clinical  aspects
Dr. E. Muralinath_ Blood indices_clinical aspects
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Zoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdfZoology 5th semester notes( Sumit_yadav).pdf
Zoology 5th semester notes( Sumit_yadav).pdf
 
Genetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditionsGenetics and epigenetics of ADHD and comorbid conditions
Genetics and epigenetics of ADHD and comorbid conditions
 
CYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptxCYTOGENETIC MAP................ ppt.pptx
CYTOGENETIC MAP................ ppt.pptx
 
POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.POGONATUM : morphology, anatomy, reproduction etc.
POGONATUM : morphology, anatomy, reproduction etc.
 
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
GBSN - Biochemistry (Unit 2) Basic concept of organic chemistry
 
Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.
 
Phenolics: types, biosynthesis and functions.
Phenolics: types, biosynthesis and functions.Phenolics: types, biosynthesis and functions.
Phenolics: types, biosynthesis and functions.
 
300003-World Science Day For Peace And Development.pptx
300003-World Science Day For Peace And Development.pptx300003-World Science Day For Peace And Development.pptx
300003-World Science Day For Peace And Development.pptx
 
Cyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptxCyanide resistant respiration pathway.pptx
Cyanide resistant respiration pathway.pptx
 
Module for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learningModule for Grade 9 for Asynchronous/Distance learning
Module for Grade 9 for Asynchronous/Distance learning
 
Clean In Place(CIP).pptx .
Clean In Place(CIP).pptx                 .Clean In Place(CIP).pptx                 .
Clean In Place(CIP).pptx .
 
FAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical ScienceFAIRSpectra - Enabling the FAIRification of Analytical Science
FAIRSpectra - Enabling the FAIRification of Analytical Science
 
(May 9, 2024) Enhanced Ultrafast Vector Flow Imaging (VFI) Using Multi-Angle ...
(May 9, 2024) Enhanced Ultrafast Vector Flow Imaging (VFI) Using Multi-Angle ...(May 9, 2024) Enhanced Ultrafast Vector Flow Imaging (VFI) Using Multi-Angle ...
(May 9, 2024) Enhanced Ultrafast Vector Flow Imaging (VFI) Using Multi-Angle ...
 
Molecular markers- RFLP, RAPD, AFLP, SNP etc.
Molecular markers- RFLP, RAPD, AFLP, SNP etc.Molecular markers- RFLP, RAPD, AFLP, SNP etc.
Molecular markers- RFLP, RAPD, AFLP, SNP etc.
 

Natural Language Processing - Writing Grammar

  • 2. Grammars ■ capable of describing most syntax of programming languages
  • 3. "Why use regular expressions to define the lexical syntax of a language?" ■ Separating the syntactic structure of a language into lexical and non lexical parts provides a convenient way of modularizing the front end of a compiler into two manageable-sized components. ■ The lexical rules of a language are frequently quite simple, and to describe them we do not need a notation as powerful as grammars ■ Regular expressions generally provide a more concise and easier-to-understand notation for tokens than grammars. ■ More efficient lexical analyzers can be constructed automatically from regular expressions than from arbitrary grammars.
  • 4. ELIMINATING AMBIGUITY A grammar that produces more than one parse tree for some sentence is said to be ambiguous.
  • 5. Example: E → E or E | E and E | not E | True | False For the string : True and False or True E E / | / | E and E E or E | / | / | | True E or E E and E True | | | | False True True False It has more than one parse trees. Therefore the grammar is ambiguous. Ambiguity Eliminated: E → E or F | F F→ F and G | G G → Not G | True | False E / | E or F / | | F and G G | | | G False True | True
  • 6. ELIMINATION OF LEFT RECURSION A grammar is left recursive if it takes the form A → Aα | β
  • 7. Two Cases ■ Immediate Left Recursion - A → A α left hand side symbol is the same as first right hand side symbol. ■ Indirect Left Recursion - A → B α → . . . → A β A extends via intermediate steps into another derivation part that starts with A.
  • 8. Example #1: ■ Determine which is left recursive. Note: A grammar is left recursive if the variable appears as prefix on one or more of its productions. (A → Aα | β) ■ Change the LR production into A→ βA’ A’→ αA’|ε ■ Continue until there are no LR Productions left. E → E or F | F F→ F and G | G G → Not G | True | False E → FE’ E’ → orFE’ | ε F→ F and G | G G → Not G | True | False E → FE’ E’ → or FE’ | ε F→ GF’ F’ → and GF’ | ε G → Not G | True | False
  • 9. Algorithm ■ Arrange the nonterminals in some order Ai,Az,... ,An. for ( each i from 1 to n ) { for ( each j from 1 to i — 1 ) { replace each production of the form Ai→Ajγ by the productions Ai→δ1γ| δ2γ|…| δkγ, where Aj→ δ1| δ2|…| δk are all Aj – productions } eliminate the immediate left recursion among Ai-productions } Input → grammar G with no cycles or ε-productions Output → an equivalent grammar with no left recursion After the first iteration, this block will replace all found Ai → Ajy productions where i>j which eliminates indirect LR
  • 10. Example #2 ■ We first put the non terminals in order. S1 → A2 B4 A2 → C3 B4 | b C3 → S1 a B4 → b ■ I = 1, J = 1: No left recursions found. ■ I = 2, J = 1: There is no A2 → S1 α. ■ I = 3, J = 1: C3 → S1 α production found: C3 → S1 a Replace S in the rhs of C with all S productions which gives us: C3 → A2 B4 a for ( each i from 1 to n ) { for ( each j from 1 to i — 1 ) { replace each production of the form Ai→Ajγ by the productions Ai→δ1γ| δ2γ|…| δkγ, where Aj→ δ1| δ2|…| δk are all Aj productions } eliminate the immediate left recursion among Ai-productions } S→ AB A→ CB | b C → Sa B → b derivation: S ⇒ A B ⇒ C B B ⇒ S a B B ⇒ A B a B B ⇒ C B B a B B ⇒ S a B B a B B ⇒ A B a B B a B B ⇒ . . . ⇒ bb(abb)*
  • 11. Cont. Example #2 ■ I = 3, J = 2: C → A α rule found. Apply same step. C3 → C3 B4 B4 a | b B4 a We get out of the inner loop where all immediate LRs are discovered. C is left recursive so eliminate. New derived grammar: C3 → b B4 a C3’ C3’ → B4 B4 a C3’ | ε Current grammar: S1 → A2 B4 A2 → C3 B4 | b C3 → A2 B4 a B4 → b for ( each i from 1 to n ) { for ( each j from 1 to i — 1 ) { replace each production of the form Ai→Ajγ by the productions Ai→δ1γ| δ2γ|…| δkγ, where Aj→ δ1| δ2|…| δk are all Aj productions } eliminate the immediate left recursion among Ai-productions } Final Grammar: S → A B A → C B | b C → bBaC’ C’ → BBaC’ | ε
  • 12. LEFT FACTORING "factoring out" prefixes which are common to two or more productions
  • 13. Method & Example: ■ For each non-terminal A find the longest prefix α to two or more alternatives statement → identifier := exp | identifier ( exp-list ) | other ■ Replace A-productions A→ αβ1 | αβ2 |…| αβn | γ by A→αA’ | γ A’→ β1 | β2 |…| βn ■ Repeat if necessary. final left factored grammar: statement → identifier statement’ | other statement’ → := exp | (exp-list) statement → identifier := exp | identifier ( exp-list ) | other Input ◦ grammar G Output ◦ equivalent left-factored grammar
  • 14. Example #2 ■ Left factor S: S → TS’ S ‘ → +S | ε ■ Left factor T: T → UT’ T’ → * T | ε S → T + S | T T → U * T | U U → (S) | V V → 0 | 1 | ... | 9 Final Left Factored Grammar: S → TS’ S ‘ → +S | ε T → UT’ T’ → * T | ε U → (S) | V V → 0 | 1 | ... | 9
  • 16. Declaration before Use L1 = {wcw|w is {a,b}+} where the first w is declaration and the second represents its use. ■ When a statement for use of variable is generated, it requires context or knowledge of whether the variable used was defined before. If this Declaration rule is satisfied, it is only then that the statement will be valid to the program. This makes the language context sensitive. Parameter No. Matching L2 = {an bm cn dm| n,m >=1} Here a and b could represent the formal-parameter lists of two functions declared while c and d represent the actual-parameter lists in calls to these two functions. ■ The requirement to match the number of arguments of the calls to the declarations for a generated language to be valid makes it non context free.