2
votes

I'm trying to write a parser for a Lua-like language, using lex and yacc. It is a language without a forced 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.

1
You could have a look at the EBNF Lua grammar: lua.org/manual/5.1/manual.html#8 to see how they implemented things.Bart Kiers
from what i see the simple function call would be part of the expressions/statements. You just have to reorder your grammar.(this makes it ugly to look at but parseable with yacc)josefx

1 Answers

1
votes

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.