Design patterns for parsing
暂无分享,去创建一个
We provide a systematic transformation of an LL(1) grammar to an object model that consists of an object structure representing the non-terminal symbols and their corresponding grammar production rules, a union of classes representing the terminal symbols (tokens). We present a variant form of the visitor pattern and apply it to the above union of token classes to model a predictive recursive descent parser on the given grammar. Parsing a non-terminal is represented by a visitor to the tokens. For non-terminals that have more than one production rule, the corresponding visitors are chained together according to the chain of responsibility pattern in order to be processed correctly by a valid token. The abstract factory pattern, where each concrete factory corresponds to a non-terminal symbol, is used to manufacture appropriate parsing visitors.Our object-oriented formulation for predictive recursive descent parsing eliminates the traditional construction of the predictive parsing table and yields a parser that is declarative and has minimal conditionals. It not only serves to teach standard techniques in parsing but also as a non-trivial exercise of object modeling for objects-first introductory courses.
[1] Ralph Johnson,et al. design patterns elements of reusable object oriented software , 2019 .
[2] Andrew W. Appel,et al. Modern Compiler Implementation in Java , 1997 .
[3] Alfred V. Aho,et al. Compilers: Principles, Techniques, and Tools , 1986, Addison-Wesley series in computer science / World student series edition.
[4] Norman Neff. OO design in compiling an OO language , 1999, SIGCSE '99.