Statement at a time parsing with yacc

przemek@viewlogic.com (Przemek Skoskiewicz)
Fri, 6 Dec 91 09:51:04 EST

          From comp.compilers

Related articles
Statement at a time parsing with yacc przemek@viewlogic.com (1991-12-06)
Re: Statement at a time parsing with yacc bart@cs.uoregon.edu (1991-12-10)
Re: Statement at a time parsing with yacc bliss@sp64.csrd.uiuc.edu (1991-12-10)
Parsing one statement at a time pg@bsg.com (Peter Garst) (1991-12-11)
Re: Statement at a time parsing with yacc chris@mks.com.ca (1991-12-11)
Statement at a time parsing with yacc compres!chris@crackers.clearpoint.com (1991-12-12)
Stmt-at-a-time-parsing with YACC zycad!vijay@sun.com (1991-12-16)
[1 later articles]
| List of all articles for this month |

Newsgroups: comp.compilers
From: przemek@viewlogic.com (Przemek Skoskiewicz)
Keywords: yacc, parse, question
Organization: Compilers Central
Date: Fri, 6 Dec 91 09:51:04 EST

I have a nifty parser for a C-like language. I can run it on a file or a
string containing one statement and everything is cool. What I want it to
do is to point to a file and ask it to parse *one* statement only, even if
there are many statements in sequence in the file.




My top two rules are as follows and %start is set to top_statements:




top_statements:
            statements {parse_return = $1;}
        | {parse_return = NULL;}
;


statements:
            statement {$$ = cons ($1, NULL);}
        | statements statement {$$ = cons ($2, $1);}
;




As you can see, if I give my parser a file, the `statements' rule is going
to match a sequence of statements, as many as there are in the file
(provided that it's syntactically correct). What I would like to do is to
conditionally disable that second clause in the `statements' rule: "|
statements statement". In this way, the parser would be looking for only
one statement and it would stop after finding it.




Question to the net:


Is it possible to trick yacc to go into this "statement by statement" mode
of operation? This has to be done dynamically, i.e. sometimes I want to
parse a whole file at once and sometimes I want it to do it statement by
statement with the parser returning control to me (yyparse) after each
statement is parsed.


Many thanks in advance. It seems like it would be a pity to waste all
this code that yacc is generating and having to write my own
"next_statement" parser in C.




Viewlogic Systems, Inc. Przemek Skoskiewicz
293 Boston Post Road West
Marlborough, MA 01752-4615, USA
email: przemek@viewlogic.com
[In some conversation with him, it came out that the hard part is that the
lexer can't tell where the statement boundaries are, so it can't easily
return the EOF token at the end of a statement. I already suggested YYACCEPT
and looking at yychar to see if there is a lookahead token. Any better
ideas? -John]
--


Post a followup to this message

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