Skip to content

Instantly share code, notes, and snippets.

@mormahr
Created January 3, 2022 22:15
Show Gist options
  • Save mormahr/354ce0f48c6e9399fcf23e2076c9e29a to your computer and use it in GitHub Desktop.
Save mormahr/354ce0f48c6e9399fcf23e2076c9e29a to your computer and use it in GitHub Desktop.
LALRPOP full shift/reduce error message
error: failed to run custom build command for `lalrpop-shift-repro v0.1.0 (/lalrpop-shift-repro)`
Caused by:
process didn't exit successfully: `/lalrpop-shift-repro/target/debug/build/lalrpop-shift-repro-33451c47716a31f3/build-script-build` (exit status: 1)
--- stdout
processing file `/lalrpop-shift-repro/src/test.lalrpop`
/lalrpop-shift-repro/src/test.lalrpop:37:5: 37:8: Conflict detected
when in this state:
Expr = Expr (*) "+" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Expr = Expr (*) "-" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmt = Expr (*) ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
and looking at a token `"+"` we can reduce to a `Stmt` but we can also shift
/lalrpop-shift-repro/src/test.lalrpop:37:5: 37:8: Local ambiguity detected
The problem arises after having observed the following symbols in the input:
Stmt+ Expr
At that point, if the next token is a `"-"`, then the parser can proceed in two different ways.
First, the parser could execute the production at
/lalrpop-shift-repro/src/test.lalrpop:37:5: 37:8, which would consume
the top 1 token(s) from the stack and produce a `Stmt`. This might then yield a parse tree like
Expr ╷ Stmt
├─Stmt──┤ │
├─Stmt+─┘ │
└─Stmt+──────┘
Alternatively, the parser could shift the `"-"` token and later use it to construct a `Expr`. This might
then yield a parse tree like
Stmt+ Expr "-" Unary
│ ├─Expr───────┤
│ └─Stmt───────┤
└─Stmt+────────────┘
See the LALRPOP manual for advice on making your grammar LR(1).
/lalrpop-shift-repro/src/test.lalrpop:41:5: 41:9: Conflict detected
when in this state:
Expr = (*) Expr "+" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Expr = (*) Expr "-" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Expr = (*) Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Num = (*) r#"[0-9]+"# ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmt = (*) Expr ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmt+ = Stmt+ (*) Stmt ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmts = Stmt+ (*) ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Term = (*) Num ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Term = (*) "(" Expr ")" ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Unary = (*) Term ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Unary = (*) "-" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
and looking at a token `"("` we can reduce to a `Stmts` but we can also shift
/lalrpop-shift-repro/src/test.lalrpop:41:5: 41:9: Conflict detected
when in this state:
Expr = (*) Expr "+" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Expr = (*) Expr "-" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Expr = (*) Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Num = (*) r#"[0-9]+"# ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmt = (*) Expr ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmt+ = Stmt+ (*) Stmt ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmts = Stmt+ (*) ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Term = (*) Num ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Term = (*) "(" Expr ")" ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Unary = (*) Term ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Unary = (*) "-" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
and looking at a token `"-"` we can reduce to a `Stmts` but we can also shift
/lalrpop-shift-repro/src/test.lalrpop:41:5: 41:9: Conflict detected
when in this state:
Expr = (*) Expr "+" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Expr = (*) Expr "-" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Expr = (*) Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Num = (*) r#"[0-9]+"# ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmt = (*) Expr ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmt+ = Stmt+ (*) Stmt ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmts = Stmt+ (*) ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Term = (*) Num ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Term = (*) "(" Expr ")" ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Unary = (*) Term ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Unary = (*) "-" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
and looking at a token `r#"[0-9]+"#` we can reduce to a `Stmts` but we can also shift
/lalrpop-shift-repro/src/test.lalrpop:37:5: 37:8: Conflict detected
when in this state:
Expr = Expr (*) "+" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Expr = Expr (*) "-" Unary ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
Stmt = Expr (*) ["(", ")", "+", "-", r#"[0-9]+"#, EOF]
and looking at a token `"+"` we can reduce to a `Stmt` but we can also shift
/lalrpop-shift-repro/src/test.lalrpop:37:5: 37:8: Local ambiguity detected
The problem arises after having observed the following symbols in the input:
Stmt+ Expr
At that point, if the next token is a `"-"`, then the parser can proceed in two different ways.
First, the parser could execute the production at
/lalrpop-shift-repro/src/test.lalrpop:37:5: 37:8, which would consume
the top 1 token(s) from the stack and produce a `Stmt`. This might then yield a parse tree like
Expr ╷ Stmt
├─Stmt──┤ │
├─Stmt+─┘ │
└─Stmt+──────┘
Alternatively, the parser could shift the `"-"` token and later use it to construct a `Expr`. This might
then yield a parse tree like
Stmt+ Expr "-" Unary
│ ├─Expr───────┤
│ └─Stmt───────┤
└─Stmt+────────────┘
See the LALRPOP manual for advice on making your grammar LR(1).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment