Rock It 《ML》JupyterLab 【丁】Code《七》語義【一】

然後大膽『解讀』《 TPG 》 Toy Parser Generator 『原始碼』,『遊藝』於『文法』『語意』物件之中,『採擷』 TPG 之『自生自成』的『精華』︰

Toy Parser Generator is a lexical and syntactic parser generator for Python. This generator was born from a simple statement: YACC is too complex to use in simple cases (calculators, configuration files, small programming languages, …).

TPG can very simply write parsers that are usefull for most every day needs (even if it can’t make your coffee). With a very clear and simple syntax, you can write an attributed grammar that is translated into a recursive descendant parser. TPG generated code is very closed to the original grammar. This means that the parser works like the grammar. A grammar rule can be seen as a method of the parser class, symbols as method calls, attributes as method parameters and semantic values as return values. You can also add Python code directly into grammar rules and build abstract syntax trees while parsing.

The first application of TPG is TPG itself. The first (not released) version of TPG has been written by hand then was used to generate next versions. Now TPG can generate itself.

For an uptodate documentation, please read tpg.pdf.

─── 《W!O 的派生‧十日談之《九》

 

所謂四則運算『先乘除後加減』是說︰

2 \ + \ 3 \times 5 \ = 2 \ + \ 15

用『括號』() ── 運算優先權 ── 解釋為︰

2 \ + \ 3 \times 5 \ = 2 \ + \ (\ 3 \times 5)

故其『語意』不會是︰

2 \ + \ 3 \times 5 \ = (2 \ + \ 3) \times 5 \ = \ 25 也。

派生 Python 會不順應『數學傳統』乎?想要目睹耶?

 

實則『語義』通常孕育於『語法結構』中呦☆

抽象語法樹

下列輾轉相除法編程碼的抽象語法樹:

while b ≠ 0

if a > b

a := a − b
else

b := b − a
return a

計算機科學中,抽象語法樹Abstract Syntax Tree,AST),或簡稱語法樹(Syntax tree),是原始碼語法結構的一種抽象表示。它以樹狀的形式表現程式語言的語法結構,樹上的每個節點都表示原始碼中的一種結構。之所以說語法是「抽象」的,是因為這裡的語法並不會表示出真實語法中出現的每個細節。比如,嵌套括號被隱含在樹的結構中,並沒有以節點的形式呈現;而類似於 if-condition-then 這樣的條件跳轉語句,可以使用帶有兩個分支的節點來表示。

和抽象語法樹相對的是具體語法樹(通常稱作分析樹)。一般的,在原始碼的翻譯和編譯過程中,語法分析器創建出分析樹,然後從分析樹生成AST。一旦AST被創建出來,在後續的處理過程中,比如語義分析階段,會添加一些信息。

※ 註︰

sudo pip3 install showast

/show_ast

An IPython notebook plugin for visualizing ASTs.

showast

PyPI version Liberapay receiving

An IPython/Jupyter notebook plugin for visualizing abstract syntax trees.

 

若言連『計算機語言』之『語義學』都尚未得到定論︰

Semantics (computer science)

In programming language theory, semantics is the field concerned with the rigorous mathematical study of the meaning of programming languages. It does so by evaluating the meaning of syntactically valid strings defined by a specific programming language, showing the computation involved. In such a case that the evaluation would be of syntactically invalid strings, the result would be non-computation. Semantics describes the processes a computer follows when executing a program in that specific language. This can be shown by describing the relationship between the input and output of a program, or an explanation of how the program will be executed on a certain platform, hence creating a model of computation.

Formal semantics, for instance, helps to write compilers, better understand what a program is doing, and to prove, e.g., that the following if statement

if 1 == 1 then S1 else S2

has the same effect as S1 alone.

Overview

The field of formal semantics encompasses all of the following:

  • The definition of semantic models
  • The relations between different semantic models
  • The relations between different approaches to meaning
  • The relation between computation and the underlying mathematical structures from fields such as logic, set theory, model theory, category theory, etc.

It has close links with other areas of computer science such as programming language design, type theory, compilers and interpreters, program verification and model checking.

……

Describing relationships

For a variety of reasons, one might wish to describe the relationships between different formal semantics. For example:

  • To prove that a particular operational semantics for a language satisfies the logical formulas of an axiomatic semantics for that language. Such a proof demonstrates that it is “sound” to reason about a particular (operational) interpretation strategy using a particular (axiomatic) proof system.
  • To prove that operational semantics over a high-level machine is related by a simulation with the semantics over a low-level machine, whereby the low-level abstract machine contains more primitive operations than the high-level abstract machine definition of a given language. Such a proof demonstrates that the low-level machine “faithfully implements” the high-level machine.

It is also possible to relate multiple semantics through abstractions via the theory of abstract interpretation.

………

 

故宜暫放下未來

自然後設語義

自然後設語義 (Natural Semantic Metalanguage, NSM),又稱自然語義後設語言,或是簡稱自然語義理論,是語言學當中語義學的理論之一,源起於波蘭語言學者安德列傑‧波古斯洛斯基(Andrzej Bogusławski)的概念。主要理論創建者是澳洲籍波蘭語言學家安娜 ‧維茲畢卡(Anna Wierzbicka),創於70年代早期(Wierzbicka 1972), 其先在華沙大學,後來以澳洲國立大學為學派的大本營。90年代後又與澳洲學者克里福‧高得(Cliff Goddard)繼續合作拓展研究領域和方向(Goddard & Wierzbicka 1994, 2002)。

理論

自然後設語義學派著重於使用有限、簡單、普遍的詞彙概念作為語義元(semantic primitives或semantic primes),運用來把較為複雜的語義概念分析到最簡單的概念,此種語義分析稱為簡約釋意(reductive paraphrase)。自然後設語義廣泛研究語言與認知、語言與文化等議題。主要的研究領域包括詞彙語義、語法語義、措辭法 、語用學,以及跨文化溝通等。這個理論強調語義學研究應該針對語義本身的描述,而非結構學派重視的語義關係或是形式語義學派重視的命題真假值。語義的描述要能夠給出簡單、清楚且無循環的定義,使用的後設語言應該是自然語言當中的語義元詞,反對透過抽象邏輯式的後設語言來呈現,因為抽象的後設語言也還是需要透過自然語言去了解其定義。

目前使用這個理論研究過的語言包括英語俄語波蘭語法語德語西班牙語瑞典語等歐洲語言,還有馬來語(屬於南島語族)、日本語 韓語標準漢語、亞威語(Ewe)(屬於非洲語言)、東克雷語(East Cree)(屬於美洲語言)和楊固尼加加拉語(Yankunytjatjara)(屬於澳洲語言)等等。[1] [2]

 

…爭議,先多求認識言語為何而用哩◎