I want to design a parser that recognizes correctly written arithmetic expressions on rational numbers. The prepared analyzer should recognize rational numbers in the notation with a fractional dash, which consists of a numerator and denominator, separated by a sign, for example 2 | 3 (two thirds) or 12 | 5 (twelve fifths, i.e. two and two fifths).
Both negative and positive numbers should be recognized.
In addition, closing and opening brackets and the operators: +, -, *, / should be recognizable (addition, subtraction, multiplication and division respectively). For each correctly written expression, the program should print its numerical value to the standard output.
Right now I'm able to do similar code but with integers. I don't see a way to do this with rational numbers.
Do you have any suggestions?
I tried something like this but it won't work properly in all cases.
bot_max is the lowest denominator DIV is the character of division (int|int where | is division)
number : INT DIV INT {
if($3 >= bot_max){
bot_max = $3;
$$ = $1;
}
else{
$$ = ($1 * bot_max) / $3;
}
}
| MINUS INT DIV INT {
if($3 >= bot_max){
bot_max = $4;
$$ = $2;
}
else{
$$ = ($2 * bot_max) / $4;
}
}
;