Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!swrinde!elroy.jpl.nasa.gov!usc!zaphod.mps.ohio-state.edu!think.com!mintaka!spdcc!iecc!compilers-sender From: meissner@osf.org Newsgroups: comp.compilers Subject: Handling keywords that are not reserved? Keywords: parse, yacc, question Message-ID: <9101172353.AA19424@curley.osf.org> Date: 17 Jan 91 23:53:17 GMT References: <48132@apple.Apple.COM> Sender: compilers-sender@iecc.cambridge.ma.us Reply-To: meissner@osf.org Organization: Compilers Central Lines: 45 Approved: compilers@iecc.cambridge.ma.us In-Reply-To: amb@Apple.COM's message of 15 Jan 91 23:38:09 GMT A. Michael Burbidge writes: |I am writing a parser for MPW's Object Pascal. Some of the extension keywords |to standard Pascal are not reserved. I am using yacc and having some trouble |figuring out a clean way to handle non-reserved keywords. And John writes: | [There isn't any clean way to handle non-reserved keywords in yacc. You | have to feed information from the parser back to the lexer to tell it when | a token can be a keyword and when it can't. -John] You can do it in other ways as well. I recall that some people used the following to parse PL/1 (which has no reserved words at all). You have a token for each keyword, and an everything else keyword. Then you have an identifier reduction like: : other | keyword1 | keyword2 | keyword3 ... ; You put this at the end of the file, so that yacc's default rules (of chosing the shift over a reduce rule in case of a conflict in a shift/reduce conflict) will give you what you want. In order to eliminate the shift/reduce conflicts, you then need to have productions like: : other | keyword2 | keyword3 ... ; : other | keyword1 | keyword3 ... ; You do have to make sure that in general the rules with the special keywords come before the more general rules. -- Michael Meissner email: meissner@osf.org phone: 617-621-8861 Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142 -- Send compilers articles to compilers@iecc.cambridge.ma.us or {ima | spdcc | world}!iecc!compilers. Meta-mail to compilers-request.