所有话言的核心都是相同的.都是基于巴氏范式的两个变种.
LEX&YACC是一种实现,一般用BNF.这块我还没搞太清,以前还有点印象真有些忘了.
还有向种,ANTLR,DFA.不管了.@H_404_11@
@H_404_11@
就是说,所有的语言,到今天为止,本质都是其于相同的理论,就是乔姆斯基和他的文法。@H_404_11@
@H_404_11@
今天,我想看看PERL 语言的内核是什么样子吧。
@H_404_11@
先进入perl原码目录,找:
*.l;*.y;*.lex;*.yacc;*.sym;*.g
@H_404_11@
win7的查找,到现在我还没学会.我是在XP下工作.
@H_404_11@
找到perly.y.
@H_404_11@
有点怪:lex哪去了?就是超人里的那个坏仁LEX?哪去了,虽说理论上,可以没有lex分析,但那样yacc日子会很难过.
@H_404_11@
所以,十有八有一定有lex.
@H_404_11@
忘了说,lex和yacc的关系,
这么说吧,lex只读一个个的asc码,而YACC只认TOKEN,
所以,组成token的任务,由lex完成。但TOKEY的定义,则是由YACC来完成。
@H_404_11@
我说得够精简吗?不象很多文章,写了一大摊。
@H_404_11@
举个示例吧,比如说,你在YACC(对了,yacc的意思是yer another language language),定义了一门语言,它识别两个单词(术语:token)@H_404_11@
那么你定义好两个token: HELLO WORLD
要注意,在yacc中,这叫token,你简单想象成,HELLO=0 WORLD =1,就对了。
@H_404_11@
但这个0和1谁给他它呢,这就得lex上场了。一般来说,lex和正则表达式,是密不可分的。lex要想完成yacc交给他的任务,没有正则表达式就瞎米了。
@H_404_11@
比如说吧,现在的流是" hello world"
lex一个字母一个字母地亲苦地找,状态机在o这个位,lex根据自己定义的正则表达式发现:找到了一个TOKEN,=HELLO,然后lex 就把这个0发给YACC.
总之吧,脏活累过,都在不上道的lex这。要不怎么叫lex呢。
@H_404_11@
现在问题来了,只有高雅的YACC是完成任务的。
而见鬼的lex,没起个好找的名字,NND。
@H_404_11@
很好办,通过我上面的分析,你肯定知道,TOKEN在YACC中定义,LEX生成。那么打个TOKEN必能找到。
不错。
@H_404_11@
就选 这个TOKEN: DOLSHARP
找到一个token.c
哈,OK了。
@H_404_11@
看看这家伙怎么描述自己的吧:
@H_404_11@
/* toke.c * * Copyright (C) 1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,* 2001,2002,2003,2004,2005,2006,2007,2008 by Larry Wall and others * * You may distribute under the terms of either the GNU General Public * License or the Artistic License,as specified in the README file. * */ /* * 'It all comes from here,the stench and the peril.' --Frodo * * [p.719 of _The Lord of the Rings_,IV/ix: "Shelob's Lair"] */ /* * This file is the lexer for Perl. It's closely linked to the * parser,perly.y. * * The main routine is yylex(),which returns the next token. */ /* =head1 Lexer interface This is the lower layer of the Perl parser,managing characters and tokens. =for apidoc AmU|yy_parser *|PL_parser Pointer to a structure encapsulating the state of the parsing operation currently in progress. The pointer can be locally changed to perform a nested parse without interfering with the state of an outer parse. Individual members of C<PL_parser> have their own documentation. =cut */ #include "EXTERN.h" #define PERL_IN_TOKE_C #include "perl.h" #include "dquote_static.c" #define new_constant(a,b,c,d,e,f,g) \ S_new_constant(aTHX_ a,STR_WITH_LEN(c),g) #define pl_yylval (PL_parser->yylval)@H_404_11@@H_404_11@
@H_404_11@
他自己说,他和YACC很紧密,这不是废话吗。
那么,是手写的?
@H_404_11@
我了个去,不由倒吸一口凉气。老外有这么牛吗?
@H_404_11@
下班了,下次有空再分析了。