Re: how to parse this?

torbenm@diku.dk (Torben Ęgidius Mogensen)
Tue, 01 Feb 2011 13:35:34 +0100

          From comp.compilers

Related articles
how to parse this? n.oje.bar@gmail.com (nojb) (2011-01-19)
Re: how to parse this? torbenm@diku.dk (2011-02-01)
| List of all articles for this month |
From: torbenm@diku.dk (Torben Ęgidius Mogensen)
Newsgroups: comp.compilers
Date: Tue, 01 Feb 2011 13:35:34 +0100
Organization: SunSITE.dk - Supporting Open source
References: 11-01-095
Keywords: parse, yacc
Posted-Date: 01 Feb 2011 13:06:50 EST

nojb <n.oje.bar@gmail.com> writes:


> Hi,
>
> My language has function applications like ML, i.e. f g h means f
> applied to two parameters g and h. But the rule
>
> exp: IDENT list(exp)
>
> in Yacc will parse this as f (g h). How can I fix this?


You can either fix it by post-processing the parse tree, or you can
write the production using explicit left-recursion:


exp: application


application: IDENT exp
                      | application exp


If applications are really ML-like, they don't need to start with an
identifier, so you should rather have something like


exp: exp exp


which, however, is ambiguous. You can fix this by adding a %prec
declaration or by dividing expressions into simple and complex
expressions and only allowing simple expressions as arguments to
function applications, i.e.,


exp: exp simple
      | simple


simple: IDENT
            | LPAR exp RPAR
            | ...


where LPAR and RPAR are the token names for parentheses.


Torben



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.