Related articles |
---|
parse last line aw0g+@andrew.cmu.edu (Aaron Wohl) (1995-02-17) |
Re: parse last line umrigar@cs.binghamton.edu (Zerksis D. Umrigar) (1995-02-21) |
Re: parse last line jpowers@ti.com (1995-02-21) |
Newsgroups: | comp.compilers |
From: | "Zerksis D. Umrigar" <umrigar@cs.binghamton.edu> |
Keywords: | lex, parse |
Organization: | Compilers Central |
References: | 95-02-134 |
Date: | Tue, 21 Feb 1995 17:08:05 GMT |
Aaron Wohl <aw0g+@andrew.cmu.edu> wants the last newline for a Basic dialect
to be optional. He writes:
>My grammar looks like:
>
>program:
> stmt
> | program stmt
> ;
>
>stmt:
> EOL
> | NEXT EOL
> | RETURN EOL
> | 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:
%token EOL NEXT RETURN ID
%%
program
: stmt
| program EOL stmt
;
stmt
: /* empty */
| NEXT
| RETURN
| ID ':' stmt
| ID '=' exp
;
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
umrigar@cs.binghamton.edu
--
Return to the
comp.compilers page.
Search the
comp.compilers archives again.