944,155 Members | Top Members by Rank

Ad:
  • Perl Discussion Thread
  • Unsolved
  • Views: 7299
  • Perl RSS
Apr 30th, 2005
0

Need to create parse tree

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
tat2dlady is offline Offline
32 posts
since Jan 2005
May 1st, 2005
0

Re: Need to create parse tree

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.
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
May 1st, 2005
0

Re: Need to create parse tree

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 = <INPUT>;

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.
Reputation Points: 10
Solved Threads: 0
Light Poster
tat2dlady is offline Offline
32 posts
since Jan 2005
May 1st, 2005
0

Re: Need to create parse tree

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:

Perl Syntax (Toggle Plain Text)
  1. 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:

Perl Syntax (Toggle Plain Text)
  1. foreach $line (@lines) {
  2. @fields = split(/ /, $line);
  3. if ($fields[0] eq 'OPAREN') {
  4. push(@fields, $fields[1]);
  5. print OUTPUT "$fields[0] ";
  6. print OUTPUT "$fields[1]";
  7. }elsif ($fields[0] eq 'CPAREN') {
  8. pop(@fields);
  9. print OUTPUT "$fields[0] ";
  10. print OUTPUT "\t$fields[1]"; # I modified this line, to add a tab first
  11. }elsif (($fields[0] ne 'OPAREN') && ($fields[0] ne 'CPAREN')) {
  12. print OUTPUT "$fields[0] ";
  13. print OUTPUT "$fields[1]";
  14. }
  15. }
Team Colleague
Reputation Points: 361
Solved Threads: 214
Taboo Programmer
Comatose is offline Offline
2,413 posts
since Dec 2004
Aug 13th, 2010
0
Re: Need to create parse tree
hello...pls help me create a parse tree...this is the source code:
#include<iostream.h>
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"<<ave;
return 0;
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mitchneys is offline Offline
9 posts
since Aug 2010
Aug 13th, 2010
0
Re: Need to create parse tree
How about actually posting in perl?
Reputation Points: 26
Solved Threads: 38
Posting Whiz in Training
mitchems is offline Offline
293 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC