0

I have written a code in C to implement LR(1) parse table, however now I am facing a problem in printing the parse tree. How do we do that in C? The tree can have variable children and since the parsing algorithm is bottom-up, I am not sure where to begin. I want to make it look like the output of the pstree command or something of that sort.

Thank You

1 Answer 1

0

You'll find it easier to construct the parse tree in the parser, and print it top-down recursively once the parse is finished. That has several advantages: first, you're not restricted to parse trees, and can instead produce an abstract syntax tree, which is generally a lot clearer. Second, you can produce the tree in any form you like.

I was going to insert a proof that it's not possible to draw a parse tree while you're doing a left-to-right parse, but rather to my surprise, I convinced myself that it is possible provided you loosen the rules a bit: you have to be able to move the cursor up and down and left and right, but you can do it without every overprinting anything. To do that, you must draw the tree with the leaves (terminals) either on the left-hand margin or at the top, and you can then draw the reductions by drawing lines from each child, and remembering what the "connection point" of the resulting node is. I don't know if this is a suitable programming exercise for someone just starting with parsing, but it is possible, even with a console application.

Here's an example of a "reverse pstree" graph:

(41*x) + (27/y) - sin(2*pi*z);

(───────────────────────┐
41─factor─term┐         │
*─────────────┤         │
x─factor──────┴term─expr┤
)───────────────────────┴factor─term─expr┐
+────────────────────────────────────────┤
(───────────────────────┐                │
27─factor─term┐         │                │
/─────────────┤         │                │
y─factor──────┴term─expr┤                │
)───────────────────────┴factor─term─────┴expr┐
-─────────────────────────────────────────────┤
sin─────────────────────────┐                 │
(───────────────────────────┤                 │
2─factor─term┐              │                 │
*────────────┤              │                 │
pi─factor────┴term┐         │                 │
*─────────────────┤         │                 │
z─factor──────────┴term─expr┤                 │
)───────────────────────────┴factor─term──────┴expr

The algorithm used to produce that keeps the width and height of every stack item; terminals start with a height of 1 and a width of the terminal itself. In each reduction rule:

  • the cursor is moved to the right until it is to the right of every child;

  • the cursor is moved up to the bottom of the first child

  • for each child, a horizontal line is drawn from its bottom right corner to the column where the cursor is, and then a vertical line is drawn until the next child's row is reached. At the last child, the reduction's non-terminal name is printed, and the height and width of the resulting non-terminal are recorded.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.