Top-Down Parsing (Leftmost Derivation)
- The process starts with the starting symbol $$ S $$ and expands the leftmost non-terminal at each step.
 - Given:
- $$ S \rightarrow AB $$
 - $$ A \rightarrow aA \mid \epsilon $$
 - $$ B \rightarrow b \mid \epsilon $$
 
 - Target: $$ aaab $$
 
Step-by-step derivation:
- $$ S \rightarrow AB $$
 - $$ AB \rightarrow aAB $$
 - $$ aAB \rightarrow aaAB $$
 - $$ aaAB \rightarrow aaaAB $$
 - $$ aaaAB \rightarrow aaa\epsilon B $$
 - $$ aaa\epsilon B \rightarrow aaab $$
 
This is a top-down parsing approach because it expands from the start symbol $$ S $$ and works downward, always expanding the leftmost non-terminal.
Bottom-Up Parsing (Rightmost Derivation in Reverse)
- The process starts with the string of terminals $$ aaab $$ and works backward, reducing strings to non-terminals until you reach $$ S $$.
 - Working backward:
- $$ aaab $$
 - $$ aaaB $$ (replace $$ b $$ with $$ B $$)
 - $$ aaaAB $$ (replace $$ \epsilon $$ with $$ A $$)
 - $$ aaAB $$ (replace $$ aA $$ with $$ A $$)
 - $$ aAB $$ (replace $$ aA $$ with $$ A $$)
 - $$ AB $$ (replace $$ aA $$ with $$ A $$)
 
 
This is a bottom-up parsing approach because it starts from the terminal string and works upward, reducing substrings to non-terminals.
Summary
- Top-down parsing starts from the root ($$ S $$) and expands leftmost non-terminals.
 - Bottom-up parsing starts from the target string and works backward, reducing substrings to non-terminals until reaching the root.
 
    
Top comments (0)