LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-09-2004, 06:02 PM   #1
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Rep: Reputation: 30
Programming like question (context-free grammar)


lets say i have a Context-free grammar tree like this
Code:
      S
     /  \
   A      B
 / \     /  \
a   A   A    B
    |    |    |
    a    a    b
how many different derivations are there that generate the final string?

there has to be a way to do this without deriving every single one...

Last edited by true_atlantis; 03-09-2004 at 06:29 PM.
 
Old 03-09-2004, 10:55 PM   #2
eshwar_ind
Member
 
Registered: Feb 2004
Location: Bangalore
Distribution: Redhat
Posts: 144

Rep: Reputation: 15
I Think it depends on the string you are going to generate. As far as i have know there is no particular formula or method to find the number of different derivations for a string.
If any please let me know.
 
Old 03-10-2004, 01:24 AM   #3
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
that is what i was trying to specify with the "Context-free grammar", the final string will be the fronteir of the tree or "aaab" and what i was askin is how many was there are to derive that string... here would be an example of one way

S=>AB=>aAB=>aaB=>aaAB=>aaaB=>aaab

does that make sence?
 
Old 03-10-2004, 04:54 AM   #4
sNicker
Member
 
Registered: Oct 2003
Location: Italy
Distribution: Slackware 9.1, Slackware 10
Posts: 33

Rep: Reputation: 15
You are asking how many different trees can build the same terminal string in an ambiguous grammar.. The answer is: "depends". Are these the only grammatical rules?

1)S=>AB
2)A=>a
3)A=>aA
4)B=>b
5)B=>AB

In this case I can see only a couple of trees... The one you drew and

S =1> AB =2> aB =5> aAB =3> aaAB =2> aaaB =4> aaab

But maybe I'm missing something..

From what I remember you can't compute all the trees for every string for a generic grammar. You can build a solution for a given grammar and a given string, but you have to work on every single occurrence (in other words, no generic algorithm can compute all the different trees for a string in an ambiguous grammar...). Maybe my memory falls somewhere, you should try an internet search or ask a professor of formal languages...
 
Old 03-10-2004, 11:04 AM   #5
true_atlantis
Member
 
Registered: Oct 2003
Distribution: fedora cor 5 x86_64
Posts: 639

Original Poster
Rep: Reputation: 30
see this problem is from a book, and it givs you the tree and asks how many ways you can derive the string... i started doing a bunch of them... here is what i started to do...

S=>AB=>aAB=>aaB=>aaAB=>aaaB=>aaab
S=>AB=>aAB=>aaB=>aaAB=>aaAb=>aaab
S=>AB=>aAB=>aAAB=>aAAb=>aAab=>aaab
S=>AB=>aAB=>aAAB=>aAAb=>aaAb=>aaab
S=>AB=>aAB=>aAAB=>aAaB=>aAab=>aaab
S=>AB=>aAB=>aAAB=>aAaB=>aaaB=>aaab
S=>AB=>aAB=>aAAB=>aaAB=>aaaB=>aaab
S=>AB=>aAB=>aAAB=>aaAB=>aaAb=>aaab
S=>AB=>AAB=>aAAB=> 6X
S=>AB=>AAB=>AaB=>Aab=>aAab=>aaab
S=>AB=>AAB=>aAB=>aAAB 6X
S=>AB=>AAB=>AaB=>aAaB=>aAab=>aaab
S=>AB=>AAB=>AaB=>aAaB=>aaaB=>aaab
S=>AB=>AAB=>aaB=>aaAB=>aaaB=>aaab
S=>AB=>AAB=>aaB=>aaAB=>aaAb=>aaab
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Looking For: FREE Online Downloadable C Programming Book. rvijay Programming 10 01-28-2005 10:19 AM
how to know how much disk space is free on linux through programming payal_shah Linux - Hardware 3 12-12-2004 02:49 PM
Quick question: full-context diff? overbored Linux - Software 0 09-27-2004 04:11 AM
"ANSI C free manual for programming referance bill icse Programming 0 08-04-2003 11:18 AM
I want some free source programs to learn on system programming Gopal Linux - General 3 06-07-2003 07:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration