Comple Example of Linguistic Analysis and Translation

Example. Read in three integers called A, B, C. Determine if the sum of the squares of the A and B is equal to C. Print the answer.

Linguistic Analysis

The purpose of the initial linguistic analysis is to translate the problem from informal specification to a specficiation for a procedural solution. The first step is to put the part of speech in the statement. Part of the analysis also determines pragmatic phrases such as the if statement below that is enclosed in braces {}.

[Read in] (v) three (adj) integers (n) called A (pn), B(pn), C (pn). Determine (v) {if (conj) [the sum] (pn) of the [squares of the A and B] [is equal to] (v) C (pn) }. Print (v) [the answer] (pn).

Remember, our goal is to rewrite the specification into something we can compute. There are several issues that need to be worked out. The proper nouns marked (pn) are the names of variables. The are several other proper nouns that are not so obvious: sum, square of A, square of B and "the answer" in the last sentence. Let's rewrite this problem using the encoding. Note: we can mix English and the formal words as we work towards a statement all in the formal words.

read A, B, C
Asquare = "square of A
Bsquare = "square of B"
sum = Asquare+Bsquare
"Determine" if sum == C
write "the answer"

Since there is nothing in the encoding that lets us "square A" directly, we need to find the computational counterpart of squaring. That's easy, since squaring A is the same as multiplying A by itself.

read A, B, C
Asquare = A*A
Bsquare = B*B
sum = Asquare+Bsquare
"Determine" if sum == C
write "the answer"

The last issue is the two sentences at the bottom. One thing we could do is have a new variable that remembers whether or not sum=C and then print the answer. That's certainly acceptable but not making use of the language. Remember that the if statement does remember whether it's condition is true or false. So we can rewrite the last two lines as an if statement with the execution picking the right print statement.

read A, B, C
Asquare = A*A
Bsquare = B*B
sum = Asquare+Bsquare
if sum == C then print "true" else print "false"

So, that's all there is to this problem.

Metacognitive Review

Whenever you solve a problem you should review what you did and what you learned.

What we did was first look at the problem statement and identity the parts of speech for each significant word in the problem statement. In general, verbs are computation statements and nouns are objects. Proper nouns are generally variables. Once we have the parts of speech we must ask "What is the computational content of the word?"

  1. Integers are primitive types so recognizing something as an integer brings in all the operations that integers can do.
  2. Proper nouns are turned into variables. Variables remember their values between assignments. Variables have an undetermined value until their first assignment.
  3. Words that do not have an obvious computational meaning must be analyzed further. The simplest is to replace the word with its definition (as we did with 'squaring').

The harder part comes if the words do not have an obvious translation. The two sentences at the end, "Determine ... the answer", had to be treated as a semantic unit. There were two "tricks". First, we knew that we needed to get the "if" statement into the game. We also saw a very round-about way of achieving our goal; but on second thought, there was an easier way. The easier way was to convert the "Determine ... C" part into the condition for the "if". Secondly, we needed to see that we could repeat the "print" statement in different parts of the "if" statement to achieve what we wanted.