Recursive Parsing in BYACC & FLEX

Serge@pentora.demon.co.uk (Serge Eaton)
12 Dec 1997 14:49:34 -0500

          From comp.compilers

Related articles
Recursive Parsing in BYACC & FLEX Serge@pentora.demon.co.uk (1997-12-12)
| List of all articles for this month |

From: Serge@pentora.demon.co.uk (Serge Eaton)
Newsgroups: comp.compilers
Date: 12 Dec 1997 14:49:34 -0500
Organization: Compilers Central
Keywords: parse, question, comment

I wonder if anybody can help me


I am currently writting a parser for a specific language for medical
instruments. The code needs flow control ie. REPEAT, FOR , IF etc.


I am currently using FLEX for the token anaylsis and BYACC (DOS Ports)
for the grammar. The problem I am having is in recursive calls to the
PARSER to parse the code linked to flow control statments.


Currently the methord I am using is to match the flow control tokens,
then use the standard INPUT() FLEX routine to get the further
construct data linked to the flow control statment. This is then
passed to BYACC, which determines the statments that need to be
reparsed iteritivly.


To try and maintain the buffer position form the original buffer, I am
using YY_CURRENT_BUFFER macro to get the curent state of the buffer. I
am then using yy_scan_string("statment") to create a new
yy_buffer_state and the calling yyparse() to parse the statment. I
then delete the new buffer using yy_delete_buffer(YY_CURRENT_BUFFER).
I the switch to the old buffer using
yy_switch_to_buffer(backreference).


Code below


/*Push string into the current buffer*/
void pushmessage(char *msg)
{


/*Copy string into new buffer and Switch buffers*/
yy_scan_string ( msg );


/*Parse the string*/
yyparse();


/*Delete the new buffer*/
yy_delete_buffer(YY_CURRENT_BUFFER);


} /* end pushmessage */


This however does not seem to work, as i seem to be getting SYNTAX
error on the end of the flow control code. I think that the parser is
losing track of the position within the input buffer. I am of the
understanding that BYACC and FLEX. are not very good at handeling
reentrant or recursive parsing without non-trivial editing of the
generated C/C++ file. Is this the case and am I better of gettiing a
commercial software like MKS YACC/LEX that has allocated dynamic
stacks that allow recursive and reentrant parsing.


Does anybody have any experiance with this sort of parser?
Thanks in advance.


Serge Eaton


Development Engineer
Zenyx Scientific
[My main suggestion is don't do that. Scan and parse once, generate byte
codes of some sort, and interpret that. If you insist on rescanning and
reparsing, carve the code up into statements and call yyparse() per
statement, pointing the lexer at the appropriate string before each call.
-John]
--


Post a followup to this message

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