I am writing a front-end to parse a set of txt
files, each file contains a set of procedures
, for instance one txt file looks like:
Sub procedure1
...
End Sub
Sub procedure2
...
End Sub
...
syntax.ml
contains:
type ev = procedure_declaration list
type procedure_declaration =
{ procedure_name : string; procedure_body : procedure_body }
type procedure_body = ...
...
parser.mly
looks like:
%start main
%type <Syntax.ev> main
%%
main: procedure_declarations EOF { List.rev $1 }
procedure_declarations:
/* empty */ { [] }
| procedure_declarations procedure_declaration { $2 :: $1 }
procedure_declaration:
SUB name = procedure_name EOS
body = procedure_body
END SUB EOS
{ { procedure_name = name; procedure_body = body } }
...
Now, I would like to retrieve the parsing of procedure_declaration
(for the purpose of exception handling). That means, I want to create parser_pd.mly
and lexer_pd.mll
, and let parser.mly
call parser_pd.main
. Therefore, parser_pd.mly
looks like:
%start main
%type <Syntax.procedure_declaration> main
%%
main: procedure_declaration EOF { $1 };
...
As most of the content in previous parser.mly
should be moved into parser_pd.mly
, parser.mly
now should be much lighter than before and look like:
%start main
%type <Syntax.ev> main
%%
main: procedure_declarations EOF { List.rev $1 }
procedure_declarations:
/* empty */ { [] }
| procedure_declarations procedure_declaration { $2 :: $1 }
procedure_declaration:
SUB name = procedure_name EOS
??????
END SUB EOS
{ { procedure_name = name;
procedure_body = Parser_pd.main (Lexer_pd.token ??????) } }
The question is I don't know how to write the ??????
part, and lexer.mll
which should be light (as it only reads token END
, SUB
and EOS
, and lets contents treated by lexer_pd.mll
). Maybe some functions from the Lexing
module are needed?
Hope my question is clear... Could anyone help?