Should this be done in Bison or by post-processing of the AST?

eyeris <drewpvogel@gmail.com>
Thu, 08 Nov 2007 21:42:25 -0800

          From comp.compilers

Related articles
Should this be done in Bison or by post-processing of the AST? drewpvogel@gmail.com (eyeris) (2007-11-08)
Re: Should this be done in Bison or by post-processing of the AST? DrDiettrich1@aol.com (Hans-Peter Diettrich) (2007-11-09)
Re: Should this be done in Bison or by post-processing of the AST? jo@durchholz.org (Joachim Durchholz) (2007-11-09)
Re: Should this be done in Bison or by post-processing of the AST? drewpvogel@gmail.com (eyeris) (2007-11-12)
| List of all articles for this month |
From: eyeris <drewpvogel@gmail.com>
Newsgroups: comp.compilers
Date: Thu, 08 Nov 2007 21:42:25 -0800
Organization: Compilers Central
Keywords: parse, AST, question
Posted-Date: 09 Nov 2007 01:44:17 EST

I am writing a parser for a rather unconventional language. It is used
to process text, with commands enclosed in braces. The if construct is
a command, rather than a syntactical construct. Here is an example:


> Answer: I'm going to the [if month eq <12>]ski lodge[else]park[endif].


My parser deals with this example just fine. However this language has
a [endif all] command that closes all open [if] blocks. Here is
another example:


> Answer: [if show_answer eq <1>]I'm going to the [if month eq <12>]ski lodge[else]park[endif all].


In my current implementation, the resulting AST for the second example
would look like this:


text (Answer: )
      if (show_answer eq <1>)
            text (I'm going to the )
            if (month eq <12>)
                  text (ski lodge)
            else
                  text (park)
            text (.)


However the text block containing the period *should* be a child of
the root text node (containing the text Answer:). Since this is a
logical issue and not really a parsing issue, it seems like the
easiest way to remedy this is by creating a new node type for the
[endif all] command and then manually expand those nodes into normal
[endif] nodes, moving the siblings following the [endif all] node out,
making them sibling of the [endif all] node's parent after the parsing
is complete.


However this also seems like a problem that many people have solved
before, probably finding clever tricks in the process. Is there an
easy way to deal with this right in the Bison grammar file?


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.