Ask Your Question
2

What is the process for eliminating left recursion in a grammar that contains both left and right recursion?

asked 2022-12-22 11:00:00 +0000

woof gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2021-05-29 19:00:00 +0000

david gravatar image

The process for eliminating left recursion in a grammar that contains both left and right recursion can be done in the following steps:

Step 1: Identify the left recursive productions i.e. the productions that start with the same non-terminal symbol.

Step 2: Replace the left recursive productions with new non-terminals and new productions that do not have left recursion.

Step 3: Reorder the new productions so that the non-left recursive productions come before the left-recursive productions.

Step 4: Eliminate any remaining left recursion using the same process as step 2.

Step 5: Remove any unit productions that may have been created during the process.

Example:

Consider the following grammar:

A → Aa | bA | cB B → Bc | dB | ε

This grammar has both left and right recursion.

Step 1: The left recursive productions are A → Aa and B → Bc.

Step 2: Replace the left recursive productions with new non-terminals.

A → bA' | cB A' → aA' | ε B → dB | ε

Step 3: Reorder the productions so that the non-left recursive productions come before the left-recursive productions.

A → cB | bA' A' → aA' | ε B → dB | ε

Step 4: Eliminate any remaining left recursion using the same process as step 2.

A → cB | bA' A' → aA' | ε B → dBB' | ε B' → cB' | ε

Step 5: Remove any unit productions that may have been created during the process.

A → cB | bA' A' → aA' | ε B → dBB' | ε B' → cB' | ε

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2022-12-22 11:00:00 +0000

Seen: 10 times

Last updated: May 29 '21