Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jul 11th, 2006, 5:13 PM   #31
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by The Dark
Try something like:
tem<string>*     procstmt()
	 {
		  if (NT->theStruct.type != procedure)
		 {
		     if(compound()==NULL) return NULL; 
 
			 if (NT->theStruct.type != scln){  return NULL;}
			 NT = Si.cget();
		 }
		 else{ NT = Si.cget();
			y = proc();
			if (y != NULL) {
                          if (procstmt() == NULL)
                            return NULL;
                        }
			else return NULL; 
		 }
                 // Return something not NULL
	 }
the problem is I dont think we are allowed to have a function call itself!
thats why i followed the longer route
programmingnoob is offline   Reply With Quote
Old Jul 11th, 2006, 6:03 PM   #32
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
That sounds a bit harsh, if the spec calls itself, your program should be able to!
The Dark is offline   Reply With Quote
Old Jul 11th, 2006, 6:44 PM   #33
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by The Dark
That sounds a bit harsh, if the spec calls itself, your program should be able to!
i know!! ... isnt it unfair?!

let me give you an example though ...
//<dec-list> ::= <dec> | <dec-list> ; <dec>
item<string>*	 declist()
	 {
		 if (dec() == NULL) { return NULL;}
		 while (NT->theStruct.type == cma) 
		 {NT = Si.cget();
		 if (dec() == NULL) {return NULL;}}
		 NT = Si.cget();
	     return NT;
	 }
programmingnoob is offline   Reply With Quote
Old Jul 11th, 2006, 6:53 PM   #34
mikaoj
Programmer
 
mikaoj's Avatar
 
Join Date: Aug 2005
Location: Norway
Posts: 56
Rep Power: 0 mikaoj is an unknown quantity at this point
[EDIT] Shit, I was on the wrong site. Sorry! [/EDIT]

Delete this post, please.
__________________
Heh.
mikaoj is offline   Reply With Quote
Old Jul 11th, 2006, 7:49 PM   #35
The Dark
Expert Programmer
 
Join Date: Jun 2005
Posts: 893
Rep Power: 4 The Dark is on a distinguished road
How about something like:
item<string>*     procstmt()
{
  while (true)
  {
    if (NT->theStruct.type != procedure)
    {
      if (compound()==NULL)
        return NULL; 

      if (NT->theStruct.type != scln)
      {
        return NULL;
      }
      NT = Si.cget();
      break;
    }
    else
    {
      NT = Si.cget();
      if (proc()==NULL)
        return NULL;
      // Loop around again
    }
  }
  return OK;
}
(I had to reformat your code to get my head around it)

Also in your last example:
while (NT->theStruct.type == cma)
Should be
while (NT->theStruct.type == scln)
The Dark is offline   Reply With Quote
Old Jul 19th, 2006, 9:11 PM   #36
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
Quote:
Originally Posted by The Dark
How about something like:
item<string>*     procstmt()
{
  while (true)
  {
    if (NT->theStruct.type != procedure)
    {
      if (compound()==NULL)
        return NULL; 

      if (NT->theStruct.type != scln)
      {
        return NULL;
      }
      NT = Si.cget();
      break;
    }
    else
    {
      NT = Si.cget();
      if (proc()==NULL)
        return NULL;
      // Loop around again
    }
  }
  return OK;
}
(I had to reformat your code to get my head around it)

Also in your last example:
while (NT->theStruct.type == cma)
Should be
while (NT->theStruct.type == scln)
thanks! but i didnt really try your code, i fixed a couple of things in my function definiton and other things, and now my yes-no parser is working now.
programmingnoob is offline   Reply With Quote
Old Jul 19th, 2006, 9:23 PM   #37
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
now, i have to create abstract syntax tree. how do i do that?
i mean i dont actually understand the concept i think

for every single rule, the abstract syntax tree is given ... but I just do not know what they are talking about ...
programmingnoob is offline   Reply With Quote
Old Jul 20th, 2006, 5:35 AM   #38
Arevos
Programming Guru
 
Arevos's Avatar
 
Join Date: Aug 2005
Location: England
Posts: 1,499
Rep Power: 5 Arevos is on a distinguished road
Quote:
Originally Posted by programmingnoob
for every single rule, the abstract syntax tree is given ... but I just do not know what they are talking about ...
The concept is really pretty simple. It just sounds complex.

Take the following expression:
x = y + 1
The AST for this might look like:
   =
 /   \
x     +
    /   \
   y     1
The assignment operator has the greatest presidence, so it sits at the top of the tree. Hopefully you can see how the AST represents the parsed code in memory. With an AST, the code is easier to access, and easier to evaluate.

From a data structure point of view, each node of the tree has a number of pointers to its children (and possible one to its parent). You can represent the node as a struct, or a class.
Arevos is offline   Reply With Quote
Old Jul 22nd, 2006, 12:19 AM   #39
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
thanks arevos, i was just lacking confidence to actually implement the tree
programmingnoob is offline   Reply With Quote
Old Jul 22nd, 2006, 12:45 AM   #40
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 3 programmingnoob is on a distinguished road
As you guys know, I am done with my parser that only validates a given expression. However I did a little bit of cheating with the rule of stmt:-

<stmt> ::= <assign> | <call> | <pread> | <pwriteln> |
<compound> | <conditional>

Now what I did was just take a look at the first token, and determine which funcion should be called. Here are the corresponding rules that clash though:

<call> ::= <proc-name> ( <params> )

<assign> ::= $id := <exp>

They would clash because both <proc-name> and $id require the first token to be $id. So I wouldnt know which function to call unless I take a look at the second token.
However, if I do get the second token, then I can not call the respective functions (assign or call in this case) because it would mess thing up, if you know what i am talking about.

I think the main problem is there is no way i can put the token back, so what i basically did is i didnt call the functions "call" and "assign", instead i just wrote them out like this:

 if (NT->theStruct.type==id) 
		 { 
			 NT = Si.cget(); if (NT->theStruct.type==asgn){ NT = Si.cget(); if (exp() == NULL) {return NULL;}}
			 else if (NT->theStruct.type==lp) {NT = Si.cget(); if (params() == NULL) { return NULL;} if (NT->theStruct.type != rp){return NULL;}NT = Si.cget();}
			return NT; 
		 }

but that's cheating! isnt it?

but what's the way out?
programmingnoob is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:45 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC