Ruby代码的解析、编译与执行机制揭秘
1. Ruby代码的解析过程
1.1 实际的Ruby语法规则
以一个简单的Ruby脚本为例:
10.times do |n|
puts n
end
Ruby的解析过程遵循一系列语法规则,如下所示:
Grammar Rules
program: top_compstmt
top_compstmt: top_stmts opt_terms
top_stmts: ... | top_stmt | ...
top_stmt: stmt | ...
stmt: ... | expr
expr: ... | arg
arg: ... | primary
primary: ... | method_call brace_block | ...
这些规则与Ruby代码一一对应, program: top_compstmt 是匹配整个Ruby程序的根语法规则。随着规则的细化,会匹配到更具体的代码部分,如 method_call 规则匹配 10.times 部分:
Grammar Rules
method_call: ... | primary_value '.' operation2 | ...
Ruby Code
10.times
超级会员免费看
订阅专栏 解锁全文
1243

被折叠的 条评论
为什么被折叠?



