《编译原理》阅读笔记:p25-p32

《编译原理》学习第 5 天,p25-p32总结,总计 8 页。

一、技术总结

1.lexical

lexical这个单词后续会经常用到,所以首先要搞懂它的英文意思,不然看到中文的“词法,语法,文法”这三个词的时候就会懵了——lexical对应这三个里面的哪一个?

(1)lexical: lexicon + al,加al后缀,表明lexical是一个形容词(adjective)。

(2)lexicon: 词根是lexis(a word)。all the words used in a particular language。意思是“全部词汇”,“词典”。

(3)lexical: adj. relating to words,词汇(word)相关的。

所以lexical 对应着“词(word)法”, lexical analyzer称为词法分析器。

2.production

(1)定义

p26,以C语言中的if-else 语句为例:

if (expression) statement else statement

如果使用expr表expression,使用stmt表示statement,那么上述语句就可以表示成:

stmt -> if (expr) stmt else stmt

->表示“can have the form”。形如“stmt -> if (expr) stmt else stmt”这样的rule称为production。

(2)作用

书中没有给出明确的定义,按照书中的定义,长“stmt -> if (expr) stmt else stmt”这样的就叫做production, 再简化一下还可写成: x -> y,“ x -> y”这个production的含义是什么呢?它的含义是x可以转为y。

(3)left side & right side

以 "stmt -> if (expr) stmt else stmt" 这个production为例,-> 左边的部分称为production的left side,-> 左边的部分称为production的right side。

3.token/terminal

在production中,lexical element(如:if, else)和 parenthsis称为token,也称为terminal、terminal symbol。

p27, "we assume that digits, signs such as <=, and boldface strings such as while are terminals"。根据这句话,terminal包含:(1)digit:如1,2,3。(2)signs: 如 <=。(3)boldface string: 如while。这里用boldface string很不好,boldface是从印刷的角度来说的,那非boldface的string是不是terminal呢?

4.nonterminal

stmt -> if (expr) stmt else stmt" 这个production

p26, variables like expr and stmt represent sequences of tokens and are called nonterminals。在这里,作者没有给出nonterminal的明确定义,暂且根据这句话的意思把像expr和stmt这样的variable称为nonterminal,然后继续往下阅读。

5.context-free grammar

(1)定义

首先,得对context-free grammar下定义,什么是context-free grammar?p26, In this section, we introduce a notation, called a context-free grammar(grammar, for short), for specifying the synaxt of a language。用于指定语言的语法的符号称为上下文无关文法,简写为grammar。

context-free grammar由四部分构成:

(1)a set of terminals。

(2)a set of nonterminals。

(3)a set of production。

(4)a start symbol。

“a designation of one of the nonterminals as the start symbol”——根据书中的定义,start symbol是从nonterminals中指定的。

6.|符号(or)

p27, For notational convenience, productions with the same nonterminals on the left can have their right sides grouped, with the algernative right sides separated by the symbol |, which we read as "or."——为了方便表示,可以把多个具有相同left side的production,写成一个production,production的right side用符号| 隔开,|读者“or”。

示例:

以7,3-1,9-5+2, 为例,我们把数字称为digit(即1是一个digit, 2是一个digit, 3是一个digtit......), 把用+号或-号隔开的expression称为list( "3-1"即是一个list,"9-5+2"是一个list,1等数字是特殊的list),那么上面这三个expression使用production来表示可以写成:

(1)digit -> 0 | 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9

(2)list -> digit

(3)list -> list - digit

(4)list -> list + digit

因为(2)、(3)、(4)这3个production的left side是一样的,都是list, 那么可以写成一个production:

list -> digit | list - digit | list + digit

7.production for nonterminal

p27, We say a production is for a nonterminal if the nonterminal appears on the left side of the production。

如果一个nonterminal出现在production的left side, 那么我们就说这个production是这个nonterminal的production(这里有点拗口,书上用的是for表示,意思就是这个production是归属于这个nonterminal的)。

当看到“production for nonterminal”我们就要想到nonterminal是在production的left side。

8.parse tree

(1)tree

关于tree相关的定义,参考维基百科即可。书中提到的interior node,实际就是我们常说的internal node。

(2)定义

grammar可以用tree来表示,这样的tree称为parse tree。parse tree描绘了从start symbol生成string(如:9-5+2)的过程。

二、其它

alphabet, string, language这三个概念书中没有严格的定义,但却又是核心概念,可以参考《Introduction to Automata Theory Languages and Computation》这本书。

今天的读书笔记写了很多,也是因为书上很多概念没有明确的定义,需要把相关的内容全部记下然,然后做交叉验证。

四、参考资料

1. 编程

(1)Alfred V. Aho,Monica S. Lam,Ravi Sethi,Jeffrey D. Ullman,《编译原理(英文版·第1版)》:https://book.douban.com/subject/5416783/

2. 英语

(1)Etymology Dictionary:https://www.etymonline.com

(2) Cambridge Dictionary:https://dictionary.cambridge.org

欢迎搜索及关注:编程人(a_codists)

posted @ 2024-06-29 17:43  codists  阅读(6)  评论(0编辑  收藏  举报