I believe everyone who reads it is familiar with the dangling else ambiguity. Therefore I will skip the explanation. I found on a compilers book(the dragon book) a not ambiguous grammar that represents IF and ELSE. Here it is.
stmt->matched_stmt | open_stmt
matched_stmt->if exp then matched_stmt else matched_stmt | other
open_stmt->if exp then stmt | if exp then matched_stmt else open_stmt
The problems is:
open_stmt->if exp then stmt | if exp then matched_stmt else open_stmt
In order to make the grammar I am work on a LL(1) grammar, I need to eliminate left common prefix, and in this case the left common prefix is:
if exp then
when I try to factory it then it again become ambiguous, this is what I tried.
stmt->matched_stmt | open_stmt
matched_stmt->if exp then matched_stmt else matched_stmt | other
open_stmt->if exp thenO'
O'->stmt | matched_stmt else open_stmt
Which is ambiguous, can anyone help me make it not ambigous and with no common left prefix? thank you very much