943,164 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 781
  • Java RSS
Feb 8th, 2010
0

Building a Language and a Parser with BNF Grammar

Expand Post »
I'm completely lost on this assignment:

Quote ...
The BNF rules (referred to as a grammar) are stored in a file. Your program is to read and echo the grammar file, and then walk the user through the process of building a sentence.

Once the grammar is read, the program will prompt the user to build a sentence in the specified grammar. For example assume the grammar is:

<s> -> let <var> = <val>
<val> -> <var> | <num>
<var> -> a | b
<num> -> 1 | 2 | 3
The program and the user could have the following interaction (user answers after the ?):
deriving <s>
deriving <var>
specify number of choice: 1 a, 2 b
?2
deriving <val>
specify number of choice: 1 <var>, 2 <num>
?2
deriving <num>
specify number of choice: 1 1, 2 2, 3 3
?1
The derived sentence is:
let b = 1
Assume the left hand side (LHS) of the first rule specifies the start symbol. Demonstrate the program with at least two grammars and for each grammar at least two examples. You need to use recursion to run through a rule after you start. Otherwise, you have to keep a stack of states of where you are.
I figure I have to use tokens of some sort to read in the grammar, but I'm stuck on the recursive part. I'm assuming the recursive part is if the grammar has:

<s> -> <s> ...

This is supposed to work for any grammar in a similar form to the example.

I appreciate any help or tips,
Nick
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
nickclarson is offline Offline
8 posts
since Oct 2006

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