试验BNF

时间:2017-10-30 14:50:22

标签: lua compiler-construction

我正在尝试使用BNF表示法。我在https://www.lua.org/manual/5.3/manual.html#9看到了Lua5.3的完整语法。我想将dothen替换为{,将end替换为}

stat ::=  ‘;’ | 
         varlist ‘=’ explist | 
         functioncall | 
         label | 
         break | 
         goto Name | 
         do block end |                                                  -> { block } 
         while exp do block end |                                        -> while exp { block }
         repeat block until exp |  
         if exp then block {elseif exp then block} [else block] end |    -> if exp { block .... } 
         for Name ‘=’ exp ‘,’ exp [‘,’ exp] do block end |               -> for ........  { block }  
         for namelist in explist do block end |                          -> for ........  { block }
         function funcname funcbody | 
         local function Name funcbody | 
         local namelist [‘=’ explist] 

funcbody ::= ‘(’ [parlist] ‘)’ block end                                  ->  ...........  { block }

如何继续使用上面修改的语法来创建解释器?这可以吗?谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

Replacing then with { and end with } conflicts with table constructors:

if a then print(1) end

becomes

if a { print(1) }

Conversely,

a = {}

becomes

a = then end