0% found this document useful (0 votes)
17 views9 pages

Basic Parsing Techniques

BASIC PARSING TECHNIQUES

Uploaded by

Priyanka Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views9 pages

Basic Parsing Techniques

BASIC PARSING TECHNIQUES

Uploaded by

Priyanka Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BASIC PARSING TECHNIQUES

Parsing in Natural Language Processing (NLP) refers to the process of analyzing a sentence or a
string of words to determine its grammatical structure with respect to a given formal grammar.
This involves identifying the parts of speech of the words, their syntactic relationships, and how
they combine to form phrases and sentences. Parsing helps in understanding the syntactic and
semantic meaning of the text.

1. Top-Down Parsing

Definition: Top-down parsing starts with the start symbol and tries to derive the input sentence by
applying production rules. It works from the highest level of the grammar (the start symbol) down
to the terminals.

Example:

Consider the following simple context-free grammar (CFG):

1. S→NP VP

2. NP→Det N

3. VP→V NP

4. Det→the

5. N→cat

6. V→sees

7. N→dog

Sentence to Parse:

"The cat sees the dog"


Top-Down Parsing Steps:

1. Start with the Start Symbol:

o Begin with the start symbol S.

2. Apply Rule for S:

o S→NP VP

3. Expand NP:

o NP→Det N

o Apply to NP in the previous step, resulting in: S→Det N VP

4. Expand VP

o VP→V NP

o Apply to VP in the previous step, resulting in:

S→Det N V NP

5. Match Terminals with Input:

o The input sentence is "The cat sees the dog".

o Det: The first word "The" matches Det→the

The parse tree now looks like:


6. Continue with N:

The next word "cat" matches N→cat.

The parse tree now looks like:

7. Continue with V:

The next word "sees" matches 𝑉→sees

The parse tree now looks like:


8. Expand the Second NP:

The next symbol to expand is the second NP in VP→V NP

9. Match Terminals with Input:

The next word "the" matches 𝐷𝑒𝑡→the

The parse tree now looks like


10. Continue with the Second N:

The final word "dog" matches N→dog

The parse tree is now complete:

Summary:

1. Start with the start symbol S


2. Apply production rules top-down, expanding non-terminals.
3. Match terminals with the input sentence.
4. Continue until the input is fully matched and the parse tree is complete.
2. Bottom-Up Parsing

Definition: Bottom-up parsing starts with the input sentence and works up to the start symbol,
attempting to reduce the sentence to the start symbol by applying production rules in reverse.

Example:

Consider the same simple context-free grammar (CFG):

1. S→NP VP

2. NP→Det N

3. VP→V NP

4. Det→the

5. N→cat

6. V→sees

7. N→dog

Sentence to Parse:

"The cat sees the dog"

Bottom-Up Parsing Steps:

Sentence to Parse:
"The cat sees the dog"
Bottom-Up Parsing Steps:

1. Start with the Input Sentence:


 Begin with the sentence: "The cat sees the dog".

2. Initialize Stack and Input:

 Stack: []
 Input: ["The", "cat", "sees", "the", "dog"]

3. Shift "The" onto the Stack:

 Stack: ["The"]
 Input: ["cat", "sees", "the", "dog"]
4. Reduce "The" to Det:

 Stack: [Det]
 Input: ["cat", "sees", "the", "dog"]
 Using rule Det→the

5. Shift "cat" onto the Stack:

 Stack: [Det, "cat"]


 Input: ["sees", "the", "dog"]

6. Reduce "cat" to N:

 Stack: [Det, N]
 Input: ["sees", "the", "dog"]
 Using rule N→cat

7. Reduce Det N to NP:

 Stack: [NP]
 Input: ["sees", "the", "dog"]
 Using rule NP→Det N
8. Shift "sees" onto the Stack:

 Stack: [NP, "sees"]


 Input: ["the", "dog"]

9. Reduce "sees" to V:

 Stack: [NP, V]
 Input: ["the", "dog"]
 Using rule V→sees

10. Shift "the" onto the Stack:

 Stack: [NP, V, "the"]


 Input: ["dog"]

11. Reduce "the" to Det:

 Stack: [NP, V, Det]


 Input: ["dog"]
 Using rule Det→the

12. Shift "dog" onto the Stack:

 Stack: [NP, V, Det, "dog"]


 Input: []

13. Reduce "dog" to N:

 Stack: [NP, V, Det, N]


 Input: []
 Using rule N→dog

14. Reduce Det N to NP:

 Stack: [NP, V, NP]


 Input: []
 Using rule NP→Det N
15. Reduce V NP to VP:

 Stack: [NP, VP]


 Input: []
 Using rule VP→V NP

16. Reduce NP VP to S:

 Stack: [S]
 Input: []
 Using rule S→NP VP

Final Parse Tree:

Summary:

1. Start with the input sentence and an empty stack.


2. Shift input symbols onto the stack until a valid reduction is possible.
3. Apply reductions based on the grammar rules.
4. Continue shifting and reducing until the stack contains only the start symbol SSS and the
input is fully consumed.

You might also like