Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

This resource has not currently been approved, and is not currently linked to from our directory of resources. It is being displayed here for preview by the author and moderators only.
Rated
Read 275 times

Contents

Related Categories

The use of the code analysis library OpenC++: modifications, improvements, error corrections - 8. Improved error correction.

AndreyKarpov

8. Improved error correction.

Unfortunately, the error correction mechanism in OpenC++ sometimes causes program crash. Problem places in OpenC++ are the code similar to this:

if(!rDefinition(def)){
  if(!SyntaxError())
    return false;
  SkipTo('}');
  lex->GetToken(cp); // WARNING: crash in the same case.
  body = PtreeUtil::List(new Leaf(op), 0, new Leaf(cp));
  return true;
}

One should pay attention to those places where the processing of errors occurs and correct them the way shown by the example of Parser::rLinkageBody() and Parser::SyntaxError() functions. The general sense of the corrections is that after an error occurs, at first presence of the next lexeme should be checked with the use of CanLookAhead() function instead of immediate extraction of it by using GetToken,().

bool Parser::rLinkageBody(Ptree*& body)
{
    Token op, cp;
    Ptree* def;
    if(lex->GetToken(op) != '{')
        return false;
    body = 0;
    while(lex->LookAhead(0) != '}'){
        if(!rDefinition(def)){
            if(!SyntaxError())
                return false;           // too many errors
            if (lex->CanLookAhead(1)) {
              SkipTo('}');
              lex->GetToken(cp);
              if (!lex->CanLookAhead(0))
                return false;
            } else {
              return false;
            }
            body =
              PtreeUtil::List(new (GC) Leaf(op), 0,
                              new (GC) Leaf(cp));
            return true;                // error recovery
        }
        body = PtreeUtil::Snoc(body, def);
    }
    lex->GetToken(cp);
    body = new (GC)
      PtreeBrace(new (GC) Leaf(op), body, new (GC) Leaf(cp));
    return true;
}
bool Parser::SyntaxError()
{
    syntaxErrors_ = true;
    Token t, t2;
    
    if (lex->CanLookAhead(0)) {
      lex->LookAhead(0, t);
    } else {
      lex->LookAhead(-1, t);
    }
    if (lex->CanLookAhead(1)) {
      lex->LookAhead(1, t2);
    } else {
      t2 = t;
    }
    
    SourceLocation location(GetSourceLocation(*this, t.ptr));
    string token(t2.ptr, t2.len);
    errorLog_.Report(ParseErrorMsg(location, token));
    return true;
}

Comments