《编译原理》阅读笔记: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)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,122评论 6 505
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,070评论 3 395
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,491评论 0 354
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,636评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,676评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,541评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,292评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,211评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,655评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,846评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,965评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,684评论 5 347
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,295评论 3 329
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,894评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,012评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,126评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,914评论 2 355

推荐阅读更多精彩内容