<div dir="auto">I was working at the whiteboard during a job interview once. I had been asked to write a function to report if its input had balanced parentheses. No problem: I wrote an RD parser in Python (which I prefer for whiteboarding) to detect balance and return True if the parse was successful and False if EOF was reached.<div dir="auto"><br></div><div dir="auto">I was starting to write some tests when the interviewer interrupted me.</div><div dir="auto"><br></div><div dir="auto">"What is that?"</div><div dir="auto"><br></div><div dir="auto">"It's a recursive descent parser. It detects if the input is well-formed."</div><div dir="auto"><br></div><div dir="auto">Blank look.</div><div dir="auto"><br></div><div dir="auto">I started to walk him through the code.</div><div dir="auto"><br></div><div dir="auto">He interrupted me. "Excuse me, I'll be back in a few minutes."</div><div dir="auto"><br></div><div dir="auto">Long wait, maybe 15-20 minutes. Someone else comes in. "Thank you, the recruiter will get back to you." That's the last I hear from them.</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 10, 2025, 12:10 AM Rob Pike <<a href="mailto:robpike@gmail.com" target="_blank" rel="noreferrer">robpike@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:arial,sans-serif">A rare case where I disagree with you, Doug. If the language is reasonably regular (I do not mean in the strict Kleene sense), a recursive descent parser is not much harder to write than a yacc grammar, and much smoother at providing good error messages. Having done many yaccs and many RD parsers, I no longer go to yacc.</div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,sans-serif">To put it another way, there are few programming tasks I enjoy more than writing a recursive descent parser for a sane language.</div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,sans-serif">Now if the language is not so regular, my position might shift. I do recall Bjarne dynamically editing the generated tables mid-parse to get yacc to handle at least one stage of C++'s development.</div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,sans-serif">Another way to think of it is that if you are designing the language and it is undergoing frequent changes in grammar, yacc could certainly be move you along faster. But even then once things had settled I'd still redo it as RD, for the quality of the result.</div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,sans-serif">You can credit Stephen R. "Software" Steve for this change in my thinking.</div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div><div class="gmail_default" style="font-family:arial,sans-serif">-rob</div><div class="gmail_default" style="font-family:arial,sans-serif"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Mon, Mar 10, 2025 at 1:12 PM Douglas McIlroy <<a href="mailto:douglas.mcilroy@dartmouth.edu" rel="noreferrer noreferrer" target="_blank">douglas.mcilroy@dartmouth.edu</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr">> everyone should write for their first compiler in Pascal for a<br>> simple language and no cheating using YACC. You need to write the whole<br>> thing if you want to understand how parsing really works.</div><div dir="ltr"><br></div><div>Yacc certainly makes it easier to write parsers for big grammars, but it's far from cheating. You need to know a lot more about parsing to use Yacc than you need to roll your own. </div><div><br></div><div>Hand parsing of a tiny grammar is almost a necessary step on the way to understanding Yacc. But I think hand-building the whole parser for a compiler is unnecessary torture--like doing trigonometry with log tables.</div><div><br></div><div>Doug</div></div></div></div></div></div>
</blockquote></div>
</blockquote></div>