[Haskell-cafe] LL(1) parsing of declarators

Lennart Augustsson lennart at augustsson.net
Sun Apr 15 14:18:37 EDT 2007


Oh, but C parsers often do a trick like extending the lexer when  
typedefs are encountered.
Typedefs part of the challange of parsing C correctly.  And the lexer  
should definitely know about C's keywords.  That's what the lexer is  
for.

	-- Lennart

On Apr 15, 2007, at 07:46 , Stefan O'Rear wrote:

> On Sun, Apr 15, 2007 at 07:42:02AM +0100, Lennart Augustsson wrote:
>> But the qualifiers aren't arbitrary names, are they?
>
> Yes they are.  I don't have knowledge of typedefs used :)
>
> Nice try though, I toyed with that idea for a long while.  Ultimately
> I decided that it would complicate the lexer too much to add knowledge
> of C's keywords.  Then I thought of the typedef problem.
>
>> On Apr 15, 2007, at 04:52 , Stefan O'Rear wrote:
>>
>>> I'm writing a code generator for C, and I'm trying to parse a C-like
>>> input language using LL(1) (parsec specifically).  The syntax of
>>> declarators is giving me trouble: (simplified)
>>>
>>> declaration = qualifiers >> (declarator `sepBy1` char ',')
>>> qualifiers = many1 name
>>> declarator = name
>>>
>>> now if we have "<name> <name>", they are both parsed by the greedy
>>> many1 in qualifiers!  I can make this work with some ugly  
>>> rearranging:
>>>
>>> declaration = fdeclarator >> many (char ',' >> declarator)
>>> fdeclarator = name >> many1 name
>>> declarator = name
>>>
>>> is there a more elegant way?
>
> Stefan



More information about the Haskell-Cafe mailing list