Re: multiple entry points in yacc grammer
29 Apr 1996 23:06:16 -0400

          From comp.compilers

Related articles
multiple entry points in yacc grammer (1996-04-19)
Re: multiple entry points in yacc grammer (1996-04-29)
| List of all articles for this month |

Newsgroups: comp.compilers
Date: 29 Apr 1996 23:06:16 -0400
Organization: Compilers Central
References: 96-04-122
Keywords: yacc, comment (Naresh Bhatia) writes:

>Can a yacc grammar have multiple entry points? For example, if I have
>a grammar that can parse C++ method declarations, can I use it to
>parse standalone method parameters? The knowledge of parsing the
>parameters is obviously embedded in the method parser, so how to use
>this knowledge to parse the parameters alone?
>[No, but you can fake it. (...). -John]

This kludge may help. I did it because in a way to handle C
instructions OR declarations at any point, there is a shift/reduce
conflict ( 'identifier;' can be a valid declaration or instruction )

At the time where you know which entry the parser must take,
make the lexer return the pseudo-token FAKEn


#undef yylex()
#define yylex() my_lex()

int fake_token;


%token FAKE1 FAKE2 FAKE3
%start start


start: FAKE1 rule_1
          | FAKE2 rule_2
          | FAKE3 rule_3

/* those are your multiple entry points :: */

rule_1: .... ;
rule_2: .... ;


#undef yylex

int my_lex()
          int ret;

          if( fake_token )
                      ret = fake_token;
                      fake_token = 0;

                      return ret;

          return yylex();

Hope this helps,

[Yup, that's how you fake it. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.