版權(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- GB 18384-2025電動(dòng)汽車安全要求
- 五年級(jí)上冊(cè)語(yǔ)文試卷及答案
- 衛(wèi)生招聘題庫(kù)及答案
- 過程裝備控制技術(shù)與應(yīng)用
- 部編版2021年四年級(jí)語(yǔ)文上冊(cè)期末測(cè)試卷【附答案】
- 淺析中職衛(wèi)校醫(yī)護(hù)生英語(yǔ)學(xué)習(xí)難點(diǎn)及應(yīng)對(duì)途徑
- 腳氣科普課件
- 2022-2023年人教版三年級(jí)語(yǔ)文下冊(cè)期中測(cè)試卷及答案【審定版】
- 電氣測(cè)量技術(shù)要領(lǐng)
- 申論考試題目分析及答案
- 南京醫(yī)科大學(xué)2026年招聘人事代理人員備考題庫(kù)及1套參考答案詳解
- 2026年教育平臺(tái)資源輸出協(xié)議
- 【《四旋翼飛行器坐標(biāo)系及相互轉(zhuǎn)換關(guān)系分析綜述》1000字】
- 2026浙江金華市婺城區(qū)城市發(fā)展控股集團(tuán)有限公司招聘59人筆試參考題庫(kù)及答案解析
- 靜脈補(bǔ)液課件
- 廣東深圳市鹽田高級(jí)中學(xué)2024~2025學(xué)年高一上冊(cè)1月期末考試化學(xué)試題 附答案
- 2026年輔警招聘考試試題庫(kù)附答案【完整版】
- 建筑施工風(fēng)險(xiǎn)辨識(shí)與防范措施
- 浙江省杭州地區(qū)六校2026屆化學(xué)高一第一學(xué)期期末學(xué)業(yè)水平測(cè)試試題含解析
- 2025年CFA二級(jí)估值與財(cái)務(wù)報(bào)表分析試卷(含答案)
- 2025年宜昌化學(xué)真題試卷及答案
評(píng)論
0/150
提交評(píng)論