stevertigo wrote:
On Mon, Jul 6, 2009 at 3:54 AM, Neil Harrisusenet@tonal.clara.co.uk wrote:
Consider the difference between the ease of writing, say, the Python-like print "%02x" % find(":", param[1]) or even the Lisp-like (print (fmt "%02x" (find ":" (param 1)))) compared to writing an "English-like" equivalent such as PRINT THE NUMBER OF CHARACTERS BEFORE THE FIRST OCCURRENCE OF THE COLON CHARACTER IN THE FIRST POSITIONAL PARAMETER FORMATTED AS A TWO-DIGIT ZERO-PADDED HEXADECIMAL NUMBER USING LOWERCASE LETTERS FOR THE HEX DIGITS A TO F
Your example is a bit unfair though, Neil. For one, how would it be parsed? Two, it only implies and does not explicitly state the "find/search" functionality you use in the examples.
Oh, don't tempt me to write an implementation... a grammar for it might look something like this:
command ::= print-expr | ...
expr ::= find-expr | param-expr | arithmetic-expr | strong-expr
no-default-param-expr ::= "THE" ordinal-number-name "POSITIONAL PARAMETER" | "THE PARAMETER CALLED" name-expr
param-expr ::= no-default-param-expr ["UNLESS THE PARAMETER IS UNDEFINED, IN WHICH CASE USE" expr "INSTEAD"]
find-expr ::= "THE NUMBER OF CHARACTERS BEFORE THE FIRST OCCURRENCE OF" expr "IN" expr
arithmetic-expr ::= expr "MULTIPLIED BY" expr | expr "ADDED TO" expr | ... | "OPEN BRACKETS" expr "CLOSE BRACKETS"
substitution-expr ::= "THE STRING" expr ", SUBSTITUTING THE STRING" expr "FOR THE STRING" expr "THROUGHOUT"
string-expr ::= "THE" char-name "CHARACTER"
print-statement ::= "PRINT" expr "FORMATTED AS" print-format
print-format-atom ::= "A LITERAL STRING" | "A" ordinal-number-name "-DIGIT" [ "ZERO-PADDED" ] ["HEXADECIMAL" | "OCTAL" | "DECIMAL" ] "NUMBER" ["USING" ["UPPERCASE" | "LOWERCASE" "LETTERS FOR THE HEX DIGITS A TO F"]
print-format ::= print-format-atom | print-format-atom "FOLLOWED BY" print-format
ordinal-number-name ::= "FIRST" | "SECOND" | "THIRD" | "FOURTH" ...
cardinal-number-name ::= "ONE" | "TWO" | "THREE" | "FOUR" ...
and then throw it at a packrat parser or similar shortest-length parser for nondeterministic languages... I leave code generation and the runtime environment as an exercise for the student. To keep fully within the spirit of the exercise, don't forget to add automatic type coercion of all data types to strings wherever necessary.
-- Neil