954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need to create parse tree

I have a file, which I have read, and need to print a parse tree for it. What I am doing is a syntax checker only for open and close parentheses (). If the item read is a open parentheses, I put it on a stack. If it is a close parentheses, I pop it off the stack. But, as I am reading and writing, I need to write the parse tree to the file. The input file looks like this:

OPAREN (
DEFUN Defun
Name Acount
OPAREN (
NAME L
CPAREN )
OPAREN (
COND cond
OPAREN (
...
etc.

The parse tree should look like this:

OPAREN (
DEFUN Defun
NAME Acount
OPAREN (
NAME L
CPAREN )
OPAREN (
COND cond
OPAREN (
... etc.

The problem I am having is printing the tabs when there is a new level. Does anyone know how I could print the tabs out at a new level. Thanks.

tat2dlady
Light Poster
32 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

I'm not sure I know what you mean..... I am not understanding "parse tree" so much, and I'm not quite understanding what you want done with Tabs. If you post code, also, it might be beneficial to use code tags which is bracket [ then the word CODE then ]. If you could just elaborate a little bit on what you need done, I'll see what I can do.

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

Here is my code:

use strict;

my @lines;
my $line;
my @fields;
my $fields;
my $count;

open(INPUT, "PerlOutput.txt") || print "Unable to open file";
open(OUTPUT, ">Parse.txt") || print "Unable to open output file";

@lines = ;

close(INPUT);

foreach $line (@lines) {
@fields = split(/ /, $line);
if ($fields[0] eq 'OPAREN') {
push(@fields, $fields[1]);
print OUTPUT "$fields[0] ";
print OUTPUT "$fields[1]";
}elsif ($fields[0] eq 'CPAREN') {
pop(@fields);
print OUTPUT "$fields[0] ";
print OUTPUT "$fields[1]";
}elsif (($fields[0] ne 'OPAREN') && ($fields[0] ne 'CPAREN')) {
print OUTPUT "$fields[0] ";
print OUTPUT "$fields[1]";
}
}

close(OUTPUT);

The input file looks like above (one lexeme and token per line in previous post). The output file did not print like I had it typed. It should be like this:

OPAREN (
DEFUN Defun
NAME Acount
[tabs here] OPAREN (
[tabs here] NAME L
[tabs here] CPAREN )
... etc.

The tabs are used to indent each level of a parse tree as you are printing it to the output file. I have the parse tree I just need to get it to indent each level when printing.Thanks for any help you have.

tat2dlady
Light Poster
32 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

Ok, what I Meant was that when you post code on the site, use code tags. Open a code tag with [ CODE ] with no spaces. Then close the code tag with [ / CODE ] no spaces. It will look like this, and will preserve tabs on this page for readability:

This is in code Tags


Also, I don't know why you are using the same array that contains all the parts of the split to push and pop from. I would have used a different one, but if it works, cool. I just don't understand HOW it works. Anyway, if you know where the indents need to be, then you can:

foreach $line (@lines) {
	@fields = split(/ /, $line);
	if ($fields[0] eq 'OPAREN') {
		push(@fields, $fields[1]);
		print OUTPUT "$fields[0] ";
		print OUTPUT "$fields[1]";
	}elsif ($fields[0] eq 'CPAREN') {
		pop(@fields);
		print OUTPUT "$fields[0] ";
		print OUTPUT "\t$fields[1]";    # I modified this line, to add a tab first
	}elsif (($fields[0] ne 'OPAREN') && ($fields[0] ne 'CPAREN')) {
		print OUTPUT "$fields[0] ";
		print OUTPUT "$fields[1]";
	}
}
Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 

hello...pls help me create a parse tree...this is the source code:
#include
int main()
{
int x,y;
double ave;
cout<<"Enter first number";
cin>>x;
cout<<"Enter second number";
cin>>y;
ave=(x+y)/2;
cout<<"the average is"<

mitchneys
Newbie Poster
9 posts since Aug 2010
Reputation Points: 10
Solved Threads: 0
 

How about actually posting in perl?

mitchems
Posting Whiz in Training
295 posts since Feb 2009
Reputation Points: 26
Solved Threads: 38
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You