In which situation stringLit in StandardTokenParsers doesn't work?

196 views Asked by At

I am writing a parser in which an arithmetic operation is going to be parsed. this arithmetic operation contains variables as well for instance: var1+1

The parser is as follow:

   package org.pvamu.hadoop.image;
   import org.apache.spark.serializer.{KryoSerializer, KryoRegistrator}
   import java.nio.file.Files;
   import java.io.File;
   import scala.util.parsing.combinator.lexical._
   import scala.util.parsing.combinator.token.StdTokens
   import scala.util.parsing.combinator.lexical.StdLexical
   import scala.util.matching.Regex
   import scala.util.parsing.combinator.syntactical._
   import scala.util.parsing.combinator._

   abstract class Expr {
         def rpn:String
   }
   case class BinaryOperator(lhs:Expr, op:String, rhs:Expr) extends Expr {
       def rpn:String = lhs.rpn + " " + rhs.rpn + " " + op
   }

   case class Number(v:String) extends Expr { def rpn:String = v }
   case class Variable(v:String) extends Expr { def rpn:String = v }
   case class Function(f:String, e:List[Expr]) extends Expr { def rpn:String    = {
       var s = ""
       e.foreach { x => s += x.rpn + " " }
       s += f
       return s
    }
  }


   class parseExp extends StandardTokenParsers {
       lexical.delimiters ++= List("+","-","*","/","^","(",")",",")
       def value :Parser[Expr] = numericLit ^^ { s => Number(s) }
       def variable:Parser[Expr] =  stringLit ^^ { s => Variable(s) }
       def parens:Parser[Expr] = "(" ~> expr <~ ")"
       def argument:Parser[Expr] = expr <~ (","?)
       def func:Parser[Expr] = ( stringLit ~ "(" ~ (argument+) ~ ")" ^^ { case f ~ _ ~ e ~ _ => Function(f, e) })
       def term = (value | parens | func | variable)
        def pow :Parser[Expr] = ( term ~ "^" ~ pow ^^ {case left ~ _ ~ right => BinaryOperator(left, "^", right) }|
            term)
       def factor = pow * ("*" ^^^ { (left:Expr, right:Expr) => BinaryOperator(left, "*", right) } |
                    "/" ^^^ { (left:Expr, right:Expr) => BinaryOperator(left, "/", right) } )
       def sum =  factor * ("+" ^^^ { (left:Expr, right:Expr) => BinaryOperator(left, "+", right) } |
                    "-" ^^^ { (left:Expr, right:Expr) => BinaryOperator(left, "-", right) } )
       def expr = ( sum | term )


        def parse(s:String) = {
          val tokens = new lexical.Scanner(s)
          phrase(expr)(tokens)
        }

          def runParser(exprstr: String) : String = exprstr match {
                   case null => return ""
                   case "" => return ""
                   case _ =>
                      parse(exprstr) match {
                            case Success(tree, _) =>
                            println("Tree: "+tree)
                            val v = tree.rpn
                            println("RPN: "+v)
                            return v
                    case e: NoSuccess => Console.err.println(e)
                         return e.toString
         }
       }
    }
 object Calculator extends InfixToPostfix {
      def main(args: Array[String]) {
      println("input : "+ args(0))
      println(runParser(args(0)))
    }
 }

Then as an input I send : "3+j" and it gives me an error saying:

   failure: string literal expected 
   3+j
     ^

I don't know which part is wrong?! I am going to parse a path and the actual problem is here but I realized that this program is not even able to parse a little string, I don't know what to do with it!

0

There are 0 answers