New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Can't parse exponentiation with multiple unary minuses #2
Comments
I took the precedence from section 2.5.6 of the Lua manual. One of the disadvantages of recursive descent parsing is that it's hard to know, from reading the code, which syntax it implements. This could be Roberto's mistake. I think we should change the precedence to match Lua's behavior, perhaps adding a comment to the code. So, yes, add the patch. One thing, though -- as above it has unary minus in there twice. I'd remove the alternative at the looser precedence level. |
Argh! Experimenting, it seems to make a difference whether it's the right arg or the left arg of the exponentiation. If worst comes to worst, it may turn out Lua's behavior does not follow a normal precedence scheme, and unary terms may have to be pulled out of the precedenced statement. |
I noticed that |
I think the solution may be to treat exponents as special -- perhaps with I think it might work if you make exponentiation a kind of postfix unary -- ::= '^' where is then another precedenced statement for the things On Thu, Jul 30, 2015 at 1:11 PM, rns notifications@github.com wrote:
|
Re the optional sign in strtod -- I think I recall, from looking at the Lua code, that it strips off leading minus signs before passing a string on to strtod() |
First cut at exponent as a separate statement: assert(2^-2 == 1/4 and -2^- -2 == - - -4) parses with this patch (needs ranks to avoid ambiguity) @@ -163,6 +163,7 @@ sub BUILD
Marpa::R2::Scanless::R -> new
({
grammar => $self -> grammar,
+ ranking_method => 'high_rule_only',
})
);
$self -> renderer
@@ -735,10 +736,10 @@ lexeme default = latm => 1 action => [name,values]
| '...'
| <tableconstructor>
| <function>
- || <exp> '^' <exp> assoc => right
- || <keyword not> <exp>
+ || <exp> '^' <exponent exp> assoc => right
+ || '-' <exp>
+ | <keyword not> <exp>
| '#' <exp>
- | '-' <exp>
|| <exp> '*' <exp>
| <exp> '/' <exp>
| <exp> '%' <exp>
@@ -749,11 +750,13 @@ lexeme default = latm => 1 action => [name,values]
| <exp> '<=' <exp>
| <exp> '>' <exp>
| <exp> '>=' <exp>
- | <exp> '==' <exp>
+ | <exp> '==' <exp> rank => 1
| <exp> '~=' <exp>
- || <exp> <keyword and> <exp>
+ || <exp> <keyword and> <exp> rank => 1
|| <exp> <keyword or> <exp>
+<exponent exp> ::=
+ <var>
+ | '(' <exp> ')'
+ | <exp> <args>
+ | <exp> ':' <Name> <args>
+ | <Number>
+ | '#' <exp>
+ | '-' <exp>
+
<prefixexp> ::= <var>
<prefixexp> ::= <functioncall> |
I'll try that approach in MarpaX-Languages-Lua-AST on lua 5.1 test suite files. Thanks! |
In the
My take at it would be like this:
Notes: I changed The |
Are the current source code files sufficient to exercise this? If not, someone will have to write 1 or more for me to add to the distro. |
@jeffreykegler -- very cool! Now, assert(2^-2 == 1/4 and -2^- -2 == - - -4) parses with this patch (against the current repo) --- a/lib/MarpaX/Languages/Lua/Parser.pm
+++ b/lib/MarpaX/Languages/Lua/Parser.pm
@@ -825,7 +825,7 @@ lexeme default = latm => 1 action => [name,values]
| '...'
| <tableconstructor>
| <function>
- || <exp> '^' <exp> assoc => right
+ || <exp> '^' <exponent> assoc => right
|| <keyword not> <exp>
| '#' <exp>
| '-' <exp>
@@ -844,6 +844,23 @@ lexeme default = latm => 1 action => [name,values]
|| <exp> <keyword and> <exp>
|| <exp> <keyword or> <exp>
+<exponent> ::=
+ <var>
+ | '(' <exp> ')'
+ || <exponent> <args>
+ || <exponent> ':' <Name> <args>
+ | <keyword nil>
+ | <keyword false>
+ | <keyword true>
+ | <Number>
+ | <String>
+ | '...'
+ | <tableconstructor>
+ | <function>
+ || <keyword not> <exponent>
+ | '#' <exponent>
+ | '-' <exponent>
+
<prefixexp> ::= <var>
<prefixexp> ::= <functioncall>
<prefixexp> ::= '(' <exp> ')' @ronsavage -- the line is from https://github.com/rns/MarpaX-Languages-Lua-AST/blob/master/t/lua5.1-tests/constructs.lua -- lua 5.1 test suite -- hope you can incorporate it into the module’s test suite, fully or partially. |
Hi rns On 31/07/15 11:34, rns wrote:
$many x $thanx; Ron Savage - savage.net.au |
If we want to show this to the Lua community as their BNF, we may want to tweak the "presentation". @rns - would it be more elegant if we broken the |
@jeffreykegler -- good point about presentation, a bit busy now, will peek at it later. |
With the above fix, lua 5.1 test suite is parsed and reproduced correctly in MarpaX-Languages-Lua-AST. Thanks a lot. @jeffreykegler -- on |
@rns: re I would note that the Lua manual does not have the distinction between right and left precedence for the '^' operator either. We should mention that to them. |
Hi On 31/07/15 19:35, rns wrote:
I think we should go with it:
Ron Savage - savage.net.au |
@jeffreykegler, @ronsavage: Well, on a second thought, the @jeffreykegler: good point about left/right precedence of exponentiation. Other differences re Lua::AST vs. Lua::Parser which come to mind: external lexing vs. events, left recursion vs. sequences, manual traversing/MarpaX::AST vs. Tree::DAG_Node, nameless nullables vs. explicit |
Fixed in just released 1.04 |
test case: https://gist.github.com/rns/a877e2942376569154c8
Update: can be made to work with the below patch (not sure if it's ok to change precedence this way, though)
The text was updated successfully, but these errors were encountered: