Error Recovery

Rustlr supports two methods of error recovery. The first method requires the designation of a special terminal symbol as an error recovery symbol, using the directive "errsym" or "errorsymbol". This symbol is assumed to not conflict with actual input tokens. The error symbol can appear at most once on the right-hand side of a production rule. When an error is encountered (a lookahead symbol with no defined transition in the LR finite-state machine), the parser looks down the parse stack for a state that has a transition defined on the error symbol. It truncates the stack and performs all possible reductions until a state that can "shift" the error symbol is found. It then simuluates the shifting of the next state associated with the error symbol, with the default semantic value of its type, onto the stack. It then skips lookheads until a valid transition is found for the new state.

The second method of error recovery is rather straightforward: the grammar can define one or more "resynchronization" terminal symbols using the "resynch" directive. When an error is encountered, the parser skips ahead past the first resynchronization symbol. Then it looks down the parse stack for a state that has a valid transition on the next symbol. For languages that ends statements with a ; (semicolon), the ; is the natural choice as the resynch symbol. The parser will report an error message for the current statement, then skip over to the next statement.

The second (resynch) method of error recovery is attempted if the first method fails or if no error symbol is defined.

If both methods of error-recovery fail, the parser simply skips input tokens until a suitable action is found.

The following grammar can be used to experiment with these error-recovery methods. It parses "cout" statements in "C+-". The grammar contains an injected main function and can replace the "main.rs" of a crate. One can test error recovery behavior with input such as

cout << x; cout << y x; cout z; cout << y << z ;
# Grammar for testing error-recovery
auto
grammarname cpm
nonterminals STAT STATLIST EXPR EXPRLIST
terminals x y z cin cout ; ( ) >> ERROR
lexterminal LLANGLE <<
topsym STATLIST
errsym ERROR
#resync ;

STATLIST --> STAT+
STAT --> cout LLANGLE EXPRLIST:s ; 

EXPR --> x | y | z
EXPR --> ( EXPR )
EXPRLIST --> EXPR<LLANGLE+>

STAT:ErrorStat --> ERROR ;
#STATLIST --> ERROR STAT
EXPR:ErrorExpr --> ( ERROR )
#EXPR --> EXPR ERROR )

!//injected into parser:
!mod cpm_ast;
!use std::io::{Write};
!
!fn readln()-> String {
!  let mut s = String::new();
!  let r = std::io::stdin().read_line(&mut s);
!  s
!}
!
!fn main() {
!   print!("Write something in C+- : ");
!   std::io::stdout().flush().unwrap();
!   let input = readln();
!   let mut lexer1 = cpmlexer::from_str(&input);
!   let mut parser1 = make_parser();
!   let res = parse_with(&mut parser1, &mut lexer1);
!   //let res = parse_train_with(&mut parser1, &mut lexer1);
!   println!("parsing success: {}",!parser1.error_occurred());
!   println!("Result: {:?}",res);
!}//main