ebnf - Scala Parser Combinators tricks for recursive bnf? -


I am trying to match this syntax:

  pgm :: = Exprs exprs :: = Expr [; Exprs] expr :: = id | | Xp. [0- 9] +  

My Scale Packet Parser Composer looks like this:

Import Scal.Till.SparkingConitor. Expand StandardTokenParsers with the PacketPervers Import Scale.til.parsing.combinator.syntactical._Object DotTrator PacketParsers {lexical.delimiters ++ = list (".", ";") Def pgm = repsep (expr, ";") Def expr: Parser [Any] = Identity | Expr ~ "." ~ Num def num = numericLit def parse (Input: string) = Phrase (pgm) (New PackratReader (new lexical.Scanner (Input) match {case success (result, _) => println ("success!") ; Some (result) case n @ _ => println (n); println ("bla"); none} def main (args: array [string]) {val prg = "x.1.2.3;" + "Y.4.1.1;" + "Z;" + "N.1.10.30" Pars (PRG);}}

But this does not work either " Greedy "and tells me:

  [1.2] Failure: End of required input x.1.2.3; y.4.1.1; z; n.1.10.30 < / Code> 

or if I | Change to . | , I get a stack overflow:

  exception thread "main" in java.lang.StackOverflowError at java.lang.Character.isLetter (Unknown source) at java.lang.Character.isLetter (unknown source) scala.util.parsing.combinator.lexical.Lexical $$ anonfun $ letter $ 1.apply (lexical.scala: 32) scala.util.parsing. Combinator.lexical.Lexical $$ anonfun $ letter $ 1.apply (lexical. Scala: 32) ... I understand why I found errors; What can I do to parse a syntax like the above? It does not seem to be hidden for me  

EDIT: Based on the reference paper I came to know that my program does not really use the new packet parser.

Ie Parser [Any] to PackratParser [Any] and Lazy Val instead of def

I mentioned above:

  import slailey. Parsing.combinator.PackratParsers Import scala.util.parsing.combinator.syntactical._StandardTokenParsers Extended with Object DotTrack PacketParsers {Lexical. Delimiter ++ = list (".", ";") Sluggish valve PGM: packetratter parser [any] = repres (xp, ";") sluggish val xx: packetpeter [any] = xp ~ "." ~ Number | Ident lazy value num = numericLit def parse (Input: string) = Phrase (pgm) (New PackratReader) match {case success (results, _) => println ("success!") ; Some (result) case n @ _ => println (n); println ("bla"); none} def main (args: array [string]) {val prg = "x.1.2.3;" + "Y.4.1.1;" + "Z;" + "N.1.10.30" parse (PRG);}}  

The problem (at least partially) is that you are not actually using the documentation for the symptoms of the packet parser scala See Runt, which says

Using packets is similar to using parsers:

  • Any class / attribute that extends to parser ( Directly or sub-class) can be mixed in the pirate parser Example: Object MyGrammar enhances standardTokenParsers with PackratParsers
  • Production of every grammar declared as DM without formal criteria becomes lazy value Is, and its type parser [A Frame] from the Petrtat into parser [Elem]. Therefore, for example, def production: parser [int] = {...} sluggish wall output: PacketPerson [int] = {...}
  • Important: using packet patents All or nothing is Fesla. They can be mixed free with regular parsers in a single grammar.

I do not know about correcting the parser combinations of Scala 2.8, but it is with the following modifications, I call it as a semicolon Was able to get to cross, which is an improvement over what you have accomplished.

Dottraighter increases the standard token parser with packeters {lexical.delimiters ++ = list (".", ";") Slow Val PGM: Packetapters [any] = Repres (XPR) , ";") Lazy Val Exp: Pacquer Parser [Any] = ICC |||| (XP ~ "." ~ Numerical Lit) DRF Pars (Input: String) = Phrase (Lex (Input) Match {Case Success (Results, _) = & gt; Println ("success!"); Some (results) case n @ _ = & gt; Println (n); Println ("bla"); None} DEF LEX (input: string) = new packetreader (new lexical scanner (input))

Comments

Popular posts from this blog

paypal - How to know the URL referrer in PHP? -

oauth - Facebook OAuth2 Logout does not remove fb_ cookie -

wpf - Line breaks and indenting for the XAML of a saved FlowDocument? -