1
$\begingroup$

Consider this example:

stmt => if (expr) stmt | if (expr) stmt else stmt | other

Why can't we use shift-reduce on this?

This is a compiler homework and I really need to answer it.

$\endgroup$
1
$\begingroup$

The grammar is ambiguous, so no deterministic parsing technique will be able to handle it. The parsing techniques called "shift-reduce" are all deterministic.

$\endgroup$
1
$\begingroup$

Expanding on @vonbran's answer, here is why the grammar is mbiguous. Say you have a statement of the form

if (expr)  if (expr) stmt else stmt 

, then the else can belong to either ifs.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.