利用PHP实现词法分析器与自定义语言_第1页
利用PHP实现词法分析器与自定义语言_第2页
利用PHP实现词法分析器与自定义语言_第3页
利用PHP实现词法分析器与自定义语言_第4页
利用PHP实现词法分析器与自定义语言_第5页
已阅读5页,还剩7页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第利用PHP实现词法分析器与自定义语言private

function

isEnLetter()

{

if

($this-currChar

==

""

||

$this-currChar

==

$this-eof)

{

return

false;

}

$ord

=

mb_ord($this-currChar,

$this-currEncode);

if

($ord

ord('a')

$ord

ord('z'))

{

return

true;

}

return

false;

}

/**

*

@return

false|int

*

是否中文字符

*/

private

function

isCnLetter()

{

return

preg_match("/^[\x{4e00}-\x{9fa5}]+$/u",

$this-currChar);

}

/**

*

@return

bool

*

是否为数字

*/

private

function

isNumber()

{

return

is_numeric($this-currChar);

}

/**

*

@return

bool

*

是否是字符串

*/

private

function

isStr()

{

return

$this-matchCompleteStr();

}

/**

*

@return

string

*

匹配完整字符串

*/

private

function

matchCompleteStr()

{

$char

=

"";

if

($this-currChar

==

"\"")

{

$this-nextChar();

while

($this-currChar

!=

"\"")

{

if

($this-currChar

!=

"\"")

{

$char

.=

$this-currChar;

}

$this-nextChar();

}

return

$char;

}

return

$char;

}

/**

*

@return

bool

*

是否是操作符

*/

private

function

isOperator()

{

return

in_array($this-currChar,

$this-operatorList);

}

/**

*

@return

string

*

匹配中文字符

*/

private

function

matchUntilNextCharIsNotCn()

{

$char

=

"";

while

($this-isCnLetter())

{

$char

.=

$this-currChar;

$this-nextChar();

}

return

$char;

}

/**

*

@return

void

获取下一个字符

*

获取下一个字符

*/

private

function

nextChar()

{

$this-currCharPos

+=

1;

$this-currChar

=

mb_substr($this-input,

$this-currCharPos,

1);

if

($this-currChar

==

"")

{

$this-currChar

=

$this-

}

}

/**

*

@param

string

$input

*

@return

bool

*

是否是关键字

*/

private

function

isKeyword(string

$input)

{

return

($this-keywordList[$input]

"")

!=

"";

}

public

function

convert(array

$tokens)

{

$code

=

"";

foreach

($this-lexerIterator($tokens)

as

$generator)

{

switch

($generator["type"])

{

case

static::KW:

$code

.=

$this-keywordList[$generator["char"]];

break;

case

static::VAR:

$code

.=

sprintf("$%s",

$generator["char"]);

break;

case

static::OPR:

$code

.=

$this-replace($generator["char"]);

break;

case

static::INT:

$code

.=

$generator["char"];

break;

case

static::STR:

$code

.=

sprintf("\"%s\"",

$generator["char"]);

break;

default:

$code

.=

$generator["char"];

}

}

return

$code;

}

private

function

replace(string

$char)

{

return

str_replace("+",

".",

$char);

}

/**

*

@param

array

$tokens

*

@return

\Generator

*/

private

function

lexerIterator(array

$tokens)

{

foreach

($tokens

as

$index

=

$token)

{

yield

$token;

}

}

三、如何使用

require

__DIR__

.

"/vendor/autoload.php";

//

定义一段代码

$code

=

EOF

姓名="腕豪";

问候="你好啊";

地址=(1+2)

*

3;

如果(地址

3){

地址=1;

地址="艾欧尼亚"

说话

=

("我"+"爱")+"你";

返回

姓名+年龄;

$lexer

=

new

Lexer($code);

//

自定义你的关键字

$kwMap

=

[

"如果"

=

"if",

"否则"

=

"else",

"返回"

=

"return",

"否则如果"

=

"elseif"

$lexer-setKeywordList($kwMap);

//

这里是生成的词

$tokens

=

$lexer-parseInput();

//

将生成的词转成php,当然你也可以尝试用php-parse转ast再转成php,这里只是简单的拼接

var_dump($lexer-convert($tokens));

生成词

[{

"type":

"variable",

"char":

"姓名",

"pos":

2

"type":

"operator",

"char":

"=",

"pos":

2

"type":

"string",

"char":

"腕豪",

"pos":

7

"type":

"operator",

"char":

";",

"pos":

8

"type":

"variable",

"char":

"问候",

"pos":

13

"type":

"operator",

"char":

"=",

"pos":

13

"typ

e":

"string",

"char":

"你好啊",

"pos":

17

"type":

"operator",

"char":

";",

"pos":

18

"type":

"variable",

"char":

"地址",

"pos":

23

"type":

"operator",

"char":

"=",

"pos":

23

"type":

"operator",

"char":

"(",

"pos":

24

"type":

"integer",

"char":

"1",

"pos":

25

"type":

"operator",

"char":

"

+",

"pos":

26

"type":

"integer",

"char":

"2",

"pos":

27

"type":

"operator",

"char":

")",

"pos":

28

"type":

"operator",

"char":

"*",

"pos":

30

"type":

"integer",

"char":

"3",

"pos":

32

"type":

"operator",

"char":

";",

"pos":

33

"type":

"keyword",

"char":

"如果",

"pos":

37

"type":

"nul

l",

"char":

"

",

"pos":

38

"type":

"operator",

"char":

"(",

"pos":

38

"type":

"variable",

"char":

"地址",

"pos":

41

"type":

"operator",

"char":

"",

"pos":

42

"type":

"integer",

"char":

"3",

"pos":

44

"type":

"operator",

"char":

")",

"pos":

45

"type":

"operator",

"char":

"{",

"pos":

46

"type":

"variable",

"char":

"地址",

"pos":

55

"type":

"operator",

"char":

"=",

"pos":

55

"type":

"integer",

"char":

"1",

"pos":

56

"type":

"operator",

"char":

";",

"pos":

57

"type":

"operator",

"char":

"}",

"pos":

60

"type":

"keyword",

"char":

"否则",

"pos":

62

"type":

"null",

"char

":

"

",

"pos":

63

"type":

"operator",

"char":

"{",

"pos":

63

"type":

"variable",

"char":

"地址",

"pos":

72

"type":

"operator",

"char":

"=",

"pos":

72

"type":

"string",

"char":

"艾欧尼亚",

"pos":

78

"type":

"operator",

"char":

";",

"pos":

79

"type":

"operator",

"char":

"}",

"pos":

82

"type":

"variable",

"char":

"说话",

"pos":

87

"type":

"operator",

"char":

"=",

"pos":

88

"type":

"operator",

"char":

"(",

"pos":

90

"type":

"string",

"char":

"我",

"pos":

93

"type":

"operator",

"char":

"+",

"pos":

94

"type":

"string",

"char":

"爱",

"pos":

97

"type":

"operator",

"c

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论