⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 输入文件.xlex

📁 词法分析器
💻 XLEX
字号:
    //注释测试
/*关键字定义*/
[Keyword_begin] //注释测试
	/*所有关键字每个一行,不能有多余字符*/
	main
	if
	else
	while
	for
	break
	continue
	return
	int
	float
	double
	char
	short
	switch
	case
	default
	string
	read
	write
[Keyword_end]

[Symbol_begin]
~
!
@
%
^
&
\*
\(
\)
-
+
\=
\{
\}
\[
\]
:
;
"
'
\|
\\
<
>
,
.
?
/
<=
>=
==
!=
[Symbol_end]


[type_begin]
	/*
		新的token类型定义
		最好用大写
	*/
	NUM
	FLOATNUM
	CONSTSTRING
	ID		//变量、函数等名称
	NOTATION
	BLANK
[type_end]

[Regular_begin]
	digit	= [0-9]
	letter	= [a-zA-Z]
	all	= [#-~]|\s|!		// \s 为空格
	name_c	= [a-zA-Z]|_
[Regular_end]

[lex_begin]
{digit}{digit}* | -{digit}{digit}*
NUM

{digit}*.{digit}{digit}* | -{digit}*.{digit}{digit}*
FLOATNUM

" {all}* "
CONSTSTRING

{name_c}  ({name_c} | {digit})*
ID

\/ \/  {all}* (\n|\n\r)
NOTATION

(\n | \s | \t | \r)(\n | \s | \t | \r)*		//回车 或者 空格 或者TAB
BLANK
[lex_end]

[nonterminal_begin]
START*
START
declare
declare_each
type
name
factor
term
term2
ag_exp
ag_2
bool_exp
bl2
exp
exp_stat
compound_stat
read_stat
write_stat
idorstring
addop
mulop
boolop
statement_list
statement

[nonterminal_end]

[grammer_begin]
START* = [START]
START = [declare][statement]
START =[statement]
declare = [declare_each][declare]
declare = [declare_each]
declare_each = [type] [name] 0;0
type = <int>
%(
	Set_type_int
)%
type = <float>
%(
	Set_type_float
)%

type = <string>
%(
	Set_type_string
)%

name = (id) 0,0 [name]
%(
	add_id
)%

name = (id)
%(
	add_id
)%

factor = (num)
%(
	add_id
)%

factor = (id):token_id:
%(
y setv %token_id		//%表示token_id的原本的字符
y sett &int			//&表示类型
)%

factor = 0(0 [exp]:exp_set: 0)0
%(
y setv exp_set
y sett exp_set
)%

term = [factor] [term2]
%(
	--/
 if (term2.type == -1)
{
	term.val = factor.val;
	term .type = factor.type;
}
else
{
	factor.val = factor.val term2.op term2.val
	if(factor.type == float || term2.type == float)
		term.type = float;
	else term.type = int;
}
	/--
)%

term2 = [mulop][term]
%(
term2.op = mulop.op;
term2.val = term.val;
term2.type = term.type;
)%

term2 = &
%(
	term.type = -1;
)%

ag_exp = [term] [ag_2]

ag_2 = [addop] [ag_exp]


ag_2 = &


bool_exp = [ag_exp][bl2]
%(
	
)%
bl2 = [boolop] [bool_exp]
%(
	bl2.op = boolop.op;
	bl2.type = bool_exp.type;
	bl2.val = bool_exp.val;
)%
bl2 = &
%(
	bl2.type = -1;
)%

exp = (id) 0=0 [bool_exp]
%(
id.val = bool_exp.val;
id.type = bool_exp.type;
exp.type = id.type;
exp.val = id.val;
)%
exp = [bool_exp]
%(
	exp.val = bool_exp.val;
	exp.type = bool_exp.type;
)%
exp_stat = [exp] 0;0
exp_stat = 0;0
compound_stat = 0{0 [statement_list] 0}0
read_stat = <read> 0(0 (id) 0)0
write_stat = <write> 0(0 [idorstring] 0)0
idorstring = (id)
idorstring = (CONSTSTRING)
addop = 0+0
addop = 0-0
mulop = 0*0
mulop = 0/0
boolop = 0<0
boolop = 0>0
boolop = 0=0
boolop = 0<=0
boolop = 0>=0
boolop = 0!=0
boolop = 0==0

statement_list = [statement]
statement_list = [statement] [statement_list]
//statement = [if_stat]
//statement = [for_stat]
//statement = [while_stat]
statement = [read_stat]
statement = [write_stat]
statement = [exp_stat]
statement = [compound_stat]

//<if> 0(0 [exp] 0)0 [statement] [else_part]
//else_part = <else> [statement] 
//else_part = &

//<while> 0(0 [exp] 0)0 [statement]

//<for> 0(0 [exp] 0;0 [exp] 0;0 [exp] 0)0	[statement]


[grammer_end]

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -