简体   繁体   中英

LR(1) parser - Making inline productions

How would you implement inline productions in an LR(1) parser? By inline productions I mean the production is there, but only for parsing-- it won't get generated in the parse tree/AST. How would I implement this? I have a list of inline productions, and I tried this: When there is a reduce action, if the lhs we are reducing to is not in the inlines list, reduce it and push the parent node on to the node stack; If the lhs is in the inlines list, then don't pop the children of the stack. This didn't work that well.

This gets at the difference between a parse tree and an abstract syntax tree . If I'm interpreting what you're saying correctly, you'd like your LR(1) parser to perform a parse, but then hand you back a tree other than the parse tree (specifically, there are some rules you need in the parser for the grammar to work, but you don't want to see them in the output).

The typical way you'd handle this would be to have your LR(1) parser execute semantic actions whenever performing a reduction. These actions would be what actually constructs the abstract syntax tree that you'd like to get once the algorithm finishes. You could then have the semantic action associated with your inline production basically be "take the existing parse tree and don't do anything with it," which corresponds to not having the parser emit anything for that production rule. For the other productions that actually do need to generate part of the syntax tree, you can have the semantic action generate part of the AST based on the production.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM