How to create a parser(lex/yacc)?

喜你入骨 提交于 2019-11-30 05:57:11

Gardens Point LEX and the Gardens Point Parser Generator are strongly influenced by LEX and YACC, and output C# code.

Your grammar is simple enough that I think your current approach is fine, but kudos for wanting to learn the "real" way of doing it. :-) So here's my suggestion for a grammar (just the production rules; this is far from a full example. The actual GPPG file needs to replace the ... by C# code for building the syntax tree, and you need token declarations etc. - read the GPPG examples in the documentation. And you also need the GPLEX file that describes the tokens):

/* Your input file is a list of "top level elements" */
TopLevel : 
    TopLevel TopLevelElement { ... }
    | /* (empty) */

/* A top level element is either a comment or a block. 
   The COMMENT token must be described in the GPLEX file as 
   any line that starts with -- . */
TopLevelElement:
    Block { ... }
    | COMMENT { ... }

/* A block starts with the token START (which, in the GPLEX file, 
   is defined as the string "Start"), continues with some identifier 
   (the block name), then has a list of elements, and finally the token
   END followed by an identifier. If you want to validate that the
   END identifier is the same as the START identifier, you can do that
   in the C# code that analyses the syntax tree built by GPPG.
   The token Identifier is also defined with a regular expression in GPLEX. */
Block:
    START Identifier BlockElementList END Identifier { ... }

BlockElementList:
    BlockElementList BlockElement { ... }
    | /* empty */

BlockElement:
    (NAME | ADDRESS) QuotedString { ... }

You will first have to define the grammar for your parser. (Yacc part)

Seem like to be something like :

file : record file
     ;

record: start identifier recordContent end identifier {//rule to match the two identifiers}
      ;

recordContent: name value; //Can be more detailed if you require order in the fields

The lexical analysis will be perform be lex. And I guess your regex will be useful to defined them.

My answer is a rough draft, I advise you to look on the internet to find a more complete tutorial on lex/yacc flex/bison, and come back here if you have a more focused problem.

I also do not know if there is a C# implementation that would allowed you to keep a managed code. You may have to use unmanaged C / C++ import.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!