how can i change this x=x+y of a c++ to a bnf form?

Recommended Answers

All 8 Replies

BNF is a way of describing the grammar for the language.

It it not a means of describing random program snippets such as this.

You might begin with <assignment> ::= <variable> "=" <expression>

thanx..

i did it that way but i guess something is wrong with my code... it doesn't want to run when i put something like x=1 or either x=x+1

Run?
What are you trying to 'run' ?

Post what you tried.

i have this program with me that i programmed but i am just stuck... i don't really know where it went wrong..... and it's my first time to program in c++... would you like to look at the code?

>would you like to look at the code?
This just slows down the process. Post your code, tell us what errors you're getting, and someone will get to it. "Will you look at the code?", "Can anyone help?", etc.. will invariably be answered with "Yes! Ask already!", so why not skip that part and just ask your question?

yes ma'am sorry for that...

don't mean to

I'm confused as to where 'bnf' fits into the discussion.

If you have some C++ code, then just post it already and stop with the 1 or 2 line posts which don't convey any information that anyone else could use to help you.

help!! do i need to change to get x=1.....?

//<ass>-><var>=<expr>
void assignment()
{
 if(nextToken == VARIABLE_TOK)
 {
  lex();
  if(nextToken==EQUAL_TOK)
  {
   lex();
   logical_expr();
   if(nextToken==END_STMT_TOK)
   {
    cout << "end statement: ok"<< endl;
    cout << END_STMT_TOK<< endl;
   cout << "assignment function: ok"<< endl;
   }
    else cout <<"put an end stmt ."<<endl;
  }
    else cout << "invalid it should be = operator only!"<< endl;
 }
  else cout << "invalid it should be variable only!"<< endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.