## Question:
## Write a function "gcd" that takes two
## numbers in $a0 and $a1, and returns their
## greatest common divisor. Both numbers will
## be greater than zero.
## Use Euclid's algorithm, based upon
## gcd(a, a) = a
## gcd(a, b) = gcd(b, a)
## gcd(a, b) = gcd(a-b, b) {use with a>b}
Euclid, a Greek mathematician who lived some time ago, figured out
these facts, which can be used recursively, or repeatedly, to arrive at
the GCD of any two numbers. He did not work with negative numbers, and
probably had the use of an abacus, which makes subtraction easy
The problem statement is:
## Write a function ffact that will compute ## the Factorial of the integer passed in $a0 ## 0! = 1! = 1.0 ## n! = (n-1)! * n ## calculate it as a real number using double precision ## as factorials get very large very soon ## ## return the answer in coprocessor register $f12 ##
If you encounter an error, write a message containing invalid and exit(0). Example:
if (op2==0) {puts("Divisionn by zero is invalid."); exit(0);}
Input will be a (possibly malformed) prefix expression. Write a function int preval(FILE *) to read the expression and (recursively) evaluate it. Note that you can call gettoken(somefile) to do the actual reading.
In your expression, you will need to separate adjacent numbers with
some other character(s) such as a space.
S -> P
P -> O P P
P -> n
O -> + | - | * | /
Input will be a (possibly malformed) fully parenthesized infix expression. Write a function int inval(FILE *) to read the expression and (recursively) evaluate it. Note that you can call gettoken(somefile) to do the actual reading.
In your expression, you will need to add enough parentheses so that each operation has exactly 2 operands.
Grammar: Our non-terminals could be called Expression, Term, and OperatorS -> E
E -> T O T
O -> + | - | * | /
T -> n
T -> ( E )
There are more possibilities to go wrong here. If you encounter an
unexpected token, please print a message containing "invalid" or
"Invalid."
My implementation uses the messages:
Invalid expression, number or parenthesis expected
Malformed, invalid operator
As the whole parsing process would be complex based upon simply a
character string, I recommend a two-step process, first recognizing
valid "tokens", and then implementing evaluation based upon the
"grammar" defined for the pre- or infix expressions.
The regular expression that defines our tokens is: [0-9][0-9]*|\+|\-|\*|\/||\(|\)
In this language, [] means a single character from a set of alternatives, * means zero or more repetitions of what preceeds it, and | gives alternate expressions. \ is an "escape", which should be used in front of all special characters, as most have meaning in regular expressions. So, in the above, \* means the actual character '*'
Thus, this RE means either a digit followed by zero or more digits
(which should be converted into an integer), or one of 6 special
characters we would expect in an arithmetic expression.
A FSM to recognize this pattern starts in a "start state" where it
accepts input until either a digit or one of the 6 characters is found.
Once tokens are obtained, a good way to describe a valid syntax is
with grammar rules. These are recursive (aha!) and consist of
non-terminal symbols (UPPERcase) and terminal symbols (lower case, or
special character). By convention, a set of grammar rules starts with
S, for "Start."
Here is a simple baby grammar consisting of Nouns and Verbs. Baby can
say things like "baby love mama" and "dog bite cat"
S -> N V N
N -> baby | mama | dog | cat
V -> love | eat | bite
A good way to use a grammar for programming is to create a function
for each kind of non-terminal (P, E, and T in the two programming
problems above).