开发者

How can I tell if a function call is an expression or a statement while writing a parser for a Lua-like language

I'm trying to write a parser for a Lua-like language, using lex and yacc. It is a language without a f开发者_Go百科orced statement terminator(a semicolon), and this feature made me not able to tell if a function call is a statement or a an expression.

For example, the following function:

function foo()  
  return { x = 5 }  
end  

will return a table. Here are some usages:

foo()  -- this is a statement
t = foo()  -- foo is an expression
a = foo().x  -- foo() is a prefix-expression
print(foo())  -- foo() is an expression

I cannot write a no-conflict yacc code, because a simple function call could be an expression, a prefix-expression, or a statement.

How can I implement this feature? Is introducing forced statement terminators the only way?

Thank you very much.


I had a similar task to solve when implementing a T-SQL parser. I ended up including the ; as if it was mandatory in the grammar, and on syntax errors I would insert a virtual ; token to terminate the current statement and then have the parser retry the reduction.

For my use case this does work very well, maybe it could also work for yours.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜