1

単純なブール式を解析する文法を構築しようとしています。
複数の式がある場合、問題が発生しています。'ed 式
を解析できる必要があります。1..n and/or

以下の各例は、完全な式です。

  1. (myitem.isavailable("1234") or myitem.ispresent("1234")) and myitem.isready("1234")
  2. myitem.value > 4 and myitem.value < 10
  3. myitem.value = yes or myotheritem.value = no

文法:

@start = conditionalexpression* | expressiontypes;

conditionalexpression = condition expressiontypes;

expressiontypes = expression | functionexpression;

expression = itemname dot property condition value;

functionexpression = itemname dot functionproperty;

itemname = Word;

propertytypes = property | functionproperty;

property = Word;

functionproperty =  Word '(' value ')';

value = Word | QuotedString | Number;

condition = textcondition;

dot = '.';

textcondition = 'or' | 'and' | '<' | '>' | '=';
4

1 に答える 1

1

ParseKitの開発者はこちら。

入力例に一致する ParseKit 文法は次のとおりです。

@start = expr;

expr = orExpr;

orExpr = andExpr orTerm*;
orTerm = 'or' andExpr;

  // 'and' should bind more tightly than 'or'
andExpr = relExpr andTerm*;
andTerm = 'and' relExpr;

  // relational expressions should bind more tightly than 'and'/'or'
relExpr = callExpr relTerm*;
relTerm = relOp callExpr;

  // func calls should bind more tightly than relational expressions
callExpr = primaryExpr ('(' argList ')')?;
argList = Empty | atom (',' atom)*;

primaryExpr     = atom | '(' expr ')';
atom            = obj | literal;

  // member access should bind most tightly
obj     = id member*;
member  = ('.' id);

id      = Word;
literal = Number | QuotedString | bool;
bool    = 'yes' | 'no';

relOp   = '<' | '>' | '=';

私がどのようにしてこの文法にたどり着いたかについてのアイデアを与えるために:

  1. あなたの言語は単純で構成可能な表現言語であることに気付きました。
  2. XPath 1.0 も比較的単純な表現言語であり、簡単に利用できて読みやすい文法であることを思い出しました。
  3. 私はオンラインで XPath 1.0 仕様にアクセスし、XPath の基本的な言語文法をざっと調べました。これは、言語の文法を設計するための素早い出発点を提供するのに役立ちました。XPath 式のパス式部分を無視すると、XPath は基本的な式言語の非常に優れたテンプレートになります。

上記の私の文法は、すべての入力例を正常に解析します (以下を参照)。お役に立てれば。

[foo, ., bar, (, "hello", ), or, (, bar, or, baz, >, bat, )]foo/./bar/(/"hello"/)/or/(/bar/or/baz/>/bat/)^
[myitem, ., value, >, 4, and, myitem, ., value, <, 10]myitem/./value/>/4/and/myitem/./value/</10^
[myitem, ., value, =, yes, or, myotheritem, ., value, =, no]myitem/./value/=/yes/or/myotheritem/./value/=/no^
于 2013-03-20T19:08:54.167 に答える