M♪o 之學習筆記本《辰》組元︰【䷠】黃牛之革

派生碼訊

辰 龍

遯:亨,小利貞。

彖曰:遯亨,遯而亨也。 剛當位而應,與時行也。 小利貞,浸而長也。遯之時義大矣哉 !

象曰:天下有山,遯﹔君子以遠小人,不惡而嚴。

︰《 易 》易曰︰

六二,執之用黃牛之革,莫之勝說。

象曰:執用黃牛,固志也。

山 山高侵昊 天 天,時 厲 厲,難與行。何德能嘉遯?志固執用黃牛革。傲來有石 猴 猴,獨尊是佛陀,如來血脈一滴傳,因而敢登唯我峰。臨 事 事心踟躕,意緒無處寄

晚晴》李商隱

深居俯夾城,春去夏猶清。
天意憐幽草,人間重晚晴。
並添高閣迥,微注小窗明。
越鳥巢乾後,歸飛體更輕。

,總是時到時擔當。

 

派︰突然停課,吹嚮了《加百利之號角!!》︰

500px-GabrielHorn

傳說中大天使加百利之號角 Gabriel’s Horn 一旦響起,就是審判日 Judgment Day 的到來。然而卻沒有任何人見過這個號角?這也許正是義大利數學家埃萬傑利斯塔‧托里拆利 Evangelista Torricelli 想像創造托里拆利小號之原因︰它是一個表面積無限大但卻體積有限的三維物體,或許該是用著第五元素乙太才能構成的吧!!

羊角螺旋

Cornu_Spiral.svg

龍捲風

比方說︰『事實就是發生過了的事』? 如果曾經有一隻猴子真的敲打出了莎士比亞的哈姆雷特,那麽這是那隻猴子它自己能知道的事實嗎?假使連它自己都不能知道,人們會認為這是件發生過的事實嗎? 又假使歷史上根本沒有莎士比亞的哈姆雷特,然而未來將會有□□的○○,到那時有隻猴子曾使這事成真的了,難道真的可以歸結說過去曾經有一隻猴子真的敲打出 了□□的○○的嗎?

學習就像是個『羊角螺旋』的軌跡,一再的一次又一次覆裹著中心的主題,每次的回歸,總是帶著新的知識與舊的記憶。自古以來人類一直想方設法希望解開大自然的奧秘,也許終將能有一天,這個思想螺旋成了『龍捲風』,大到能含括天地萬物。

這樣的一個學習者將會如何建造自己知識之金字塔的呢?他會不會用『想像的實驗』去釐清『基本概念』之糾葛的呢?還是用『推導歸謬』的邏輯,去探測一個『自明假設』之深遠結論的呢?又或者會將在大自然中發現的方程式求解,然後『畫圖』與『演示』這個解之意義的呢?……

如果從人類的創造發明史來看,那樣的學習者終將使用當時之最好的『學習工具』,打造自己的『學習工具箱』,甚至會創新『既有之工具』。就現今來講,除了使用電腦的一般應用軟體之外 ── 比方說文書處理等等 ──,最重要的就是能掌握 『程式語言』與『數學語言』的工具。或許這正是樹莓派基金會一開始打造樹莓派時所想的重要原因,讓學習者能有學習的工具!!
……

 

生 ︰打算動手作家習,先讀 Christophe Delord 之

Toy Parser Generator
or
How to easily write parsers in Python

。 雖他自謙是個『玩具般』的語法分析產生器,一堆『字詞』無法望文生義,略列個梗概︰

7.2. reRegular expression operations

This module provides regular expression matching operations similar to those found in Perl.

Both patterns and strings to be searched can be Unicode strings as well as 8-bit strings. However, Unicode strings and 8-bit strings cannot be mixed: that is, you cannot match an Unicode string with a byte pattern or vice-versa; similarly, when asking for a substitution, the replacement string must be of the same type as both the pattern and the search string.

Regular expressions use the backslash character ('\') to indicate special forms or to allow special characters to be used without invoking their special meaning. This collides with Python’s usage of the same character for the same purpose in string literals; for example, to match a literal backslash, one might have to write '\\\\' as the pattern string, because the regular expression must be \\, and each backslash must be expressed as \\ inside a regular Python string literal.

The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '\' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation.

It is important to note that most regular expression operations are available as module-level functions and RegexObject methods. The functions are shortcuts that don’t require you to compile a regex object first, but miss some fine-tuning parameters.

Regular Expression HOWTO

Introduction

Regular expressions (called REs, or regexes, or regex patterns) are essentially a tiny, highly specialized programming language embedded inside Python and made available through the re module. Using this little language, you specify the rules for the set of possible strings that you want to match; this set might contain English sentences, or e-mail addresses, or TeX commands, or anything you like. You can then ask questions such as “Does this string match the pattern?”, or “Is there a match for the pattern anywhere in this string?”. You can also use REs to modify a string or to split it apart in various ways.

Regular expression patterns are compiled into a series of bytecodes which are then executed by a matching engine written in C. For advanced use, it may be necessary to pay careful attention to how the engine will execute a given RE, and write the RE in a certain way in order to produce bytecode that runs faster. Optimization isn’t covered in this document, because it requires that you have a good understanding of the matching engine’s internals.

The regular expression language is relatively small and restricted, so not all possible string processing tasks can be done using regular expressions. There are also tasks that can be done with regular expressions, but the expressions turn out to be very complicated. In these cases, you may be better off writing Python code to do the processing; while Python code will be slower than an elaborate regular expression, it will also probably be more understandable.

Recursive descent parser

In computer science, a recursive descent parser is a kind of top-down parser built from a set of mutually recursive procedures (or a non-recursive equivalent) where each such procedure usually implements one of the productions of the grammar. Thus the structure of the resulting program closely mirrors that of the grammar it recognizes.[1]

A predictive parser is a recursive descent parser that does not require backtracking. Predictive parsing is possible only for the class of LL(k) grammars, which are the context-free grammars for which there exists some positive integer k that allows a recursive descent parser to decide which production to use by examining only the next k tokens of input. The LL(k) grammars therefore exclude all ambiguous grammars, as well as all grammars that contain left recursion. Any context-free grammar can be transformed into an equivalent grammar that has no left recursion, but removal of left recursion does not always yield an LL(k) grammar. A predictive parser runs in linear time.

Recursive descent with backtracking is a technique that determines which production to use by trying each production in turn. Recursive descent with backtracking is not limited to LL(k) grammars, but is not guaranteed to terminate unless the grammar is LL(k). Even when they terminate, parsers that use recursive descent with backtracking may require exponential time.

Although predictive parsers are widely used, and are frequently chosen if writing a parser by hand, programmers often prefer to use a table-based parser produced by a parser generator, either for an LL(k) language or using an alternative parser, such as LALR or LR. This is particularly the case if a grammar is not in LL(k) form, as transforming the grammar to LL to make it suitable for predictive parsing is involved. Predictive parsers can also be automatically generated, using tools like ANTLR.

Predictive parsers can be depicted using transition diagrams for each non-terminal symbol where the edges between the initial and the final states are labelled by the symbols (terminals and non-terminals) of the right side of the production rule.[2]

……

,真不知那吃 蟹 蟹的第一人,如何想來,又怎麼下得了口!?噗哧一笑,陡地,

貓  貓的理論上心頭︰

通常人們都說不要『迷信』,那『科學』自己會不會也『變成』一種迷信呢?或者說所謂的科學又是『什麼』呢?因為並非一直以來科學就是『像今天』一樣,現今的科學有著這樣的『一種精神』︰

一、事實立論
二、在事實的基礎上,建立用來解說的假設,然後形成它的理論
三、人人時時方方都可實驗,嘗試驗證或者推翻 一與二之所說
四、保持懷疑設想現象創革工具;再次持續不斷想推翻一、二和三所言

不知這樣的精神能不能』『西』之分?還是有『』『』之別呢?

大科學家牛頓養了一隻貓,他到那因為進出門戶不方便快樂』,所以在門上打了個洞,果然貓就快樂了起來。多年之後那貓生了小貓,牛頓很高興的在那個洞的旁邊又打了一個小洞,這樣小貓也一定會很『快樂』的了。真不知牛頓如何想出這個『貓的理論』︰

大貓走大洞;小貓走小洞。

難道『小貓』就不可大洞』?還是不這樣小貓』就會快樂』??

,何不效法牛頓,『大』洞『小』洞『快樂』的打,一『洞』通了是『一』洞,早晚總能『洞穿』!!☿☺

 

碼 ︰無 習 。停課。

 

行 ︰主意已定,  先將 Calc.py 中文化︰

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import math
import operator
import string
import tpg

if tpg.__python__ == 3:
    operator.div = operator.truediv
    raw_input = input

def make_op(op):
    return {
        '加'   : operator.add,
        '減'   : operator.sub,
        '乘'   : operator.mul,
        '除'   : operator.div,
        '餘數'   : operator.mod,
        '^'   : lambda x,y:x**y,
        '指數'  : lambda x,y:x**y,
        '餘弦' : math.cos,
        '正弦' : math.sin,
        '切弦' : math.tan,
        'acos': math.acos,
        'asin': math.asin,
        'atan': math.atan,
        '平方' : lambda x:x*x,
        '根號': math.sqrt,
        '正值' : abs,
        'norm': lambda x,y:math.sqrt(x*x+y*y),
    }[op]

class Calc(tpg.Parser, dict):
    r"""
        separator space '\s+' ;

        token pow_op    '\^|\*\*'                                               make_op         token add_op    '[加減]' make_op
        token mul_op    '[乘除餘數]'                                                 make_op         token funct1    '(餘弦|正弦|切弦|acos|asin|atan|平方|根號|正值)\b' make_op
        token funct2    '(norm)\b'                                              make_op         token real      '(\d+\.\d*|\d*\.\d+)([eE][-+]?\d+)?|\d+[eE][-+]?\d+' float
        token integer   '\d+'                                                   int         token VarId     '\w*'                                                    ;          START/e ->                 'vars' e=self.mem()
            |   VarId/v '=' Expr/e      self[v]=e             |   Expr/e         ;          Var/self.get(v,0)-> VarId/v ;          Expr/e -> Term/e ( add_op/op Term/t e=op(e,t)
                         )*
        ;

        Term/t -> Fact/t ( mul_op/op Fact/f     t=op(t,f)                          )*         ;          Fact/f ->                 add_op/op Fact/f f=op(0,f)
            |   Pow/f
        ;

        Pow/f -> Atom/f ( pow_op/op Fact/e      f=op(f,e)                         )?         ;          Atom/a ->                 real/a             |   integer/a             |   Function/a             |   Var/a             |   '<img src="http://www.freesandal.org/wp-content/ql-cache/quicklatex.com-47b60ee797a9328acb352e3ae7895aa5_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="' Expr/a '" title="Rendered by QuickLaTeX.com" height="19" width="68" style="vertical-align: -5px;"/>'         ;          Function/y ->                 funct1/f '<img src="http://www.freesandal.org/wp-content/ql-cache/quicklatex.com-22e0e32183ddc257d3b853d237d23b4a_l3.png" class="ql-img-inline-formula quicklatex-auto-format" alt="' Expr/x '" title="Rendered by QuickLaTeX.com" height="19" width="69" style="vertical-align: -5px;"/>' y = f(x)
            |   funct2/f '' Expr/x1 ',' Expr/x2 ''  y = f(x1,x2)         ;      """      def mem(self):         vars = sorted(self.items())         memory = [ "%s = %s"%(var, val) for (var, val) in vars ]         return "\n\t" + "\n\t".join(memory)  print("Calc (TPG example)") calc = Calc() while 1:     l = raw_input("\n:")     if l:         try:             print(calc(l))         except Exception:             print(tpg.exc())     else:         break </pre>    <a href="http://www.freesandal.org/wp-content/uploads/牛.gif"><img class="alignnone size-full wp-image-33359" src="http://www.freesandal.org/wp-content/uploads/牛.gif" alt="牛" width="29" height="43" /></a>  牛刀小試一番,沒料到這第一刀 <a href="http://www.freesandal.org/wp-content/uploads/初.gif"><img class="alignnone size-full wp-image-35625" src="http://www.freesandal.org/wp-content/uploads/初.gif" alt="初" width="30" height="44" /></a> 初裁,竟然就順遂︰☺ <pre class="lang:sh decode:true"># pi@raspberrypi ~ python3 calc.py
Calc (TPG example)

:天高 = 108000
108000

:地平 = 0
0

:仰角 = 3.14159 除 6
0.5235983333333333

:天高 乘 正弦(仰角)
53999.95863462362

:根號(179)
13.379088160259652

:vars

	仰角 = 0.5235983333333333
	地平 = 0
	天高 = 108000

:

 

訊 ︰宛如那一株

小草

作詞:林建助
作曲:陳輝雄

大風起 把頭搖一搖
風停了 又挺直腰
大雨來 彎著背 讓雨澆
雨停了 抬起頭 站直腳

不怕風 不怕雨 立志要長高
小草 實在是 並不小

。☿