I am a beginner started learning theoretical computer science. I just came through context-free grammars.
So my question is: what is the different between left-most and right-most derivation?
Because both of them gave me the same parse tree.
Computer Science Stack Exchange is a question and answer site for students, researchers and practitioners of computer science. It only takes a minute to sign up.
Sign up to join this communityI am a beginner started learning theoretical computer science. I just came through context-free grammars.
So my question is: what is the different between left-most and right-most derivation?
Because both of them gave me the same parse tree.
Given a derivation tree for a word, you can "implement" it as a sequence of productions in many different ways. The leftmost derivation is the one in which you always expand the leftmost non-terminal. The rightmost derivation is the one in which you always expand the rightmost non-terminal.
For example, here are two parse trees borrowed from Wikipedia:
The leftmost derivation corresponding to the left parse tree is $$ A \to A + A \to a + A \to a + A - A \to a + a - A \to a + a - a $$ The rightmost derivation corresponding to the left parse tree is $$ A \to A + A \to A + A - A \to A + A - a \to A + a - a \to a + a - a $$ The leftmost derivation corresponding to the right parse tree is $$ A \to A - A \to A + A - A \to a + A - A \to a + a - A \to a + a - a $$ The rightmost derivation corresponding to the right parse tree is $$ A \to A - A \to A - a \to A + A - a \to A + a - a \to a + a - a $$