Search code examples
parsingocamlmenhirerror-recovery

Manually tweak env/stack/semantic value to achieve an error recovery


I'm trying to implement error recovery in this version of the project (make followed by ./parse e1.input to test).

Given (1 in e1.input, I would like the parser to act as if it parsed ) and build an AST Paren (Int 1). Thus, I tried

let (startp, endp) = positions in
let _ = feed (T T_RPAREN) startp () endp env in

However, it returned an error Invalid_argument("feed: outgoing transition does not exist")

hahahaha depth of env 2
hahahaha current_state_number 2
hahahaha startp 1:1
hahahaha endp   1:2

element number of state 2
element startp 1:1
element endp   1:2
element incoming_symbol an integer
element v toComplete
element item: an expression -> an integer.

element number of state 1
element startp 1:0
element endp   1:1
element incoming_symbol (
element v toComplete
element item: an expression -> ( .an expression )

Line 1, characters 2-2: Error (parsing)
  Error while analyzing an expression.
Fatal error: exception Invalid_argument("feed: outgoing transition does not exist")

Does anyone know why this error was raised?


Solution

  • Before feeding ), we should first reduce, the following code works:

    let env_new = force_reduction (find_production 1) env in
    let env_new_new = feed (T T_RPAREN) startp () endp env_new in
    (lex, input_needed env_new_new)
    

    For (1+2 for instance, we should reduce twice before feeding ), the following code works:

    let env_new = force_reduction (find_production 1) env in
    let env_new_new = force_reduction (find_production 3) env_new in
    let env_new_new_new = feed (T T_RPAREN) startp () endp env_new_new in
    (lex, input_needed env_new_new_new)