Left recursion

In the formal language theory of computer science, left recursion is a special case of recursion where a string is recognized as part of a language by the fact that it decomposes into a string from that same language (on the left) and a suffix (on the right). For instance, can be recognized as a sum because it can be broken into , also a sum, and , a suitable suffix.

In terms of context-free grammar, a nonterminal is left-recursive if the leftmost symbol in one of its productions is itself (in the case of direct left recursion) or can be made itself by some sequence of substitutions (in the case of indirect left recursion).

Definition

A grammar is left-recursive if and only if there exists a nonterminal symbol that can derive to a sentential form with itself as the leftmost symbol.[1] Symbolically,

,

where indicates the operation of making one or more substitutions, and is any sequence of terminal and nonterminal symbols.

Direct left recursion

Direct left recursion occurs when the definition can be satisfied with only one substitution. It requires a rule of the form

where is a sequence of nonterminals and terminals . For example, the rule

is directly left-recursive. A left-to-right recursive descent parser for this rule might look like

void Expression() {
  Expression();
  match('+');
  Term();
}

and such code would fall into infinite recursion when executed.

Indirect left recursion

Indirect left recursion occurs when the definition of left recursion is satisfied via several substitutions. It entails a set of rules following the pattern

where are sequences that can each yield the empty string, while may be any sequences of terminal and nonterminal symbols at all. Note that these sequences may be empty. The derivation

then gives as leftmost in its final sentential form.

Uses

Left recursion is commonly used as an idiom for making operations left-associative: that an expression a+b-c-d+e is evaluated as (((a+b)-c)-d)+e. In this case, that evaluation order could be achieved as a matter of syntax via the three grammatical rules

These only allow parsing the a+b-c-d+e as consisting of the a+b-c-d and e, where a+b-c-d in turn consists of the a+b-c and d, while a+b-c consists of the a+b and c, etc.

Removing left recursion

Left recursion often poses problems for parsers, either because it leads them into infinite recursion (as in the case of most top-down parsers) or because they expect rules in a normal form that forbids it (as in the case of many bottom-up parsers[clarification needed]). Therefore, a grammar is often preprocessed to eliminate the left recursion.

Removing direct left recursion

The general algorithm to remove direct left recursion follows. Several improvements to this method have been made.[2] For a left-recursive nonterminal , discard any rules of the form and consider those that remain:

where:

  • each is a nonempty sequence of nonterminals and terminals, and
  • each is a sequence of nonterminals and terminals that does not start with .

Replace these with two sets of productions, one set for :

and another set for the fresh nonterminal (often called the "tail" or the "rest"):

Repeat this process until no direct left recursion remains.

As an example, consider the rule set

This could be rewritten to avoid left recursion as

Removing all left recursion

The above process can be extended to eliminate all left recursion, by first converting indirect left recursion to direct left recursion on the highest numbered nonterminal in a cycle.

Inputs A grammar: a set of nonterminals and their productions
Output A modified grammar generating the same language but without left recursion
  1. For each nonterminal :
    1. Repeat until an iteration leaves the grammar unchanged:
      1. For each rule , being a sequence of terminals and nonterminals:
        1. If begins with a nonterminal and :
          1. Let be without its leading .
          2. Remove the rule .
          3. For each rule :
            1. Add the rule .
    2. Remove direct left recursion for as described above.

Step 1.1.1 amounts to expanding the initial nonterminal in the right hand side of some rule , but only if . If was one step in a cycle of productions giving rise to a left recursion, then this has shortened that cycle by one step, but often at the price of increasing the number of rules.

The algorithm may be viewed as establishing a topological ordering on nonterminals: afterwards there can only be a rule if . Note that this algorithm is highly sensitive to the nonterminal ordering; optimizations often focus on choosing this ordering well.[clarification needed]

Pitfalls

Although the above transformations preserve the language generated by a grammar, they may change the parse trees that witness strings' recognition. With suitable bookkeeping, tree rewriting can recover the originals, but if this step is omitted, the differences may change the semantics of a parse.

Associativity is particularly vulnerable; left-associative operators typically appear in right-associative-like arrangements under the new grammar. For example, starting with this grammar:

the standard transformations to remove left recursion yield the following:

Parsing the string "1 - 2 - 3" with the first grammar in an LALR parser (which can handle left-recursive grammars) would have resulted in the parse tree:

Left-recursive parsing of a double subtraction
Left-recursive parsing of a double subtraction

This parse tree groups the terms on the left, giving the correct semantics (1 - 2) - 3.

Parsing with the second grammar gives

Right-recursive parsing of a double subtraction
Right-recursive parsing of a double subtraction

which, properly interpreted, signifies 1 + (-2 + (-3)), also correct, but less faithful to the input and much harder to implement for some operators. Notice how terms to the right appear deeper in the tree, much as a right-recursive grammar would arrange them for 1 - (2 - 3).

Accommodating left recursion in top-down parsing

A formal grammar that contains left recursion cannot be parsed by a LL(k)-parser or other naive recursive descent parser unless it is converted to a weakly equivalent right-recursive form. In contrast, left recursion is preferred for LALR parsers because it results in lower stack usage than right recursion. However, more sophisticated top-down parsers can implement general context-free grammars by use of curtailment. In 2006, Frost and Hafiz described an algorithm which accommodates ambiguous grammars with direct left-recursive production rules.[3] That algorithm was extended to a complete parsing algorithm to accommodate indirect as well as direct left recursion in polynomial time, and to generate compact polynomial-size representations of the potentially exponential number of parse trees for highly ambiguous grammars by Frost, Hafiz and Callaghan in 2007.[4] The authors then implemented the algorithm as a set of parser combinators written in the Haskell programming language.[5]

See also

References

  1. ^ Notes on Formal Language Theory and Parsing at the Wayback Machine (archived 2007-11-27). James Power, Department of Computer Science National University of Ireland, Maynooth Maynooth, Co. Kildare, Ireland.JPR02
  2. ^ Moore, Robert C. (May 2000). "Removing Left Recursion from Context-Free Grammars" (PDF). 6th Applied Natural Language Processing Conference: 249–255.
  3. ^ Frost, R.; R. Hafiz (2006). "A New Top-Down Parsing Algorithm to Accommodate Ambiguity and Left Recursion in Polynomial Time". ACM SIGPLAN Notices. 41 (5): 46–54. doi:10.1145/1149982.1149988. S2CID 8006549., available from the author at http://hafiz.myweb.cs.uwindsor.ca/pub/p46-frost.pdf Archived 2015-01-08 at the Wayback Machine
  4. ^ Frost, R.; R. Hafiz; P. Callaghan (June 2007). "Modular and Efficient Top-Down Parsing for Ambiguous Left-Recursive Grammars" (PDF). 10th International Workshop on Parsing Technologies (IWPT), ACL-SIGPARSE: 109–120. Archived from the original (PDF) on 2011-05-27.
  5. ^ Frost, R.; R. Hafiz; P. Callaghan (January 2008). "Parser Combinators for Ambiguous Left-Recursive Grammars". Practical Aspects of Declarative Languages (PDF). Lecture Notes in Computer Science. Vol. 4902. pp. 167–181. doi:10.1007/978-3-540-77442-6_12. ISBN 978-3-540-77441-9.

Read other articles:

2023 Zimbabwean general election ← 2018 23 August 2023 (2023-08-23) 2028 → ← outgoing memberselected members → Presidential electionRegistered6,623,511 ( 16.29%)Turnout68.86% ( 16.24pp)   Candidate Emmerson Mnangagwa Nelson Chamisa Party ZANU–PF CCC Popular vote 2,350,711 1,967,343 Percentage 52.60% 44.03% President before election Emmerson Mnangagwa ZANU–PF Elected President Emmerson Mnangagwa ZANU–PF National Assemb...

 

American baseball player (1900-1971) This article is about the baseball outfielder. For the pitcher, see Goose Gossage. Baseball player Goose GoslinGoslin in 1924Left fielderBorn: (1900-10-16)October 16, 1900Salem, New Jersey, U.S.Died: May 15, 1971(1971-05-15) (aged 70)Bridgeton, New Jersey, U.S.Batted: LeftThrew: RightMLB debutSeptember 16, 1921, for the Washington SenatorsLast MLB appearanceSeptember 25, 1938, for the Washington SenatorsMLB statisticsBatt...

 

Election in Ohio Main article: 1868 United States presidential election 1868 United States presidential election in Ohio ← 1864 November 3, 1868 1872 →   Nominee Ulysses S. Grant Horatio Seymour Party Republican Democratic Home state Illinois New York Running mate Schuyler Colfax Francis P. Blair, Jr. Electoral vote 21 0 Popular vote 280,167 238,621 Percentage 54.00% 46.00% County Results Grant   50-60%   60-70%   70...

HoremhebDettaglio del viso di Horemheb, recante la barba posticcia e il copricapo nemes con l'ureo, da una scultura in pietra calcarea che lo raffigura assiso accanto al dio Horus che lo abbraccia. Vienna, Kunsthistorisches Museum[1]Signore dell'Alto e del Basso EgittoIn caricadibattuto; 1319 a.C. –1292 a.C.[2][3] PredecessoreAy SuccessoreRamesse I Nome completoDjeserkheperura-Setepenra Horemheb-Meriamon NascitaEracleopoli Luogo di sepolturatomba KV57 ...

 

Dusty RhodesRhodes 1982Nama lahirVirgil Riley Runnels Jr.Lahir(1945-10-11)11 Oktober 1945Austin, Texas, U.S.Meninggal11 Juni 2015(2015-06-11) (umur 69)Orlando, Florida, U.S.Sebab meninggalGagal ginjalAlma materWest Texas State University (West Texas A&M University)PasanganSandra Runnels ​ ​(m. 1965; c. 1975)​Michelle Runnels ​ ​(m. 1978⁠–⁠2015)​Anak4, Dustin Rhodes, Kristin Runnels,...

 

Academic journalThe American Journal of GastroenterologyDisciplineGastroenterologyLanguageEnglishEdited byJasmohan Bajaj, MD, MS, FACG & Millie Long, MD, MPH, FACGPublication detailsFormer name(s)Review of GastroenterologyHistory1934–presentPublisherLippincott Williams & Wilkins (United States)FrequencyMonthlyImpact factor12.045 (2021)Standard abbreviationsISO 4 (alt) · Bluebook (alt1 · alt2)NLM (alt) · MathSciNet (alt )ISO 4Am. J....

Radio station in Greencastle, PennsylvaniaWRGG-LPGreencastle, PennsylvaniaBroadcast areaGreencastle, PennsylvaniaAntrim Township, PennsylvaniaWaynecastle, PennsylvaniaFrequency93.7 FM MHzBrandingWRGG 93.7 FMProgrammingFormatFull serviceOldies[1]AffiliationsNowCast WeatherWestwood One NewsOwnershipOwnerWRGG Good Companion RadioHistoryFirst air dateJune 14, 2016[2]Former call signsWRGG-LP (2015-Present)[3]Technical informationFacility ID197436ClassL1ERP29 wattsHAAT55 met...

 

1999 Hong Kong action romantic comedy film directed by Vincent Kok GorgeousDirected byVincent KokWritten by Jackie Chan Vincent Kok Yiu Fai Lo Produced byJackie ChanStarring Jackie Chan Shu Qi Tony Leung Chiu-wai Wakin Chau CinematographyCheung Man-poEdited by Cheung Ka-Fai Kwong Chi-leung Music byWong Dang-yiProductioncompanyGolden HarvestDistributed byGolden HarvestRelease date 12 February 1999 (1999-02-12) Running time120 minutesCountryHong KongLanguagesCantoneseMandarinHokk...

 

この記事は検証可能な参考文献や出典が全く示されていないか、不十分です。出典を追加して記事の信頼性向上にご協力ください。(このテンプレートの使い方)出典検索?: コルク – ニュース · 書籍 · スカラー · CiNii · J-STAGE · NDL · dlib.jp · ジャパンサーチ · TWL(2017年4月) コルクを打ち抜いて作った瓶の栓 コルク(木栓、�...

يفتقر محتوى هذه المقالة إلى الاستشهاد بمصادر. فضلاً، ساهم في تطوير هذه المقالة من خلال إضافة مصادر موثوق بها. أي معلومات غير موثقة يمكن التشكيك بها وإزالتها. (ديسمبر 2018) ملاكمة – رجالوزن البانتام في الألعاب الأولمبية الصيفية 1952 التاريخ 28 يوليو – 2 أغسطس المنافسين 23من 23 ب�...

 

Marco Tullio Giordana (kelahiran 1 Oktober 1950 di Milan) adalah seorang sutradara dan penulis latar Italia. Film buatannya Quando sei nato non puoi più nasconderti masuk ke dalam Festival Film Cannes 2005.[1] Filmografi Sutradara To Love the Damned (1980) The Fall of the Rebel Angels (1981) Notti e nebbie (1984) - Film TV Appointment in Liverpool (1988) Especially on Sunday (1991) - segmen La neve sul fuoco L'unico paese al mondo (1994) Who Killed Pasolini? (1995) Scarpette bianche ...

 

Nature reserve in Ealing and Hounslow, UK Gunnersbury TriangleIUCN category IV (habitat/species management area)[1]Acid grassland and birches on old railway trackLocationHounslow/EalingNearest cityLondon, EnglandCoordinates51°29′39.08″N -0°16′5.8″W / 51.4941889°N 0.268278°W / 51.4941889; -0.268278Governing bodyLondon Wildlife Trustwww.wildlondon.org.uk/nature-reserves/gunnersbury-triangle Gunnersbury Triangle is a 2.57-hectare (6.4-acre) l...

本條目存在以下問題,請協助改善本條目或在討論頁針對議題發表看法。 此條目需要擴充。 (2013年1月1日)请協助改善这篇條目,更進一步的信息可能會在討論頁或扩充请求中找到。请在擴充條目後將此模板移除。 此條目需要补充更多来源。 (2013年1月1日)请协助補充多方面可靠来源以改善这篇条目,无法查证的内容可能會因為异议提出而被移除。致使用者:请搜索一下条目的...

 

土库曼斯坦总统土库曼斯坦国徽土库曼斯坦总统旗現任谢尔达尔·别尔德穆哈梅多夫自2022年3月19日官邸阿什哈巴德总统府(Oguzkhan Presidential Palace)機關所在地阿什哈巴德任命者直接选举任期7年,可连选连任首任萨帕尔穆拉特·尼亚佐夫设立1991年10月27日 土库曼斯坦土库曼斯坦政府与政治 国家政府 土库曼斯坦宪法 国旗 国徽 国歌 立法機關(英语:National Council of Turkmenistan) ...

 

Pour les articles homonymes, voir O barré. Un zéro barré, un zéro pointé et un zéro ordinaire. Zéro barré habituel. Le zéro barré ou le zéro pointé sont des conventions typographiques utilisées pour différencier le chiffre 0 qu'il représente de la lettre O, dont l'apparence est proche. Ce zéro représenté 0̸ est donc marqué d'une barre diagonale ou d'un point. Origine L'utilisation du zéro barré précède de loin l'ère informatique, puisque des textes des XIIe et...

Indian-American journalist and author Fareed ZakariaZakaria in 2012BornFareed Rafiq Zakaria (1964-01-20) 20 January 1964 (age 60)Mumbai, Maharashtra, IndiaEducationYale University (BA)Harvard University (MA, PhD)OccupationsJournalistwriterpolitical commentatorEmployerCNNNotable credit(s)Fareed Zakaria GPS, host (2008–present)Time, contributing editor (2010–2014)Newsweek International, editor (2000–2010)Foreign Exchange, host (2005–2007)Foreign Affairs, former managing editorSpous...

 

العلاقات السورينامية الكرواتية سورينام كرواتيا   سورينام   كرواتيا تعديل مصدري - تعديل   العلاقات السورينامية الكرواتية هي العلاقات الثنائية التي تجمع بين سورينام وكرواتيا.[1][2][3][4][5] مقارنة بين البلدين هذه مقارنة عامة ومرجعية للدولتين: و�...

 

German jazz trombonist Albert MangelsdorffMangelsdorff in concertBackground informationBorn(1928-09-05)September 5, 1928Frankfurt am Main, Hesse-Nassau, Prussia, GermanyDiedJuly 25, 2005(2005-07-25) (aged 76)Frankfurt am Main, Hesse, GermanyGenresJazzOccupation(s)MusicianInstrument(s)TromboneYears active1948–2005Formerly ofUnited Jazz + Rock EnsembleMusical artist Albert Mangelsdorff (September 5, 1928 – July 25, 2005) was a German jazz trombonist. Working mainly in free jazz, he was...

Scenic road in Utah and Colorado in the United States Dinosaur Diamond National Scenic BywayDinosaur Diamond highlighted in redRoute informationLength486 mi[1] (782 km)Existed2002–presentComponenthighways US 191 US 40 SH 64 SH 139 I-70 / US 6 / US 50 SR-128 Major junctionsEast end US 50 / SH 340 Grand Junction, ColoradoWest end US 6 / US 191 Price, Utah LocationCountryUnited StatesStatesUt...

 

Voce principale: Società Calcio Albanova. Società Calcio AlbanovaStagione 1995-1996Sport calcio Squadra Albanova Allenatore Pasquale Santosuosso Presidente Mario Natale Serie C25º posto nel girone C. Maggiori presenzeCampionato: Basile (33) Miglior marcatoreCampionato: Cordelli (9) 1994-1995 1996-1997 Si invita a seguire il modello di voce Questa voce raccoglie le informazioni riguardanti la Società Calcio Albanova nelle competizioni ufficiali della stagione 1995-1996. Rosa N. Ruolo...