[英]grammar of a new language [closed]
我有这种语法,我无法理解如何为其创建解析器:
module = properties fields methods module#3 'end'
properties = list#0 (property add#2)*
property = 'class' 'name' class# ';'
fields = list#0 (field add#2)*
field = type list#0 id add#2 [';'/','] ! (',' id add#2)* ';' field#2
methods = list#0 (method add#2)*
method = (type id / nothing#0 id) ! '(' args ')' follow method#4
args = list#0 (arg add#2 (',' arg add#2)*)?
arg = type id ! arg#2 / nothing#0 id ! arg#2
statements = list#0 (statement add#2)*
statement = do / jump / compound / simple
follow = block / jump / compound / simple
jump = break / continue / return
compound = if / while
simple = local / assign
do = 'do' '{' statements '}' do#1
block = '{' statements '}' block#1
break = 'break' ';' break#0
continue = 'continue' ';' continue#0
return = 'return' (exp / nothing#0) ';' return#1
if = 'if' '(' exp ')' follow ('else' follow / nothing#0) if#3
while = 'while' '(' exp ')' follow while#2
local = type id ! init? local#2 ';'
init = 'assign' exp assign#2 / '.' id dot#2 '(' exps ')' call#2
assign = id 'assign' ! exp assign#2 ';'
exp = id ( '(' exps ')' call#2 / '.' id dot#2 '(' exps ')' call#2 )?
exps = list#0 (exp add#2 (',' exp add#2)*)?
type = 'name' type#
id = 'name' id#
'.' = 'DOT'
谁能让我理解这个语法。 谢谢
这个:
module = properties fields methods module#3 'end'
意思是:
a module consists of properties, followed by fields, followed by methods, followed by the word "end"
因此,为了解析模块,编译器应:
parse properties
parse fields
parse methods
match the word "end"
带“#”的项目是应创建的语法节点的类型,该数字指示应将多少先前的解析结果传递给它。
在Python语言中,代码可能看起来像这样:
def parse_module():
properties = parse_properties()
fields = parse_fields()
methods = parse_methods()
module = make_module(properties, fields, methods)
match("end")
return module
“ /”分隔替代项,“(...)*”表示任意数量的重复项,“?” 表示可选项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.