Semicolon Counter


--- procedure SEMI
--
--  Count all semi-colons in the input file and send this
--  count to the output file.

with TEXT_IO; use TEXT_IO;

procedure SEMI -- Count all semicolons.

is

    package INT_IO is new INTEGER_IO (NATURAL);

    CHAR: CHARACTER;
    LINE: STRING (1 .. 250);

    LAST: NATURAL;

    COUNTER: NATURAL := 0;

    QUOTE, DQUOTE, MINUS : BOOLEAN;

begin

    while not END_OF_FILE loop

        GET_LINE (LINE, LAST);

        if LAST >= LINE'FIRST then

            QUOTE := FALSE;
            DQUOTE := FALSE;
            MINUS := FALSE;

            for I in 1 .. LAST loop
                CHAR := LINE (I);
                case CHAR is
                    when '-' =>
                        if not DQUOTE and not QUOTE then
                            if MINUS then
                                -- double MINUS, i.e., comment; exit loop
                                exit;
                            else
                                -- We've seen a minus.
                                MINUS := TRUE;
                            end if;
                        end if;
                    when '"' =>
                        if not QUOTE then
                            -- not within a quote
                            DQUOTE := not DQUOTE;
                        end if;
                        MINUS := FALSE;
                    when ''' =>
                        if not DQUOTE then
                            -- not within a double quote
                            QUOTE := not QUOTE;
                        end if;
                        MINUS := FALSE;
                    when ';' =>
                        if not DQUOTE and not QUOTE then
                            -- not within quotes of any kind, count it.
                            COUNTER := COUNTER + 1;
                        end if;
                        MINUS := FALSE;
                    when OTHERS =>
                        MINUS := FALSE;
		end case;
            end loop;

        end if;

    end loop;

    PUT ("There are");
    INT_IO.PUT (COUNTER, WIDTH => 10);
    PUT_LINE (" semi-colons in the input stream.");

end SEMI;


Contributed by: Clyde Roby
Contributed on: October 12, 1999
License: Public Domain

Back