Re: Lua syntax ambiguity
Andrey Dobrovolsky <[email protected]>
| Newsgroups | gmane.comp.lang.lua.general |
|---|---|
| Message-ID | <CAN5FJ7dRP8bUQvmzbHsBbRU=AZJbT+q7irpKsiz3zbKLPKjJkQ@mail.gmail.com> |
Hi Martin,
I think it wouldn't be complicated for code generator to insert extra
space before and after an index value:
t={[ [[a]] ]=true}
and it would be enough, for correct parsing.
Simple modification of the rule
field ::= "[ " exp " ]" ‘=’ exp
probably is the minimal change, however I don't know whether string
literals of the length greater than 1 are allowed in EBNF. If not then
field ::= '[' ' ' exp ' ' ']' ‘=’ exp
Thanks for an interesting corner case,
-- Andrew
нд, 16 серп. 2026 р. о 18:00 Halalaluyafail3 <[email protected]> пише:
>
> The syntax is token based as far as I understand, so in t={[[[a]]]=true} it sees: punctuator {, string literal [[[a]], punctuator ], punctuator =, keyword true, and punctuator }. That does not meet the syntax so it is invalid.
>
> On Sunday, August 16, 2026 at 10:25:12 AM UTC-4 Martin Eden wrote:
>
> Ahoy list,
>
> Let's say we're writing Lua code generator. He just applies grammar rules.
>
> So let's take parts of Lua grammar and apply them.
>
> chunk ::= block
>
> block ::= {stat} [retstat]
>
> stat ::= varlist ‘=’ explist
>
> varlist ::= var {‘,’ var}
>
> var ::= Name | prefixexp ‘[’ exp ‘]’
>
> explist ::= exp {‘,’ exp}
>
> exp ::= false | true | LiteralString | prefixexp | tableconstructor
>
> prefixexp ::= var
>
> tableconstructor ::= ‘{’ [fieldlist] ‘}’
>
> fieldlist ::= field {fieldsep field} [fieldsep]
>
> field ::= ‘[’ exp ‘]’ ‘=’ exp
>
> fieldsep ::= ‘,’ | ‘;’
>
> Problem is that "LiteralString" can start with "[" (long quotes).
> And so when writing index in "field" we emit "[", then we go
> to "exp" which may be "LiteralString" and we will emit another "[".
>
> So we'll have text like "t={[[[a]]]=true}".
>
> It's generated by perfectly abiding EBNF rules and greedy parser will
> miserably fail on it.
>
> I doubt there is a way to fix it by mangling EBNF grammar.
> That's just consequence that character to open index is "[",
> same character was chosen to start "long quotes", and table
> index can be literal string.
>
> -- Martin
>
> --
> You received this message because you are subscribed to the Google Groups "lua-l" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
> To view this discussion visit https://groups.google.com/d/msgid/lua-l/1a35c810-a4be-4697-86bf-454af3892e23n%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "lua-l" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected].
To view this discussion visit https://groups.google.com/d/msgid/lua-l/CAN5FJ7dRP8bUQvmzbHsBbRU%3DAZJbT%2Bq7irpKsiz3zbKLPKjJkQ%40mail.gmail.com.