4 ms·
Is there any formalization of how to invert a formatter, or is this just something they're figuring out in code as they go along?
by gaze 12y ago
Is there any formalization of how to invert a formatter, or is this just something they're figuring out in code as they go along?
- jlarocco 12y agoI looked at the code and the manual a little bit, and it almost looks like they're just writing an object oriented style wrapper syntax around Bison and Flex. At a quick glance, I'm not sure they're really inverting a formatter as much as they're adding a "syntax(...)" attribute that tells the tool how to format the parse tree elements generated from that grammar rule.
- agumonkey 12y agoHa, spinning it as inversion... not very classy.
- infogulch 12y agoMaybe that's why "classier" is in quotes in the title.
- david-gudeman 12y agoWell, the language of class patterns can be read naturally in either direction--parser or formatter--much like a grammar can be read as a recognizer or a generator. And in fact there are features in the class patterns that really only apply when parsing and are ignored when formatting. You could, with some justice, describe it as a reversible notation rather than as a formatting language that can be inverted. However, constructing a formatter from class patterns is a fairly trivial one-to-one conversion to C++ while constructing a parser requires some complex analysis and restructuring.
- akkartik 12y agoThis blog post by the author points at Daniel Yellin for the theoretical underpinnings: http://unobtainabol.blogspot.com/2014/10/constraint-attribute-grraphs.html http://unobtainabol.blogspot.com/2014/10/constraint-attribut...
- sklogic 12y agoI wonder how they handle cases like "a*(b+c)".
- ecaradec 12y agoI agree. I'm very weak in grammar, but I dont understand how this one would work. Shouldn't it be ? Expr ::= Expr + Term | Expr - Term | Term; Term ::= Term * Factor | Term / Factor | Factor Factor ::= Int | ( Expr )
- sklogic 12y agoExactly. And yet it's not easy to infer mechanically that an Expr (but not its Term branch) must be printed in "( ... )" when it's in the Factor branch of an AST.
- david-gudeman 12y agoThere are precedence indicators to describe precedence (BTW, this is needed for formatting as well as parsing). The details are too complicated for a reply, but they are in the language manual.