How can i Write a LEX and Bison Program to recognize a valid arithmetic expression that uses operators +, -, * and /and It gives priority to parentheses () and Accepts definition of variables and this variables can include the previous expressions and operations?
m=5
y=9+5
This question is a bit open-ended to fully answer. In brief, you will need Bison rules for an expression that looks something like:
varassign : VARIABLE '=' exp
;
exp : '(' exp ')'
| exp '+' exp
| exp '-' exp
| '-' exp
| NUMBER
;
You will need to specify the precedence of operators. Refer to the Bison Manual section Specifying Operator Precedence for details.
For lex, you just need to return the single-character tokens, and have a rule that parses [0-9]+ as a NUMBER token. And presumably you return any non-keyword string of letters as VARIABLE.