yacc-a-dy YACC yacc, Help me HACK!

Bryan Glennon bryang at chinet.chi.il.us
Thu Oct 11 12:18:08 AEST 1990


>From article <289 at cti1.UUCP>, by mpledger at cti1.UUCP (Mark Pledger):

> 
>      Could somebody please give me a hand with a lex & yacc problem.  I am 
> trying to build a small subset of the SQL language.  For purposes of this 
> question my yacc grammer is defined below. 
> 

There are (I think) two problems here.  The first is in the lexical analyzer,
which returns the token EOC when it sees a semicolon.  Unless EOC is defined
as ';', this will be a problem, since the cmds rule expects to see select ';':

>";"                  { 
>                     yylval.string = yytext;
>                     return(EOC);         
>                     }

>cmds        : select ';'

I changed the analyzer to return ';' when it sees a semicolon.

The second problem lies in the definition of cmds.  It does not recurse, so
it will not know what to do with statements other than the first.  Anyway,
here is the original rule, and the changes I made:

>cmds        : select ';'
>            { 
>            fprintf(stdout,"SQL syntax correct.\n");
>            }
>            ;

cmds        :
            | cmds select ';'
            { 
            fprintf(stdout,"SQL syntax correct.\n");
            }
            ;

My version seems to work, although I had to guess as to what the symbol lookup
routine does.  If you want to see my complete test source, drop me a line and
I will e-mail it to you.

BTW, you don't have to allocate dynamic memory to hold the matched patterns,
since yytext is a global.  What you do have to be careful of (and I think this
is what the response was referring to) is that you don't return the address of
local strorage that is created within the analyzer.  

Hopes this helps...
                              Bryan

...chinet!bryang                            "Hey, Rock!  Watch me pull a
...chinet!bpgc!bryan                         rabbit outta my hat!" 
                                                     -Bullwinkle



More information about the Comp.lang.c mailing list