Pratt Parsing For Algebraic Expressions

Parsing algebraic expressions is always a pain. If you need to compute, say, 2+4*2, the answer should be the same as (2 + (4 *2)), not ((2 + 4) * 2) — in other words, the right answer is 10, not 12. The classic way to do this is to use two stacks and a table of precedences for the operators. However, [Martin Janiczek] prefers to use Pratt Parsers and wants to show you how they work.

The parser is named after [Vaughn Pratt]. The algorithm works with a table of precedence where operators with higher precedence have higher numbers. It then builds a left and right portion of a string, using recursion. So if you consider 2+4*2, you wind up, on the first pass, with (2+ parse(4*2)). The second parse returns a full expression to produce: (2+(4*2)).

Continue reading “Pratt Parsing For Algebraic Expressions”

Retrotechtacular: Fire Control Computers In Navy Ships

Here is a two-part Navy training film from 1953 that describes the inner workings of mechanical fire control computers. It covers seven mechanisms: shafts, gears, cams, differentials, component solvers, integrators, and multipliers, and does so in the well-executed fashion typical of the era.

Fire control systems depend on many factors that occur simultaneously, not the least of which are own ship’s speed and course, distance to a target, bearing, the target’s speed and course if not stationary, initial shell velocity, and wind speed and direction.

The mechanisms are introduced with a rack and pinion demonstration in two dimensions. Principally speaking, a shaft carries a value based on revolutions. From this, a system can be geared at different ratios.

Cams take this idea further, transferring a regular motion such as rotation to an irregular motion. They do so using a working surface as input and a follower as output. We are shown how cams change rotary motion to linear motion. While the simplest example is limited to a single revolution, additional revolutions can be obtained by extending the working surface. This is usually done with a ball in a groove.

Continue reading “Retrotechtacular: Fire Control Computers In Navy Ships”