[comp.compilers] Handling keywords that are not reserved?

amb@Apple.COM (A. Michael Burbidge) (01/16/91)

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.

I know this is a rather vague question, but if anyone has any tips or hints on
handling non-reserved keywords using yacc please let me know.

Mike Burbidge
Apple Computer, Inc.
amb@apple.com
[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]
-- 
Send compilers articles to compilers@iecc.cambridge.ma.us or
{ima | spdcc | world}!iecc!compilers.  Meta-mail to compilers-request.

meissner@osf.org (01/18/91)

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:

	<identifier>: 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:

	<identifier_except_keyword1>: other
				    | keyword2
				    | keyword3 ... ;

	<identifier_except_keyword2>: 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.