Syntactic predicate

A syntactic predicate specifies the syntactic validity of applying a production in a formal grammar and is analogous to a semantic predicate that specifies the semantic validity of applying a production. It is a simple and effective means of dramatically improving the recognition strength of an LL parser by providing arbitrary lookahead. In their original implementation, syntactic predicates had the form “( α )?” and could only appear on the left edge of a production. The required syntactic condition α could be any valid context-free grammar fragment.

More formally, a syntactic predicate is a form of production intersection, used in parser specifications or in formal grammars. In this sense, the term predicate has the meaning of a mathematical indicator function. If p1 and p2, are production rules, the language generated by both p1 and p2 is their set intersection.

As typically defined or implemented, syntactic predicates implicitly order the productions so that predicated productions specified earlier have higher precedence than predicated productions specified later within the same decision. This conveys an ability to disambiguate ambiguous productions because the programmer can simply specify which production should match.

Parsing expression grammars (PEGs), invented by Bryan Ford, extend these simple predicates by allowing "not predicates" and permitting a predicate to appear anywhere within a production. Moreover, Ford invented packrat parsing to handle these grammars in linear time by employing memoization, at the cost of heap space.

It is possible to support linear-time parsing of predicates as general as those allowed by PEGs, but reduce the memory cost associated with memoization by avoiding backtracking where some more efficient implementation of lookahead suffices. This approach is implemented by ANTLR version 3, which uses Deterministic finite automata for lookahead; this may require testing a predicate in order to choose between transitions of the DFA (called "pred-LL(*)" parsing).[1]

Overview

Terminology

The term syntactic predicate was coined by Parr & Quong and differentiates this form of predicate from semantic predicates (also discussed).[2]

Syntactic predicates have been called multi-step matching, parse constraints, and simply predicates in various literature. (See References section below.) This article uses the term syntactic predicate throughout for consistency and to distinguish them from semantic predicates.

Formal closure properties

Bar-Hillel et al.[3] show that the intersection of two regular languages is also a regular language, which is to say that the regular languages are closed under intersection.

The intersection of a regular language and a context-free language is also closed, and it has been known at least since Hartmanis[4] that the intersection of two context-free languages is not necessarily a context-free language (and is thus not closed). This can be demonstrated easily using the canonical Type 1 language, :

Let  (Type 2)
Let  (Type 2)
Let 

Given the strings abcc, aabbc, and aaabbbccc, it is clear that the only string that belongs to both L1 and L2 (that is, the only one that produces a non-empty intersection) is aaabbbccc.

Other considerations

In most formalisms that use syntactic predicates, the syntax of the predicate is noncommutative, which is to say that the operation of predication is ordered. For instance, using the above example, consider the following pseudo-grammar, where X ::= Y PRED Z is understood to mean: "Y produces X if and only if Y also satisfies predicate Z":

S    ::= a X
X    ::= Y PRED Z
Y    ::= a+ BNCN
Z    ::= ANBN c+
BNCN ::= b [BNCN] c
ANBN ::= a [ANBN] b

Given the string aaaabbbccc, in the case where Y must be satisfied first (and assuming a greedy implementation), S will generate aX and X in turn will generate aaabbbccc, thereby generating aaaabbbccc. In the case where Z must be satisfied first, ANBN will fail to generate aaaabbb, and thus aaaabbbccc is not generated by the grammar. Moreover, if either Y or Z (or both) specify any action to be taken upon reduction (as would be the case in many parsers), the order that these productions match determines the order in which those side-effects occur. Formalisms that vary over time (such as adaptive grammars) may rely on these side effects.

Examples of use

ANTLR

Parr & Quong[5] give this example of a syntactic predicate:

 stat: (declaration)? declaration
     | expression
     ;

which is intended to satisfy the following informally stated[6] constraints of C++:

  1. If it looks like a declaration, it is; otherwise
  2. if it looks like an expression, it is; otherwise
  3. it is a syntax error.

In the first production of rule stat, the syntactic predicate (declaration)? indicates that declaration is the syntactic context that must be present for the rest of that production to succeed. We can interpret the use of (declaration)? as "I am not sure if declaration will match; let me try it out and, if it does not match, I shall try the next alternative." Thus, when encountering a valid declaration, the rule declaration will be recognized twice—once as syntactic predicate and once during the actual parse to execute semantic actions.

Of note in the above example is the fact that any code triggered by the acceptance of the declaration production will only occur if the predicate is satisfied.

Canonical examples

The language can be represented in various grammars and formalisms as follows:

Parsing Expression Grammars
 S  &(A !b) a+ B !c
 A  a A? b
 B  b B? c
§-Calculus

Using a bound predicate:

S → {A}B
A → X 'c+'
X → 'a' [X] 'b'
B → 'a+' Y
Y → 'b' [Y] 'c'

Using two free predicates:

A → <'a+'>a <'b+'>b Ψ(a b)X <'c+'>c Ψ(b c)Y
X → 'a' [X] 'b'
Y → 'b' [Y] 'c'
Conjunctive Grammars

(Note: the following example actually generates , but is included here because it is the example given by the inventor of conjunctive grammars.[7]):

S → AB&DC
A → aA | ε
B → bBc | ε
C → cC | ε
D → aDb | ε
Perl 6 rules
 rule S { <before <A> <!before b>> a+ <B> <!before c> }
 rule A { a <A>? b }
 rule B { b <B>? c }

Parsers/formalisms using some form of syntactic predicate

Although by no means an exhaustive list, the following parsers and grammar formalisms employ syntactic predicates:

ANTLR (Parr & Quong)
As originally implemented,[2] syntactic predicates sit on the leftmost edge of a production such that the production to the right of the predicate is attempted if and only if the syntactic predicate first accepts the next portion of the input stream. Although ordered, the predicates are checked first, with parsing of a clause continuing if and only if the predicate is satisfied, and semantic actions only occurring in non-predicates.[5]
Augmented Pattern Matcher (Balmas)
Balmas refers to syntactic predicates as "multi-step matching" in her paper on APM.[8] As an APM parser parses, it can bind substrings to a variable, and later check this variable against other rules, continuing to parse if and only if that substring is acceptable to further rules.
Parsing expression grammars (Ford)
Ford's PEGs have syntactic predicates expressed as the and-predicate and the not-predicate.[9]
§-Calculus (Jackson)
In the §-Calculus, syntactic predicates are originally called simply predicates, but are later divided into bound and free forms, each with different input properties.[10]
Raku rules
Raku introduces a generalized tool for describing a grammar called rules, which are an extension of Perl 5's regular expression syntax.[11] Predicates are introduced via a lookahead mechanism called before, either with "<before ...>" or "<!before ...>" (that is: "not before"). Perl 5 also has such lookahead, but it can only encapsulate Perl 5's more limited regexp features.
ProGrammar (NorKen Technologies)
ProGrammar's GDL (Grammar Definition Language) makes use of syntactic predicates in a form called parse constraints.[12] ATTENTION NEEDED: This link is no longer valid!
Conjunctive and Boolean Grammars (Okhotin)
Conjunctive grammars, first introduced by Okhotin,[13] introduce the explicit notion of conjunction-as-predication. Later treatment of conjunctive and boolean grammars[14] is the most thorough treatment of this formalism to date.

References

  1. ^ Parr, Terence (2007). The Definitive ANTLR Reference: Building Domain-Specific Languages. The Pragmatic Programmers. p. 328. ISBN 978-3-540-63293-1.
  2. ^ a b Parr, Terence J.; Quong, Russell (October 1993). "Adding semantic and syntactic predicates to LL(k): Pred-LL(k)". Adding Semantic and Syntactic Predicates to LL(k) parsing: pred-LL(k). Lecture Notes in Computer Science. Vol. 786. Army High Performance Computing Research Center Preprint No. 93-096. pp. 263–277. CiteSeerX 10.1.1.26.427. doi:10.1007/3-540-57877-3_18. ISBN 978-3-540-57877-2. Retrieved 26 August 2023.
  3. ^ Bar-Hillel, Y.; Perles, M.; Shamir, E. (1961). "On formal properties of simple phrase structure grammars". Zeitschrift für Phonetik, Sprachwissenschaft und Kommunikationsforschung. 14 (2): 143–172..
  4. ^ Hartmanis, Juris (1967). "Context-Free Languages and Turing Machine Computations". Proceedings of Symposia in Applied Mathematics. Mathematical Aspects of Computer Science. 19. AMS: 42–51. doi:10.1090/psapm/019/0235938. ISBN 9780821867280.
  5. ^ a b Parr, Terence; Quong, Russell (July 1995). "ANTLR: A Predicated-LL(k) Parser Generator" (PDF). Software: Practice and Experience. 25 (7): 789–810. doi:10.1002/spe.4380250705. S2CID 13453016.
  6. ^ Stroustrup, Bjarne; Ellis, Margaret A. (1990). The Annotated C++ Reference Manual. Addison-Wesley. ISBN 9780201514599.
  7. ^ Okhotin, Alexander (2001). "Conjunctive grammars" (PDF). Journal of Automata, Languages and Combinatorics. 6 (4): 519–535. doi:10.25596/jalc-2001-519. S2CID 18009960. Archived from the original (PDF) on 26 June 2019.
  8. ^ Balmas, Françoise (20–23 September 1994). "An Augmented Pattern Matcher as a Tool to Synthesize Conceptual Descriptions of Programs". Proceedings KBSE '94. Ninth Knowledge-Based Software Engineering Conference. Proceedings of the Ninth Knowledged-Based Software Engineering Conference. Monterey, California. pp. 150–157. doi:10.1109/KBSE.1994.342667. ISBN 0-8186-6380-4.
  9. ^ Ford, Bryan (September 2002). Packrat Parsing: a Practical Linear-Time Algorithm with Backtracking (Master’s thesis). Massachusetts Institute of Technology.
  10. ^ Jackson, Quinn Tyler (March 2006). Adapting to Babel: Adaptivity & Context-Sensitivity in Parsing. Plymouth, Massachusetts: Ibis Publishing. CiteSeerX 10.1.1.403.8977.
  11. ^ Wall, Larry (2002–2006). "Synopsis 5: Regexes and Rules".
  12. ^ "Grammar Definition Language". NorKen Technologies.
  13. ^ Okhotin, Alexander (2000). "On Augmenting the Formalism of Context-Free Grammars with an Intersection Operation". Proceedings of the Fourth International Conference "Discrete Models in the Theory of Control Systems" (in Russian): 106–109.
  14. ^ Okhotin, Alexander (August 2004). Boolean Grammars: Expressive Power and Algorithms (Doctoral thesis). Kingston, Ontario: School of Computing, Queens University.

Read other articles:

Alarm bayi monitor Alarm bayi atau monitor bayi adalah sistem pemancar dan penerima sederhana (satu arah) yang digunakan untuk mendengarkan suara yang ditimbulkan oleh bayi dari jarak jauh. Pemancar ini, yang dilengkapi dengan mikropon, yang ditempatkan dekat anak dan penerima yang dilengkapi dengan pengeras suara, diteruskan oleh, atau dekat dengan orang yang merawat mereka tiap waktu. Beberapa alarm bayi bersifat rangkap dua (dua arah), menggunakan transceiver yang memungkinan orang yang me...

 

 

Pusat kota Shin-Ōkubo pada siang hari. Shin-Ōkubo (新大久保code: ja is deprecated ) adalah sebuah wilayah di Shinjuku, Tokyo yang dikenal karena komunitas Korea ekstensifnya.[1] Wilayah tersebut dibangun di sekitaran Stasiun Shin-Ōkubo dan dapat diakss pada Jalur Yamanote. Shin-Ōkubo adalah tempat tinggal dari penduduk Korea di Jepang serta imigran Korea,[2] dan populer karena bom budaya pop Korea Hallyu. Pada saat ini, orang Nepal juga bermukim di wilayah tersebut dan...

 

 

Questa voce sull'argomento anatomia vegetale è solo un abbozzo. Contribuisci a migliorarla secondo le convenzioni di Wikipedia. Ortica: il perigonio è verdastro e sormontato da pannocchie pendenti. Iris: il perigonio è costituito dai tepali violetti. Il perigonio è l'involucro esterno, che racchiude la parte sessuale del fiore, nel caso in cui questo involucro sia formato da pezzi tutti eguali[1] che a loro volta vengono denominati tepali.[2] Indice 1 Etimologia 2 Ca...

Artikel ini tidak memiliki referensi atau sumber tepercaya sehingga isinya tidak bisa dipastikan. Tolong bantu perbaiki artikel ini dengan menambahkan referensi yang layak. Tulisan tanpa sumber dapat dipertanyakan dan dihapus sewaktu-waktu.Cari sumber: Ogoh-ogoh – berita · surat kabar · buku · cendekiawan · JSTOR Ubud, 2008 Ogoh-ogoh adalah karya seni patung dalam kebudayaan Bali yang menggambarkan kepribadian Bhuta Kala. Dalam ajaran Hindu Dharma, Bhu...

 

 

Computer display feature Resolution independence is where elements on a computer screen are rendered at sizes independent from the pixel grid, resulting in a graphical user interface that is displayed at a consistent physical size, regardless of the resolution of the screen.[1] Concept As early as 1978, the typesetting system TeX due to Donald Knuth introduced resolution independence into the world of computers. The intended view can be rendered beyond the atomic resolution without an...

 

 

2019 game show Killer CampGenreWhodunit game showCreated by James Donkin Ben Wilson Presented byBobby MairCountry of originUnited KingdomOriginal languageEnglishNo. of seasons2No. of episodes13ProductionExecutive producers Steph Harris Ben Wilson Karen Smith Production locationLithuaniaRunning time45 minutesProduction companyTuesday's Child TelevisionOriginal releaseNetwork ITV2 (season 1) The CW (season 2) Release27 October 2019 (2019-10-27) –8 December 2021 (2021-12-08) Kil...

Baronetcy in the Baronetage of the United Kingdom There have been six baronetcies created for persons with the surname Newton, three in the Baronetage of England, one in the Baronetage of Nova Scotia and two in the Baronetage of the United Kingdom. The Newton Baronetcy, of Charlton in the County of Kent, was created in the Baronetage of England on 2 April 1620 for Adam Newton. The name of the baronetcy was changed to Puckering.[1] The Newton Baronetcy, of Barrs Court in the County of ...

 

 

Gaya atau nada penulisan artikel ini tidak mengikuti gaya dan nada penulisan ensiklopedis yang diberlakukan di Wikipedia. Bantulah memperbaikinya berdasarkan panduan penulisan artikel. (Pelajari cara dan kapan saatnya untuk menghapus pesan templat ini) Danau Aneuk LaotDanau Aneuk Laot dan Teluk Sabang pada tahun 1929KoordinatKoordinat: 5°52′29″N 95°19′23″E / 5.87472°N 95.32306°E / 5.87472; 95.32306Jenis perairanDanauWilayah tangkapan air- km2 (- mi²)T...

 

 

此條目可参照英語維基百科相應條目来扩充。 (2021年5月6日)若您熟悉来源语言和主题,请协助参考外语维基百科扩充条目。请勿直接提交机械翻译,也不要翻译不可靠、低品质内容。依版权协议,译文需在编辑摘要注明来源,或于讨论页顶部标记{{Translated page}}标签。 约翰斯顿环礁Kalama Atoll 美國本土外小島嶼 Johnston Atoll 旗幟颂歌:《星條旗》The Star-Spangled Banner約翰斯頓環礁�...

هنودمعلومات عامةنسبة التسمية الهند التعداد الكليالتعداد قرابة 1.21 مليار[1][2]تعداد الهند عام 2011ق. 1.32 مليار[3]تقديرات عام 2017ق. 30.8 مليون[4]مناطق الوجود المميزةبلد الأصل الهند البلد الهند  الهند نيبال 4,000,000[5] الولايات المتحدة 3,982,398[6] الإمار...

 

 

This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Acacia film – news · newspapers · books · scholar · JSTOR (October 2020) (Learn how and when to remove this message) 2003 South Korean filmAcaciaDirected byPark Ki-hyeongWritten bySeong Gi-youngPark Ki-hyungProduced byKang Sung-kyuPark Ki-hyungYu Yeong-shi...

 

 

زوندردينست الإنشاء 1940  تعديل مصدري - تعديل   Sonderdienst ((بالألمانية: Special Services)‏ كانت التشكيلات شبه العسكرية الألمانية النازية التي تم إنشاؤها في الحكومة العامة الفاصلة أثناء احتلال بولندا في الحرب العالمية الثانية. كانت تستند إلى تشكيلات الشوتزشتافل المماثلة تسمى <a hre...

Uganda communications There are a number of systems of communication in Uganda, including a system of telephony, radio and television broadcasts, internet, mail, and several newspapers. The use of phones and the internet in Uganda has rapidly increased in the last few years. History East Africa and Uganda Protectorates 1912 five rupees stamp 1900 to 1970 See also: Postage stamps and postal history of Kenya, Uganda, Tanganyika and Postage stamps and postal history of East Africa and Uganda Pro...

 

 

American homebuilt aircraft This article needs additional citations for verification. Please help improve this article by adding citations to reliable sources. Unsourced material may be challenged and removed.Find sources: Bowers Fly Baby – news · newspapers · books · scholar · JSTOR (September 2013) (Learn how and when to remove this message) Bowers Fly Baby Role Sport and personal aircraftType of aircraft Manufacturer homebuilt aircraft Designer Pete...

 

 

У этого термина существуют и другие значения, см. Дуровка. ДеревняБольшегорье 52°43′36″ с. ш. 36°46′54″ в. д.HGЯO Страна  Россия Субъект Федерации Орловская область Муниципальный район Покровский Сельское поселение Журавецкое История и география Прежние названи�...

رضوان بن تتش معلومات شخصية تاريخ الميلاد القرن 11 تاريخ الوفاة 10 ديسمبر 1113 الجنسية  الدولة العباسية الدولة السلجوقية الديانة الإسلام الأولاد ألب أرسلان الأخرس[1]ملكشاه  [لغات أخرى]‏[1]سلطان شاه بن رضوان[1]  الأب تتش بن ألب أرسلان عائلة السلالة السلجو...

 

 

Disambiguazione – Se stai cercando l'omonimo ex slittinista austriaco, vedi Markus Schmidt (slittinista). Questa voce sull'argomento calciatori austriaci è solo un abbozzo. Contribuisci a migliorarla secondo le convenzioni di Wikipedia. Segui i suggerimenti del progetto di riferimento. Markus SchmidtNazionalità Austria Altezza190 cm Peso81 kg Calcio RuoloCentrocampista Termine carriera1º marzo 2012 CarrieraGiovanili 1994-1998SV Rohrbach Squadre di club1 1998-2012 Mattersb...

 

 

Орден Франсиско МирандыOrden Francisco de Miranda Страна  Венесуэла Тип орден Статус вручается Статистика Дата учреждения 1943 год Очерёдность Старшая награда Орден Освободителя Младшая награда Орден Андреса Бельо  Медиафайлы на Викискладе Орден Франсиско Миранды (исп. Or...

« Hétéro » redirige ici. Pour l’article homophone, voir Éthéraux. L'hétérosexualité est une attirance romantique, une attirance sexuelle ou un comportement sexuel entre des personnes de sexes ou de genres opposés. En tant qu'orientation sexuelle, l'hétérosexualité est un « modèle durable d'attirances émotionnelles, romantiques et/ou sexuelles » pour les personnes du sexe opposé ; elle fait également « référence au sentiment d'identité d'...

 

 

Pemilik HidupkuAlbum studio karya NikitaDirilis21 Mei 2014 di IndonesiaDirekam—GenreGospelLabelNatashia Nikita RecordsProduserLily Tanjaya, Natashia Nikita & Heri SantosaKronologi Nikita Pelangi Kasih(2012)Pelangi KasihString Module Error: Match not found Pemilik Hidupku(2014) Pemilik Hidupku adalah album rohani ketigabelas Natashia Nikita dan dirilis pada tahun 2014. Album ini merupakan album pertama Nikita yang dirilis oleh Natashia Nikita Records. Pemilik Hidupku adalah album per...