In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "is the grammar of Lua unambiguous?". In daily operation, I believe that many people have doubts about the grammar of Lua. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the question of "is the grammar of Lua unambiguous?" Next, please follow the editor to study!
First, define the following functions:
Function foo (a) print ("foo print", a) return an end function goo (a) print ("goo print", a) return an end function hoo (a) print ("hoo print", a) return an end
Take a look at this code:
Foo (goo)
(hoo) (1979)
If you try to compile and execute the above program, the interpreter will report an error such as "ambiguous syntax (function call x new statement) near'('"). Why? Perhaps the person who wrote the program originally meant that the * * line foo (goo) is a separate function call statement (statement), while the second line (hoo) (1979) is another separate function call statement (the separator between statements in Lua-semicolons are not required but optional). But don't forget that foo (goo) (hoo) is a perfectly valid form of function call (newline characters are ignored as white space during compilation), and foo (goo) (hoo) (1979) can also be a complete function call statement. In this way, the compiler will not know the true intentions of the programmer.
We can dig a little deeper into the compilation process. The formal definition of Lua syntax (converted to BNF standard form) contains the following production forms:
(1) stat-> functioncall (production of statements)
(2) prefixexp-> functioncall (production of prefix expressions)
(3) functioncall-> prefixexp args
| | prefixexp': 'Name args (production of function calls) |
It can be found that functioncall can be reduce to stat or prefixexp. (1) and (2) there is a conflict between (1) and (2), and the compiler does not know which one to use to regulate foo (goo), so an error occurs.
In fact, it is also very simple to solve this ambiguity problem. Add a semicolon after the * * line, and the compiler will compile the code into two separate statements. Or merge the two lines into a single line, and foo (goo) (hoo) (1979) is considered a complete function call (it's still ambiguous at this point, but Lua5.04 solves ambiguity by preferring prefixexp-> functioncall for specification).
In fact, there are three other situations that can also cause ambiguity:
-- prefixexp-> functioncall conflicts with-- exp-> functioncall. -- the compiler does not know whether to interpret foo (goo) as an expression (exp) or a prefix expression local v = foo (goo) (hoo) (1979)-- exp-> var conflicts with prefixexp-> var-- the variable (var) m in the second line does not know whether to be treated as an expression or a prefix expression m = foo local v = m (goo) (1979)-- prefixexp->'('exp') 'and- -exp->'('exp') 'conflict-I don't know whether to treat (t.fn) as an expression or a prefix expression t = {fn = foo} local v = (t.fn) (goo) (1979) so far The study on "is the grammar of Lua unambiguous?" is over. I hope it can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.