diff options
author | Ryo Nihei <nihei.dev@gmail.com> | 2022-05-07 11:23:43 +0900 |
---|---|---|
committer | Ryo Nihei <nihei.dev@gmail.com> | 2022-05-10 23:14:41 +0900 |
commit | 0eb44f044b6a4f051126e2e46fd8840dcb105ae9 (patch) | |
tree | 187217bcf636830a4746e4fc80ac8282d72ddd12 /driver | |
parent | Add --json option to vartan-parse command (diff) | |
download | cotia-0eb44f044b6a4f051126e2e46fd8840dcb105ae9.tar.gz cotia-0eb44f044b6a4f051126e2e46fd8840dcb105ae9.tar.xz |
Make #prec directive change only precedence and not associativity
Diffstat (limited to 'driver')
-rw-r--r-- | driver/parser_test.go | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/driver/parser_test.go b/driver/parser_test.go index 4795e29..c717205 100644 --- a/driver/parser_test.go +++ b/driver/parser_test.go @@ -496,7 +496,52 @@ foo termNode("foo", "foo"), ), }, - // The 'prec' directive can set precedence and associativity of a production. + // A production has the same precedence and associativity as the right-most terminal symbol. + { + specSrc: ` +%name test + +%left add + +expr + : expr add expr // This alternative has the same precedence and associativiry as 'add'. + | int + ; + +ws #skip + : "[\u{0009}\u{0020}]+"; +int + : "0|[1-9][0-9]*"; +add + : '+'; +`, + // This source is recognized as the following structure because the production `expr → expr add expr` has the same + // precedence and associativity as the symbol 'add'. + // + // ((1+2)+3) + // + // If the symbol doesn't have the precedence and left associativity, the production also doesn't have the precedence + // and associativity and this source will be recognized as the following structure. + // + // (1+(2+3)) + src: `1+2+3`, + ast: nonTermNode("expr", + nonTermNode("expr", + nonTermNode("expr", + termNode("int", "1"), + ), + termNode("add", "+"), + nonTermNode("expr", + termNode("int", "2"), + ), + ), + termNode("add", "+"), + nonTermNode("expr", + termNode("int", "3"), + ), + ), + }, + // The 'prec' directive can set precedence of a production. { specSrc: ` %name test @@ -527,7 +572,7 @@ div : '/'; `, // This source is recognized as the following structure because the production `expr → sub expr` - // has the `#prec mul` directive and has the same precedence and associativity of the symbol `mul`. + // has the `#prec mul` directive and has the same precedence of the symbol `mul`. // // (((-1) * 20) / 5) // |