help-bison
[Top][All Lists]
Advanced

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

RE: The Bison Manual Example Infix Calculator


From: Donges, William E.,, III
Subject: RE: The Bison Manual Example Infix Calculator
Date: Sat, 3 Aug 2002 14:56:59 +0200


-----Original Message-----
From:   Hans Aberg [mailto:address@hidden
Sent:   Fri 8/2/2002 6:41 AM
To:     Donges, William E.,, III
Cc:     address@hidden
Subject:        Re: The Bison Manual Example Infix Calculator
Reply-to: address@hidden


The Bison generated parser only reads the token numbers handed over by the
yylex function, and does not know or care about the origin, if it is >from a
string or stream or whatever. So merely change your yylex function.

In yylex example of the manual, you can merely replace the getchar(), etc
calls,  top provide the characters from wherever you want them to be read.

Ok I have rewritten the yylex function in the example as follows

int yylex (char *s) {
        int c==s[0];

        /* skip white space  */
        for(;isspace(c);s++,c=s[0]);
        if (c == '.' || isdigit (c)) {
                sscanf(s,"%d",&yyval);
                for(;isdigit(c);s++,c=s[0]);
                return NUM;
        } else if (c == EOF) {
                return 0;
        } else {
                /* return single chars */
                return c;
        }
}

Which would seem to be what I want to do in this case, at least comparing
it to the existing one which uses getchar, but I still dont understand how
to tell the parser that I want to parse a string in main.


I mean if I have main setup as follows

int main() {
char *s="      3+2-10^5-8";
yyparse();
}


it of course will not do what I want... How do I actually get the
string literal into yylex and parse the result? And how can I capture the
resultant int or float to a variable rather then just having it displayed
to stdout?





reply via email to

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