版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
第利用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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 骨增量术术后疼痛管理家属教育
- 2026年录音棚个人定制与音乐制作服务流程
- 2026年虚拟仿真实验教学专题讲座主题:开发技术与教学应用
- 2026年车间看板管理内容与更新制度
- 2026年催化剂行业行业协会合作与资源整合
- 2026年企业网络安全体系建设:从合规到主动防御
- 2026年老年人健康管理服务规范培训
- 2026年汽修厂汽车改装技术入门与法规培训
- 2026年培训师线上直播授课技巧
- 2026年化学教研组学期工作计划模板
- 2026浙江大学“一带一路”国际医学院行政部门招聘2人备考题库(2026年第6批)附答案详解(培优)
- 2026年江西省水投工程咨询集团有限公司社会招聘11人笔试备考试题及答案解析
- 2026上海市大数据中心招聘10名笔试模拟试题及答案解析
- 河北省秦皇岛市海港区2025-2026年九年级下一模化学试卷(含答案)
- 黑龙江省哈尔滨市南岗区2026年中考一模语文试题(含答案)
- (新疆二模)新疆2026年普通高考三月适应性检测理科综合试卷(含答案)
- GB/T 6109.5-2025漆包圆绕组线第5部分:180级聚酯亚胺漆包铜圆线
- MOOC 针灸学-经络养生与康复-暨南大学 中国大学慕课答案
- 消防设施系统维保方案
- 建筑施工现场职业危害防治措施
- 三角堰高度流量对照表
评论
0/150
提交评论