المحلل المحلل المحلل: كيفية إنهاء التكرار على الكلمة الرئيسية

StackOverflow https://stackoverflow.com/questions/1528333

  •  20-09-2019
  •  | 
  •  

سؤال

أحاول معرفة كيفية إنهاء تكرار الكلمات باستخدام الكلمة الأساسية. مثال:

class CAQueryLanguage extends JavaTokenParsers {
    def expression = ("START" ~ words ~ "END") ^^ { x =>
        println("expression: " + x);
        x
    }
    def words = rep(word) ^^ { x =>
        println("words: " + x)
        x
    }
    def word = """\w+""".r
}

عندما تنفذ

val caql = new CAQueryLanguage
caql.parseAll(caql.expression, "START one two END")

يطبع words: List(one, two, END), ، مشيرا إلى words وقد استهلك المحلل END الكلمة الرئيسية في الإدخال الخاص بي، وترك محلل التعبير غير قادر على المباراة. أود END لن تكون مطابقة words, ، والتي سوف تسمح expression لتحليل بنجاح.

هل كانت مفيدة؟

المحلول

هل هذا ما كنت تبحث عنه؟

import scala.util.parsing.combinator.syntactical._

object CAQuery extends StandardTokenParsers {
    lexical.reserved += ("START", "END")
    lexical.delimiters += (" ")

    def query:Parser[Any]= "START" ~> rep1(ident) <~ "END"

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

println(CAQuery.parse("""START a END"""))       //List(a)
println(CAQuery.parse("""START a b c END"""))   //List(a, b, c)

إذا كنت ترغب في مزيد من التفاصيل، يمكنك التحقق هذه المدونة post.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top