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

COMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demeritsCOMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demeritsCherry
 
module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learninglevieagacer
 
GBSN - Biochemistry (Unit 3) Metabolism
GBSN - Biochemistry (Unit 3) MetabolismGBSN - Biochemistry (Unit 3) Metabolism
GBSN - Biochemistry (Unit 3) MetabolismAreesha Ahmad
 
Terpineol and it's characterization pptx
Terpineol and it's characterization pptxTerpineol and it's characterization pptx
Terpineol and it's characterization pptxMuhammadRazzaq31
 
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
 
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body 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.Cherry
 
Plasmid: types, structure and functions.
Plasmid: types, structure and functions.Plasmid: types, structure and functions.
Plasmid: types, structure and functions.Cherry
 
Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cyathodium bryophyte: morphology, anatomy, reproduction etc.Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cyathodium bryophyte: morphology, anatomy, reproduction etc.Cherry
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusNazaninKarimi6
 
Human genetics..........................pptx
Human genetics..........................pptxHuman genetics..........................pptx
Human genetics..........................pptxCherry
 
Taphonomy and Quality of the Fossil Record
Taphonomy and Quality of the  Fossil RecordTaphonomy and Quality of the  Fossil Record
Taphonomy and Quality of the Fossil RecordSangram Sahoo
 
Cot curve, melting temperature, unique and repetitive DNA
Cot curve, melting temperature, unique and repetitive DNACot curve, melting temperature, unique and repetitive DNA
Cot curve, melting temperature, unique and repetitive DNACherry
 
Site specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdfSite specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdfCherry
 
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptx
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptxClimate Change Impacts on Terrestrial and Aquatic Ecosystems.pptx
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptxDiariAli
 
The Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxThe Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxseri bangash
 
X-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
X-rays from a Central “Exhaust Vent” of the Galactic Center ChimneyX-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
X-rays from a Central “Exhaust Vent” of the Galactic Center ChimneySérgio Sacani
 
Concept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdfConcept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdfCherry
 
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.Cherry
 
ONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteRaunakRastogi4
 

Recently uploaded (20)

COMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demeritsCOMPOSTING : types of compost, merits and demerits
COMPOSTING : types of compost, merits and demerits
 
module for grade 9 for distance learning
module for grade 9 for distance learningmodule for grade 9 for distance learning
module for grade 9 for distance learning
 
GBSN - Biochemistry (Unit 3) Metabolism
GBSN - Biochemistry (Unit 3) MetabolismGBSN - Biochemistry (Unit 3) Metabolism
GBSN - Biochemistry (Unit 3) Metabolism
 
Terpineol and it's characterization pptx
Terpineol and it's characterization pptxTerpineol and it's characterization pptx
Terpineol and it's characterization 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
 
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body GBSN - Microbiology (Unit 3)Defense Mechanism of the body
GBSN - Microbiology (Unit 3)Defense Mechanism of the body
 
Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.Porella : features, morphology, anatomy, reproduction etc.
Porella : features, morphology, anatomy, reproduction etc.
 
Plasmid: types, structure and functions.
Plasmid: types, structure and functions.Plasmid: types, structure and functions.
Plasmid: types, structure and functions.
 
Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cyathodium bryophyte: morphology, anatomy, reproduction etc.Cyathodium bryophyte: morphology, anatomy, reproduction etc.
Cyathodium bryophyte: morphology, anatomy, reproduction etc.
 
development of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virusdevelopment of diagnostic enzyme assay to detect leuser virus
development of diagnostic enzyme assay to detect leuser virus
 
Human genetics..........................pptx
Human genetics..........................pptxHuman genetics..........................pptx
Human genetics..........................pptx
 
Taphonomy and Quality of the Fossil Record
Taphonomy and Quality of the  Fossil RecordTaphonomy and Quality of the  Fossil Record
Taphonomy and Quality of the Fossil Record
 
Cot curve, melting temperature, unique and repetitive DNA
Cot curve, melting temperature, unique and repetitive DNACot curve, melting temperature, unique and repetitive DNA
Cot curve, melting temperature, unique and repetitive DNA
 
Site specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdfSite specific recombination and transposition.........pdf
Site specific recombination and transposition.........pdf
 
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptx
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptxClimate Change Impacts on Terrestrial and Aquatic Ecosystems.pptx
Climate Change Impacts on Terrestrial and Aquatic Ecosystems.pptx
 
The Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptxThe Mariana Trench remarkable geological features on Earth.pptx
The Mariana Trench remarkable geological features on Earth.pptx
 
X-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
X-rays from a Central “Exhaust Vent” of the Galactic Center ChimneyX-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
X-rays from a Central “Exhaust Vent” of the Galactic Center Chimney
 
Concept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdfConcept of gene and Complementation test.pdf
Concept of gene and Complementation test.pdf
 
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.
Genome Projects : Human, Rice,Wheat,E coli and Arabidopsis.
 
ONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for voteONLINE VOTING SYSTEM SE Project for vote
ONLINE VOTING SYSTEM SE Project for vote
 

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.