C-interpreter, newlines as separators?

"Noel S. Gorelick" <ngorelic@speclab.cr.usgs.gov>
Fri, 28 Apr 1995 19:44:51 GMT

          From comp.compilers

Related articles
C-interpreter, newlines as separators? ngorelic@speclab.cr.usgs.gov (Noel S. Gorelick) (1995-04-28)
Re: C-interpreter, newlines as separators? norvell@cs.toronto.edu (1995-05-09)
| List of all articles for this month |

Newsgroups: comp.compilers
From: "Noel S. Gorelick" <ngorelic@speclab.cr.usgs.gov>
Keywords: parse, question
Organization: Compilers Central
Date: Fri, 28 Apr 1995 19:44:51 GMT

I am writing what is turning out to be a C-like interpreter. As an
interpreter, the trailing semicolons are a nusiance, and seem kind of
silly most of the time. I would like to be able to optionally use newlines
as statement separators. My problem is shown below:


        for (i=0 ; i<10 ; i=i+1) bar(i) // works


        for (i=0 ; i<10 ; i=i+1) { // works
                bar(i)
        }


        for (i=0 ; i<10 ; i=i+1) // doesn't work
        {
                bar(i)
        }


These three statements should all be equal, but the the newline trailing
the for() in the last one matches to an empty expression.


What I think I need to do is to conditionally ignore newlines. Only when
trying to match a 'separator' rule, should a newline not be ignored. However,
I'm not savvy enough with yacc/lex to know what I'm doing. Here are the
rules I've got:


expression_statement
        : separator { $$ = NULL; }
        | expression separator { $$ = $1; }
        ;


separator
        : '\n'
        | ';'
        ;




I tried some code after matching 'expression', to disable/enable eating
of newlines, but this fails, causing the lexer to eat all newlines.


expression_statement
        : separator { $$ = NULL; }
        | expression { eatNL = 0; } separator { $$ = $1; eatNL = 1}
        ;


Any help would be greatly appreciated.
Thanks,


-- Noel (ngorelic@speclab.cr.usgs.gov)
--


Post a followup to this message

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