General Information
Tutorials
Reference Manuals
Libraries
Translation Tasks
Tools
Administration
|
Abstract Syntax Tree UnparsingUsing an UnparserIn order to make the discussion concrete, we will use a trivial expression language as an example. That language is defined by the FunnelWeb file `example.fw':
@O@<example.lido@>==@{ RULE PlusExp: Expression ::= Expression '+' Expression END; RULE StarExp: Expression ::= Expression '*' Expression END; RULE Parens: Expression ::= '(' Expression ')' END; RULE IdnExp: Expression ::= Identifier END; RULE IntExp: Expression ::= Integer END; RULE CallExp: Expression ::= Identifier '(' Arguments ')' END; RULE ArgList: Arguments LISTOF Expression END; @} @O@<example.gla@>==@{ Identifier: C_IDENTIFIER Integer: C_INT_DENOTATION @} @O@<example.con@>==@{ Axiom: Expression . Expression: Expression '+' Term / Term . Term: Term '*' Factor / Factor . Factor: Identifier / Integer / Identifier '(' Arguments ')' / '(' Expression ')' . Arguments: Expression // ',' . @} @O@<example.map@>==@{ MAPSYM Expression ::= Term Factor . @}
The non-literal terminal symbols
The concrete grammar and mapping specification
together provide
three precedence levels of Finally, the concrete grammar specifies the comma as the argument separator in procedure calls. This fact is not, however, explicit in the tree. How could a pretty-printer for this language be constructed? Eli is capable of generating an unparser specification from the FunnelWeb file given above. That specification is, itself, a FunnelWeb file. These two FunnelWeb files, plus some additional information, provide a complete definition of the pretty-printer. Suppose that the additional information will be provided by a FunnelWeb file `Add.fw'. Here is a type-`specs' file that will define the pretty-printer:
example.fw example.fw :idem Add.fw The first line describes the file defining the language, the second describes the textual unparser derived from the language definition (see Deriving an Unparser), and the last describes the file defining the additional information. A processor is derived from this type-`specs' file in the usual way (see exe -- Executable Version of the Processor of Products and Parameters Reference). `Add.fw' must specify three additional pieces of information to complete the pretty-printer:
Our sample language involves only expressions, so the formatting strategy can be specified by instantiating a library module used to format C programs:
$/Output/C_Separator.fw
The generated unparser specification establishes a type-
SYMBOL Axiom COMPUTE Sep_Out(THIS.IdemPtg); END;
Finally, we can ensure that commas separate the arguments by overriding
the generated
@O@<Add.specs@>==@{ $/Output/C_Separator.fw @} @O@<Add.lido@>==@{ SYMBOL Axiom COMPUTE Sep_Out(THIS.IdemPtg); END; RULE: Arguments LISTOF Expression COMPUTE Arguments.IdemPtg= CONSTITUENTS Expression.IdemPtg SHIELD Expression WITH (PTGNode, PTGArgSep, IDENTICAL, PTGNull); END; @} @O@<Add.ptg@>==@{ ArgSep: $ { "," [Separator] } $ @}
The
|