The Artima Developer Community
Sponsored Link

Programming in Scala Forum
Combinator Parsing Problem

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Partha Biswas

Posts: 3
Nickname: parthab
Registered: Jul, 2007

Combinator Parsing Problem Posted: Apr 15, 2008 2:51 AM
Reply to this message Reply
Advertisement
Dear Everybody,
I was trying to parse various strings. One rule tells that the String must contain a smaller string like HGSDC:MSISDN=919810001671,SUD=OICK7-0; which can be described by a Java regular expression "HGS?[CEI]*".

I have written a code like following :


import scala.util.parsing.combinator._
import scala.util.parsing.combinator.syntactical._
import java.util.regex.{Pattern, Matcher}

object Test extends StandardTokenParsers with ImplicitConversions with Application {

lexical.delimiters ++= List(",", "?", "[", "]", ":", "=", ";", "-")

def expr: Parser[Any] = opt(startStop) ~> rep(member) <~ opt(startStop)

def startStop = "[" | "]"

def member: Parser[Any] = opt(numericLit) ~ ident ~ rep(opt(",") ~ opt(":") ~ ident ~ opt("=") ~ opt("-") ~ opt(numericLit) ~ opt(";") <~ opt("?"))

val lSTr = List("[ Hello,Hi How Do U D oo ?]",
"60 HGSSE:MSISDN=919810637917,SS=BAIC,BSG=TS10;",
"53 HGSDC:MSISDN=919810001671,SUD=OICK7-0;",
" XGSDW")

val pat = Pattern.compile("HGS?[CEI]*")
for (str <- lSTr) {
if(pat.matcher(str).find()) {
val tokens = new lexical.Scanner(str)
println("input: " + str)
val result = (phrase(expr)(tokens))
println(result)
}
}
}


The outpput lokks like :

input: 60 HGSSE:MSISDN=919810637917,SS=BAIC,BSG=TS10;
[1.55] parsed: List(((Some(60)~HGSSE)~List(((((((None~Some(:))~MSISDN)~Some(=))~None)~Some(919 810637917))~None), ((((((Some(,)~None)~SS)~Some(=))~None)~None)~None), ((((((None~None)~BAIC)~None)~None)~None)~None), ((((((Some(,)~None)~BSG)~Some(=))~None)~None)~None), ((((((None~None)~TS10)~None)~None)~None)~Some(;)))))
input: 53 HGSDC:MSISDN=919810001671,SUD=OICK7-0;
[1.49] parsed: List(((Some(53)~HGSDC)~List(((((((None~Some(:))~MSISDN)~Some(=))~None)~Some(919 810001671))~None), ((((((Some(,)~None)~SUD)~Some(=))~None)~None)~None), ((((((None~None)~OICK7)~None)~Some(-))~Some(0))~Some(;)))))



Requirements :
The String may or may not start with numeric literal. I want the output as a Array[String] or List[String] or a tuple. Also the elements should be as it is in the input, white spaces being the delimiter, like :
"60","HGSSE:MSISDN=919810637917,SS=BAIC,BSG=TS10;"


Firstly, how to use combinator.ImplicitConversions with ParseResult ( created by phrase(expr) ) ? Or, is there any other way ?

Also I was not comfortable using ":", "=", ";", "-" as lexical.delimiters, when they are really not delimiters in my case. I want to write 'member' cleanly. What I have built here ( the 'member' ) is on a trial and error basis. Could you please rectify that ? How could I use a Chainl1 , if required. Please help at the earliest. I am a neophyte in Scala, and I have started coding in Scala. I have requirement to parse a little different kind of String also, about which I will let you know in the next letter.

Thanks in anticipation and with earnest regards ~
Partha Biswas

Topic: Combinator Parsing Problem Revisited Previous Topic   Next Topic Topic: Need clarification on closure example page 198

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use