利用PHP實(shí)現(xiàn)詞法分析器與自定義語(yǔ)言_第1頁(yè)
利用PHP實(shí)現(xiàn)詞法分析器與自定義語(yǔ)言_第2頁(yè)
利用PHP實(shí)現(xiàn)詞法分析器與自定義語(yǔ)言_第3頁(yè)
利用PHP實(shí)現(xiàn)詞法分析器與自定義語(yǔ)言_第4頁(yè)
利用PHP實(shí)現(xiàn)詞法分析器與自定義語(yǔ)言_第5頁(yè)
已閱讀5頁(yè),還剩7頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

第利用PHP實(shí)現(xiàn)詞法分析器與自定義語(yǔ)言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

*

是否為數(shù)字

*/

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

獲取下一個(gè)字符

*

獲取下一個(gè)字符

*/

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

*

是否是關(guān)鍵字

*/

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);

//

自定義你的關(guān)鍵字

$kwMap

=

[

"如果"

=

"if",

"否則"

=

"else",

"返回"

=

"return",

"否則如果"

=

"elseif"

$lexer-setKeywordList($kwMap);

//

這里是生成的詞

$tokens

=

$lexer-parseInput();

//

將生成的詞轉(zhuǎn)成php,當(dāng)然你也可以嘗試用php-parse轉(zhuǎn)ast再轉(zhuǎn)成php,這里只是簡(jiǎn)單的拼接

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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論