In a language with standard let and if expressions, what should be the their precedence such that they can be nested without parentheses?
For example, I want to be able to parse both of these expressions:
if let a = b in c then d else e
let a = if b then c else d in e
I am using lalrpop to generate a parser. My attempt looks like this:
grammar;
pub Expr: () = {
#[precedence(level="0")]
Name => { println!("Name: {}", <>); },
#[precedence(level="1")]
#[assoc(side="right")]
"let" <name:Name> "=" <value:Expr> "in" <body:Expr> => { println!("Let: {}", name); },
#[precedence(level="2")]
"if" <check:Expr> "then" <then:Expr> "else" <els:Expr> => { println!("If"); },
};
Name: &'input str = r"[a-zA-Z_]+'?";
However, this can only parse the first of the above expressions. If I swap the precedence of the last two rules, then it can only parse the second!
What am I missing?
if
orlet
) allows to unambiguosly identify the applicable rule. The same goes for the nested expressions, so having alet
in theif
's condition is perfectly fine $\endgroup$