-2

I'm programming an OS. So I want to know how can an expression like (2*(7+8)-9)+2 can be initialised into variable using user defined function like extract() :

extract(char a[])
{
//some code
}

char expr[] = "(2*(7+8)-9)+2";
int value = extract(expr);

I'm doing this for variable initialisation from string.( Any tips/ideas/suggestions? )

  • extract acts like `eval` in other languages. u need to write a arithmetic interpreter. BTW, why do u want to initialize `value` this way? – delta Mar 24 '17 at 01:26
  • 2
    You are writing an _operating system_ and asking how to parse a simple expression? – AlexP Mar 24 '17 at 01:26
  • Yeah, I edited it – ARAVIND I M Mar 24 '17 at 01:26
  • Please help me. – ARAVIND I M Mar 24 '17 at 01:27
  • You write a lexer. You write a grammar. You write a parser to get the derivation tree of the expression according to the grammar. You interpret the derivation tree. – AlexP Mar 24 '17 at 01:28
  • I'm new to this language. Doing this for fun, don't know much, learned it only by using internet. Please help – ARAVIND I M Mar 24 '17 at 01:28
  • I don't know how to do that. – ARAVIND I M Mar 24 '17 at 01:30
  • http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm – delta Mar 24 '17 at 01:31
  • 3
    If you are new to C, then this is way too hard to start with something as complex as an infix expression parser. Start first with evaluating postfix expressions, you will find it much easier. If you want to see an example of a postifx expression parser (written in c++, so not the perfect example for C but will give you the general structure), check out postfix.cpp available here: https://courses.cs.sfu.ca/2016sp-cmpt-135-d1/pages/Assignment1 – ScottK Mar 24 '17 at 01:42
  • I understand little less complicated things. Please explain if you post source code. – ARAVIND I M Mar 24 '17 at 01:45
  • You can view my slides on how to parse and evaluate postfix expressions using C++ here but you will need to translate the general algorithm into C. It will give you the idea at least. https://courses.cs.sfu.ca/2016sp-cmpt-135-d1/pages/Wk02.1_pdf – ScottK Mar 24 '17 at 01:49
  • Also, how to add order of precedence to the code. – ARAVIND I M Mar 24 '17 at 02:11
  • That is exactly my point about using postfix first. Postfix has no precedence. For example: "2*3+1" in infix is written in postfix as: "2 3 * 1 +" and this can be evaluated by pushing numbers or results on a stack, then when you hit an operator, you pop the operands off the stack and push the result. No precedence needed. Work through this example before trying infix, which because of precedence is substantially more difficult. Infix parsing is not for beginners. – ScottK Mar 24 '17 at 02:32
  • But what if user types something like (1*2+3)/2+23 – ARAVIND I M Mar 24 '17 at 02:33
  • Don't try to understand Infix parsing. Have you read the slides I provided in the link? They will help you understand basic parsing, and it includes the solution in postfix.cpp in the solutions link to the assignment page. As a fyi..., In postfix, "(1*2+3)/2+23" becomes "1 2 * 3 + 2 / 23 +". – ScottK Mar 24 '17 at 02:35
  • So I got to parse input it into post fix? – ARAVIND I M Mar 24 '17 at 02:36
  • PS. I read it. Thank you. – ARAVIND I M Mar 24 '17 at 02:38
  • How do I parse infix to postfix? – ARAVIND I M Mar 24 '17 at 02:40
  • Before you can hope to code an infix parser in C, you should first attempt to code a postfix parser in C. These links show you how it is done in C++, so you have work ahead of you to get the algorithm translated to C. A PostFix parser is basic First Year stuff, but an Infix parser is Third Year level. Also writing an operating system is third year level as well. Since you say you are doing this for fun, start with something easy like PostFix first, in order to learn the language basics. – ScottK Mar 24 '17 at 02:42
  • OK, thank you, do you have materials for third year stuffs.It could be useful. – ARAVIND I M Mar 24 '17 at 02:44
  • Parsing infix expressions requires a technique like Recursive Decent. I doubt it would be helpful for you at this point. If you want more information, google it. Good luck! – ScottK Mar 24 '17 at 02:47
  • StackOverflow has a good write up on Infix parsing and Recursive Decent parsing here http://stackoverflow.com/a/13856790/6693299 – ScottK Mar 24 '17 at 02:59

1 Answers1

0

StackOverflow has already covered the topic of Infix expression parsing and Recursive Decent parsing using C++ here. Read it over to understand the algorithm, then see if you can convert it to C. That will definitely be "fun" and teach you a little bit about C along the way.

Community
  • 1
  • 1
ScottK
  • 1,526
  • 1
  • 16
  • 23