From: | anton@mips.complang.tuwien.ac.at (Anton Ertl) |
Newsgroups: | comp.compilers |
Date: | Wed, 19 Jan 2011 14:45:28 GMT |
Organization: | Institut fuer Computersprachen, Technische Universitaet Wien |
References: | 10-12-040 10-12-043 11-01-005 11-01-009 11-01-027 11-01-057 11-01-086 |
Keywords: | parse, design, LL(1) |
Posted-Date: | 21 Jan 2011 22:06:17 EST |
Peter Canning <9cn6w6402@sneakemail.com> writes:
>On 1/14/2011 2:04 PM, Chris F Clark wrote:
>> From a grammar writing point of view, it is not particularly
>> difficult to introduce PL/I style keywords to any LR grammar. The
>> same changes should generally work for LL grammars also. If people
>> are interested I can document them here.
>
>I'm definitely interested in seeing a description of how to do this (in
>my case for LL grammars).
It's pretty easy for LL(1) grammars: At each position, treat all
keywords as identifiers if they don't occur in the first set. E.g.,
in many languages "IF" occurs only as keyword at the start of a
statement, so in a PL/I-style variant of those languages "IF" could be
treated as identifier everywhere else.
A disadvantage of this approach is that some syntax errors can only be
discovered later. E.g, to pick up the example above, if the semicolon
in front of an IF is missing, a Pascal compiler will notice a syntax
error when it sees the "IF", but not necessarily a compiler for a
PL/I-style language. I don't know how relevant this is in practice.
If only new keywords (for language extensions beyond the original
standard, where new keywords often cause problems) are treated in this
way, this disadvantage is probably small compared to the benefit.
Such a scheme is probably best implemented in the parser (which knows
about the first-sets), but at the interface to the scanner (so the
higher levels don't need to have special treatment of such
identifiers).
- anton
--
M. Anton Ertl
anton@mips.complang.tuwien.ac.at
http://www.complang.tuwien.ac.at/anton/
Return to the
comp.compilers page.
Search the
comp.compilers archives again.