954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Building a Language and a Parser with BNF Grammar

I'm completely lost on this assignment:

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:

-> let = -> | -> a | b -> 1 | 2 | 3 The program and the user could have the following interaction (user answers after the ?): deriving deriving specify number of choice: 1 a, 2 b ?2 deriving specify number of choice: 1 , 2 ?2 deriving 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:

-> ...

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

I appreciate any help or tips,
Nick

nickclarson
Newbie Poster
8 posts since Oct 2006
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You