- Notifications
You must be signed in to change notification settings - Fork131
added a pure functional implementation of operator precedence parsing#470
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
peteraldous wants to merge5 commits intoscala:mainChoose a base branch frompeteraldous:precedence
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Draft
Changes fromall commits
Commits
Show all changes
5 commits Select commitHold shift + click to select a range
2b69cef
added a pure functional implementation of operator precedence parsing
peteraldous056d3ac
added docs and improved names
peteraldous1c169f5
New interface for specifying precedence that doesn't allow mixed asso…
peteraldous200f745
added a few basic tests
peteraldoused91206
added parentheses to address an error from CI
peteraldousFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
100 changes: 100 additions & 0 deletionsshared/src/main/scala/scala/util/parsing/combinator/Parsers.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletionsshared/src/test/scala/scala/util/parsing/combinator/PrecedenceParserTest.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package scala.util.parsing.combinator | ||
// import scala.language.implicitConversions | ||
import java.io.StringReader | ||
import org.junit.Test | ||
import org.junit.Assert.{ assertEquals, assertTrue, fail } | ||
import scala.util.parsing.input.StreamReader | ||
class PrecedenceParsersTest { | ||
abstract class Op | ||
object Plus extends Op { | ||
override def toString = "+" | ||
} | ||
object Minus extends Op { | ||
override def toString = "-" | ||
} | ||
object Mult extends Op { | ||
override def toString = "*" | ||
} | ||
object Divide extends Op { | ||
override def toString = "/" | ||
} | ||
object Equals extends Op { | ||
override def toString = "=" | ||
} | ||
abstract class Node | ||
case class Leaf(v: Int) extends Node { | ||
override def toString = v.toString | ||
} | ||
case class Binop(lhs: Node, op: Op, rhs: Node) extends Node { | ||
override def toString = s"($lhs $op $rhs)" | ||
} | ||
object ArithmeticParser extends RegexParsers { | ||
val prec = List( | ||
(Associativity.Left, List(Mult, Divide)), | ||
(Associativity.Left, List(Plus, Minus)), | ||
(Associativity.Right, List(Equals))) | ||
def integer: Parser[Leaf] = "[0-9]+".r ^^ { (s: String) => Leaf(s.toInt) } | ||
def binop: Parser[Op] = "+" ^^^ Plus | "-" ^^^ Minus | "*" ^^^ Mult | "/" ^^^ Divide | "=" ^^^ Equals | ||
def expression = new PrecedenceParser(integer, binop, prec, Binop.apply) | ||
} | ||
def testExp(expected: Node, input: String): Unit = { | ||
ArithmeticParser.expression(StreamReader(new StringReader(input))) match { | ||
case ArithmeticParser.Success(r, next) => { | ||
assertEquals(expected, r); | ||
assertTrue(next.atEnd); | ||
} | ||
case e => { | ||
fail(s"Error parsing $input: $e"); | ||
} | ||
} | ||
} | ||
@Test | ||
def basicExpTests: Unit = { | ||
testExp(Leaf(4), "4") | ||
testExp(Binop(Leaf(1), Plus, Leaf(2)), "1 + 2") | ||
testExp(Binop(Leaf(2), Mult, Leaf(1)), "2 * 1") | ||
} | ||
@Test | ||
def associativityTests: Unit = { | ||
testExp(Binop(Binop(Leaf(1), Minus, Leaf(2)), Plus, Leaf(3)), "1 - 2 + 3") | ||
testExp(Binop(Leaf(1), Equals, Binop(Leaf(2), Equals, Leaf(3))), "1 = 2 = 3") | ||
} | ||
@Test | ||
def precedenceTests: Unit = { | ||
testExp(Binop(Binop(Leaf(0), Mult, Leaf(5)), Minus, Leaf(2)), "0 * 5 - 2") | ||
testExp(Binop(Leaf(3), Plus, Binop(Leaf(9), Divide, Leaf(11))), "3 + 9 / 11") | ||
testExp(Binop(Binop(Leaf(6), Plus, Leaf(8)), Equals, Leaf(1)), "6 + 8 = 1") | ||
testExp(Binop(Leaf(4), Equals, Binop(Leaf(5), Minus, Leaf(3))), "4 = 5 - 3") | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.