Need to create parse tree

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Need to create parse tree

 
0
  #1
Apr 30th, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Need to create parse tree

 
0
  #2
May 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 31
Reputation: tat2dlady is an unknown quantity at this point 
Solved Threads: 0
tat2dlady tat2dlady is offline Offline
Light Poster

Re: Need to create parse tree

 
0
  #3
May 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 2,413
Reputation: Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough Comatose is a jewel in the rough 
Solved Threads: 211
Team Colleague
Comatose's Avatar
Comatose Comatose is offline Offline
Taboo Programmer

Re: Need to create parse tree

 
0
  #4
May 1st, 2005
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:

  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:

  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. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5833 | Replies: 3
Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC