YACC question

Robert Steven Glickstein bobg+ at andrew.cmu.edu
Mon Jan 15 12:12:12 AEST 1990


Here is a fragment of Yacc code:

    %token PLUS MINUS

    %%

    expr:       mulexpr PLUS mulexpr
        | mulexpr MINUS mulexpr

It's very straightforward; the yylex() routine must be written to return
the constant PLUS when it encounters a '+' in the input, and the
constant MINUS when it encounters a '-' in the input.  However, Yacc
allows you to rewrite the above fragment as

    %%

    expr:       mulexpr '+' mulexpr
        | mulexpr '-' mulexpr

My question is, where does Yacc find the '+' and the '-' characters? 
Apparently they're not gotten via a call to yylex().  Does Yacc simply
do a getchar()?

I ask because I have written a parser which can be configured to read
from various input sources (standard input, file, string).  There are
many places in the parser where token-name constants are used when a
single character will do; however, if the single character is retrieved
by getchar() or some other hardwired mechanism, I'll have to stick to
the yylex() approach (since my yylex() knows where to read characters
from).

Please e-mail your replies.  Thanks in advance.

_______________________________
Bob Glickstein, System Designer
Information Technology Center  room 220
Carnegie Mellon University
Pittsburgh, PA  15213-3890
(412) 268-6743

Internet: bobg+ at andrew.cmu.edu
Bitnet: bobg%andrew.cmu.edu at cmuccvma.bitnet
UUCP: ...!harvard!andrew.cmu.edu!bobg

I could dance till the cows come home.  On second thought, I'd rather
dance with the cows till you come home.
		-- Groucho Marx



More information about the Comp.unix.questions mailing list