tag:blogger.com,1999:blog-7045524330253482541.post8273130233347873524..comments2024-01-04T22:19:45.990-08:00Comments on Jim McBeath: Scala Parser CombinatorsJim McBeathhttp://www.blogger.com/profile/10541190774989580614noreply@blogger.comBlogger13125tag:blogger.com,1999:blog-7045524330253482541.post-78596394051235304402016-03-11T20:15:19.553-08:002016-03-11T20:15:19.553-08:00Oriol: I can't tell without seeing more of you...Oriol: I can't tell without seeing more of your code, but I suspect the problem is that your list expression requires a type between the square brackets, so that "[]" really is an illegal input because it does not specify a type. One of the disadvantages of using these parser combinators is that the error messages are not always very helpful.<br /><br />You might want to check out my later <a href="http://jim-mcbeath.blogspot.com/2011/07/debugging-scala-parser-combinators.html" rel="nofollow">post</a> on debugging Scala parser combinators (and be sure to read the comments there).Jim McBeathhttps://www.blogger.com/profile/10541190774989580614noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-30403226294711291342016-02-26T02:06:22.189-08:002016-02-26T02:06:22.189-08:00Good Morning,
I want to use [ and ] too to parse...Good Morning, <br /><br />I want to use [ and ] too to parse, for example "[]" or "[Int]" to get EmptyList() or List(TypeInt()) but when I add "[" and "]" to the delimiters every time that I write for example "[]" in the entrance I get illegal token error, so I think that I need to add some other thing but I don't know what...<br /><br />My delimiter list is:<br /><br />lexical1.delimiters ++= List("λ", ".", ",","[","]","(",")","\\","|","/","=")<br /><br />And the rule, that I think that's correct is: <br /><br />lazy val list: P1[List] = "[" ~> type <~ "]" ^^ {case x => List(x)}<br /><br />Could you help me please?<br /><br />Thanks in advanceAnonymoushttps://www.blogger.com/profile/06633224449368799275noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-66377784307558844032015-06-18T11:50:04.709-07:002015-06-18T11:50:04.709-07:00Thanks Jim you explained great. I have created my ...Thanks Jim you explained great. I have created my own parser for identifying path but I have a problem with that would you please help me to solve it. Here is a link to the problem: http://stackoverflow.com/questions/30902235/how-to-solve-an-error-related-to-creating-parser-from-regex<br />agrinhttps://www.blogger.com/profile/06221937219118022292noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-67217921992819107782011-07-28T15:45:31.629-07:002011-07-28T15:45:31.629-07:00Dobes: In response to your question I have written...Dobes: In response to your question I have written a <a href="http://jim-mcbeath.blogspot.com/2011/07/debugging-scala-parser-combinators.html" rel="nofollow">post</a> with a couple of techniques you might be able to use.Jim McBeathhttps://www.blogger.com/profile/10541190774989580614noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-51958493762051151582011-07-27T22:03:29.180-07:002011-07-27T22:03:29.180-07:00Any tips on how to debug your grammars? My gramma...Any tips on how to debug your grammars? My grammar isn't working and I can't figure out why. Doesn't seem to be any tooling to trace parsing activity or anything nice like that.Dobeshttps://www.blogger.com/profile/09392777569321223496noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-28275493071260029442011-07-23T21:22:13.149-07:002011-07-23T21:22:13.149-07:00Dobes: The scaladoc location has changed since I p...Dobes: The scaladoc location has changed since I posted this. It should be pretty easy to find in the API documentation at scala-lang.org.<br /><br />In the post, when I added the subtraction operator I changed from using repsep to using * because there were now two possible separators (+ or -), and I needed to be able to create different parse trees based on that.<br /><br />The ^^^ operator throws away the result of the parse and replaces it with the specified value. In this case, the parsed item is the operator character (+ or -), which is discarded and replaced by the corresponding operator function (EAdd or ESub). If you use the "current" rather than "beta" documentation, you should see a bit more info for the ^^^ operator.<br /><br />Yes, you can replace "(a:Expr, b:Expr) => EAdd(a,b)" in this situation by just "EAdd" and it will still compile and work the same way, but perhaps not for the reason you might expect. If EAdd were a function, then you would have to write "EAdd _" to make that work; but because EAdd is a case class, it happens to work when supplying only the case class name. If you want something with the same type signature here, you would use "EAdd.apply _".Jim McBeathhttps://www.blogger.com/profile/10541190774989580614noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-46676847443621529112011-07-22T07:01:16.254-07:002011-07-22T07:01:16.254-07:00I think you can replace something like:
"+&q...I think you can replace something like:<br /><br />"+" ^^^ { (a:Expr, b:Expr) => EAdd(a,b) }<br /><br />With simply:<br /><br />"+" ^^^ EAdd<br /><br />... I haven't tried it yet, but I think it'll work and might be simpler. Or am I missing something?Dobeshttps://www.blogger.com/profile/09392777569321223496noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-68916621119348705552011-07-22T01:30:11.117-07:002011-07-22T01:30:11.117-07:00Could you explain more about ^^^ ? The scaladoc I...Could you explain more about ^^^ ? The scaladoc I am looking at doesn't say anything at all about it:<br /><br />http://www.scala-lang.org/api/beta/scala/util/parsing/combinator/Parsers$Parser.htmlDobeshttps://www.blogger.com/profile/09392777569321223496noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-52862585919938853252011-07-22T01:28:37.607-07:002011-07-22T01:28:37.607-07:00The scaladoc links in this article seem to be brok...The scaladoc links in this article seem to be broken now :-(<br /><br />Why did you get rid of repsep()? I shows up once and then disappears without explanation.Dobeshttps://www.blogger.com/profile/09392777569321223496noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-7747523945231222772009-05-11T11:22:00.000-07:002009-05-11T11:22:00.000-07:00Thanks! (hmm, I hoped I would get an email when yo...Thanks! (hmm, I hoped I would get an email when you responded, strange). I was mistakenly using Scala 2.6.0 (tried maven for the first time :P) and created a nice confusion for myself :)Anonymoushttps://www.blogger.com/profile/07286834543482860937noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-4936409950005968992009-04-30T20:32:00.000-07:002009-04-30T20:32:00.000-07:00Dimitris: I tried your code and it worked fine for...Dimitris: I tried your code and it worked fine for me. I added your sum method just after the value method. It compiled without error, I changed the line in your main to use sum instead of value and 2+3 instead of 2, and it gave me a proper parse containing EAdd with nested EConst values of 2.0 and 3.0. I tested this using 2.7.3.final, with the code in a file loaded with the :load command at the REPL prompt.Jim McBeathhttps://www.blogger.com/profile/10541190774989580614noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-34289606328691663962009-04-29T09:49:00.000-07:002009-04-29T09:49:00.000-07:00Hi,
The entry seems simple enough, but unfortunat...Hi,<br /><br />The entry seems simple enough, but unfortunately it doesn't work for me. <br /><br />I have this code:<br /><br />object ArithmeticParser extends <br />StdTokenParsers {<br /> type Tokens = StdLexical<br /> val lexical = new StdLexical<br /><br /> lexical.delimiters ++= List("+")<br /><br /> def value = numericLit ^^ { s => EConst(s.toDouble) }<br /><br /><br /> def main(args: Array[String]) = {<br /> Console.println(value(new lexical.Scanner("2")))<br /> }<br />}<br /><br />(and Expr, EConst, EAdd)<br /><br />This works.<br /><br />Now I try to add the "sum" definition, i.e. these lines:<br /><br /> def sum = value ~ "+" ~ value ^^ { case left ~ "+" ~ right =><br /> EAdd(left, right) }<br /><br /><br />I get three compile errors:<br /><br />ArithmeticParser.scala:14: error: not found: value ~<br /> def sum = value ~ "+" ~ value ^^ { case left ~ "+" ~ right =><br /> <br /> ^<br />ArithmeticParser.scala:15: error: not found: value left<br /> EAdd(left, right) }<br /> <br /> ^<br />ArithmeticParser.scala:15: error: not found: value right<br /> EAdd(left, right) }<br /> <br /> ^<br /><br />I have no clue why this doesn't work. Any idea?<br /><br />Thanks.Anonymoushttps://www.blogger.com/profile/07286834543482860937noreply@blogger.comtag:blogger.com,1999:blog-7045524330253482541.post-50157187103176967962009-01-27T13:07:00.000-08:002009-01-27T13:07:00.000-08:00Thanks for this Jim, this is the clearest explanat...Thanks for this Jim, this is the clearest explanation on Scala's parser combinators I have found.Channing Waltonhttps://www.blogger.com/profile/12749648550096851903noreply@blogger.com