Writing a formal language parser with Lisp

后端 未结 3 1600
灰色年华
灰色年华 2020-12-30 10:44

My company is designing a new domain specific scripting language; I have to implement a parser that translates our brand new programming language into a common scripting lan

3条回答
  •  一生所求
    2020-12-30 11:07

    Well, "the usual" way to do this in Common Lisp is … to do it in Lisp.

    A lot of domain-specific languages (and Lisp is pretty much notoriously specialized for this purpose!) are simply written as extensions to Lisp itself, using the macro facility. The upside is, it's trivial to write a DSL. The downside is, they often tend to "look like" lisp.

    Some examples of DSL's within the Common Lisp standard include the LOOP macro's own sub-language and the sub-language of the FORMAT specifiers.

    Since Lisp's s-expression notation is nominally a written form of an Abstract Syntax Tree, it's one way to avoid having much of your own lexer or parser; you can just use READ.

    That all being said, you can use some common packages that might be found in GRAYLEX or CL-LEXER or so forth; looking at the parsers for some other language with a similar syntax to yours might help. In Quicklisp, I see:

    CL-USER> (ql:system-apropos "parse")
    #                                                                                                                                                                                                   
    #                                                                                                                                                                                         
    #                                                                                                                                                                                                     
    #                                                                                                                                                                                                 
    #                                                                                                                                                                                           
    #                                                                                                                                                                                                            
    #                                                                                                                                                                                                        
    #                                                                                                                                                                                                        
    #                                                                                                                                                                                         
    #                                                                                                                                                                     
    #                                                                                                                                                                          
    #                                                                                                                                                                                                           
    #                                                                                                                                                                                                                       
    #                                                                                                                                                                                                           
    #                                                                                                                                                                                                      
    #                                                                                                                                                                                                             
    #                                                                                                                                                                                     
    #                                                                                                                                                                                                         
    #                                                                                                                                                                                                   
    #                                                                                                                                                                                                               
    #                                                                                                                                                                                                                
    #                                                                                                                                                                                                    
    #                                                                                                                                                                                                          
    #                                                                                                                                                                                                                
    #                                                                                                                                                                                                       
    #                                                                                                                                                                                        
    #                                                                                                                                                                               
    #                                                                                                                                                                                  
    #                                   
    

提交回复
热议问题