help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Conditional execution rules


From: James Buchanan
Subject: Conditional execution rules
Date: Mon, 6 Aug 2001 02:32:38 +1000

I have always tried to figure out how to make a type of conditional control
statement. I'm deeply disappointed that the lex and yacc book published by
OReilly gave boring examples like a menu generation language, an SQL parser
and a postfix to infix (or infix to postfix) translator. I didn't think
these were useful examples at all (well they weren't for translating control
structures anyway).

It seems that, for example:

if_stmt:        IF_TOK '(' bool_cond ')' THEN_TOK
                {
                    /* mumble */

in parsing these types of things that it has to emit labels finding the ends
of these conditionals and to somehow jump to them if the condition evaluated
true or false. What about nested if-then-else statements? What if bison had
parsed a hypothetical basic-like language statement:

    IF foo > bar AND NOT bar THEN
        IF foo < $variable THEN
            WRITE "mumble"
        ELSIF foo > $variable THEN
            WRITE "mumble-mumble"
        ELSE
            WRITE "foo"
        ENDIF
    ENDIF

In real language parsers, does the parser parse out the whole shebang, or
one bit at a time? For example does the grammar look like this:

if_stmt:        IF cond THEN
                        stmt
                    ENDIF
            ;

where stmt can include ELSIF and ELSE parts, or more IF-THEN parts to an
arbitrary depth, or do grammars for real languages (I mean those implemented
like C/C++ in GCC, or Perl or whatever) parse in piecemeal:

if_stmt:        IF cond THEN
                        stmt
            ;

elseif_stmt:        ELSIF cond THEN
                                stmt
            ;

else_stmt:        ELSE stmt ENDIF
                ;

and somehow try to track them to make sure an ENDIF follows a IF-THEN at the
end?


This is totally confusing. I haven't found any examples yet. Well, I've
tried to read the GCC grammars but they are far too complicated for a
beginner.

Does anyone know of any techniques that are used? My original thoughts were
to build a list of coditions and walk the list evaluating the conditions,
then walking though the statements, but, ahh, I don't know.

James





reply via email to

[Prev in Thread] Current Thread [Next in Thread]