This content originally appeared on DEV Community and was authored by Mujahida Joynab
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.
This content originally appeared on DEV Community and was authored by Mujahida Joynab
Mujahida Joynab | Sciencx (2025-11-03T19:30:35+00:00) Top-Down and Bottom-Up Parsing. Retrieved from https://www.scien.cx/2025/11/03/top-down-and-bottom-up-parsing/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.