Search code examples
parsingf#fsyacc

Is it possible to use FsYacc parser developed for one language as part of parsing process for other language?


I'm implementing parsing and expression evaluation for two languages L1 and L2. Important thing is that L1 can be used as separate language or as a part of L2, and L2 contains only several keywords, which are absent in L1.

I've done already Lexing -> Parsing -> AST production -> AST processing process for L1 and used for this F# with FsLex, FsYacc utilities.

Is it possible to use already developed parsing process (I mean tokens, AST production defined in L1 parser) during parsing another language L2?

AST: AST of the L1 will be used as part of AST for L2, and it will be used the same AST processing process.

FsLex Lexer: possible will be common for both languages, and I will need just include into L1 lexer also several absent keywords for L2. But if it is possible to have separate lexers for L1 and L2, and refer to L1 from L2 lexer, it will be excellent.

FsYacc Parser: I would not like 'copy-C' all L1 parsers code into the L2. Is there is a way to reference in my L2, tokens and AST data production defined in L1 parser?

Thanks in advance


Solution

  • Here is an interesting article which mentions the difficulties of grammar composition. The short-story is that you can't do what you want to do using yacc-like parser generators. That does not mean you can't achieve code reuse using some macro-based system, but it will remain a hack.