0

I am trying to use Scala Combinator-Parsing to parse and arithmetic operation in which at least one of the variables is string. I am a beginner in scala and I like this feature of it! For instance I wanna parse:
(number + 2) * 4

It has a good example of parsing numbers (float and integer with parenthesis) like:

  class Arith extends JavaTokenParsers {  
      def expr: Parser[Any] = term~rep("+"~term | "-"~term)
      def term: Parser[Any] = factor~rep("*"~factor | "/"~factor)
      def factor: Parser[Any] = floatingPointNumber | "("~expr~")"
    } 

and this one for string:

       object MyParsers extends RegexParsers {
        val ident: Parser[String] = """[a-zA-Z_]\w*""".r
       }

how can I change the first part to parse string beside numbers?

1 Answer 1

1

I think you should add it to the factor alternatives:

def factor: Parser[Any] = ident | floatingPointNumber | "("~expr~")"
Sign up to request clarification or add additional context in comments.

2 Comments

then can I extend "class Arith extends JavaTokenParsers with RegexParsers" ??? because ident extends RegexParsers and Arith class that I mentioned above extends JavaTokenParsers, to add ident I should extend the class that ident is there, like MyParsers
JavaTokenParsers already extends RegexParsers so you do not need to combine the two. Also, JavaTokenParsers already defines ident so you do not really need MyParsers at all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.