Re: parse last line

"Zerksis D. Umrigar" <>
Tue, 21 Feb 1995 17:08:05 GMT

          From comp.compilers

Related articles
parse last line (Aaron Wohl) (1995-02-17)
Re: parse last line (Zerksis D. Umrigar) (1995-02-21)
Re: parse last line (1995-02-21)
| List of all articles for this month |

Newsgroups: comp.compilers
From: "Zerksis D. Umrigar" <>
Keywords: lex, parse
Organization: Compilers Central
References: 95-02-134
Date: Tue, 21 Feb 1995 17:08:05 GMT

Aaron Wohl <> wants the last newline for a Basic dialect
to be optional. He writes:

>My grammar looks like:
> stmt
> | program stmt
> ;
> | ID ':' stmt
> | ID '=' exp EOL
> ...
>All the stuff I tried to allow the last EOL to be optional ended up with
>shift/reduce conflicts on ID. I tried replacing the lex character
>input to return an extra eol before eof. But different versions of

If you choose (because of portability problems) not to manipulate lex's
character input to ensure that an EOF is always immediately preceeded by a
'\n', you could change the EOL from a statement terminator to a statement
separator in your grammar as follows:

    : stmt
    | program EOL stmt
    : /* empty */
    | NEXT
    | RETURN
    | ID ':' stmt
    | ID '=' exp
    : ID

The above grammar has no conflicts. It should satisfy your requirments for
the last EOL being optional: stmt does not end with EOL --- hence a program
need not end with a EOL; if a program does end with a EOL the last stmt will
be parsed as empty.

I have a mild preference for the pre-lex character manipulation solution to
the above problem, even tho' such a solution may not be portable between
different lexes. The reason is that after solving syntactic (conflict) and
semantic problems by grammar manipulation, one often lands up with a grammar
so far from the original, that it becomes hard to see the relationship
between the parsing grammar and the original definitional grammar.

-zerksis umrigar

Post a followup to this message

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