SoftTimur
SoftTimur

Reputation: 5520

Exception handling in parsing

I am writing an analyzer which analyzes many independent files. All the files have the same structure: it have a Initiation(), followed by several procedures. For instance:

Sub Initiation()
...
End Sub

Sub procedure1()
...
End Sub

Sub procedure2()
...
End Sub

...

The Initiation() initiates everything, then the analysis of each procedure is independent. Before analyzing them, I need of cause to parse them.

In main.ml I use let procedures = Parser.main Lexer.token buf to call the parsing. If everything goes well, procedures contains the abstract syntax trees of the procedures: Initiation(), procedure1(), etc.

However, at the current stage, if the parsing raises an error while parsing a procedure, it returns a Parser.Error, and stops from parsing the whole file. As a result, the procedures after the troubling procedure cannot be parsed.

What I would like to do, is to have an error handling somewhere, so that let procedures = Parser.main Lexer.token buf always succeeds, even though inside a part of procedures cannot be parsed (in that case, its value can be UnparseableProcedure, instead of an abstract syntax tree).

The aim is really to parse as many as procedures for each file, and then analyze them as many as possible...

Does anyone know how to do it?

Upvotes: 0

Views: 130

Answers (1)

Kakadu
Kakadu

Reputation: 2839

Recovering from error in LR analyzers is difficult (and I always recommend to use parser-combinators) but maybe you can write something like that (?):

proc:
   begin_sub sub_name sub_body end_sub      { Some ($1,$2) }
|  begin_sub anything_except_endsub end_sub { None (* error case *) } 
;

I'm not sure that it will work but u can try.

Upvotes: 1

Related Questions