I have written parser_sub.mly
and lexer_sub.mll
which can parse a subroutine
. A subroutine
is a block of statement englobed by Sub
and End Sub
.
Actually, the raw file I would like to deal with contains a list of subroutines and some useless texts. Here is an example:
' a example file
Sub f1()
...
End Sub
haha
' hehe
Sub f2()
...
End Sub
So I need to write parser.mly
and lexer.mll
which can parse this file by ignoring all the comments (e.g. haha
, ' hehe
, etc.) and calling parser_sub.main
, and returns a list of subroutines.
Could anyone tell me how to let the parser ignore all the useless sentences (sentences outside a
Sub
andEnd Sub
)?Here is a part of
parser.mly
I tried to write:%{ open Syntax %} %start main %type <Syntax.ev> main %% main: subroutine_declaration* { $1 }; subroutine_declaration: SUB name = subroutine_name LPAREN RPAREN EOS body = procedure_body? END SUB { { subroutine_name = name; procedure_body_EOS_opt = body; } }
The rules and parsing for
procedure_body
are complex and are actually defined inparser_sub.mly
andlexer_sub.mll
, so how could I letparser.mly
andlexer.mll
do not repeat defining it, and just callparser_sub.main
?