This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: cpplib: expression parser patch 2


On Sat, Apr 01, 2000 at 08:21:50AM +0900, Neil Booth wrote:
> This is the second of a set of patches to the preprocessor's expression
> parser.
> 
> Most of the patch is code rearrangement and regrouping, which does not
> affect parsing logic.  There is however one major change: the handling
> of '(' and ')'.
> 
> Previously, these were handled with the standard shift / reduce logic
> based on left- and right priorities.  The current patch forces an
> immediate shift of '(' without considering reductions, and an
> immediate reduction of ')' to the previous '(' without shifting.  This
> paves the way for simplification of the priority scheme in the next
> patch.

Does this handle

#if 2 * ( 3 + 4    /* missing right paren */
#if 2 *   3 + 4 )  /* missing left paren */

I see error messages for both cases, but I don't see how we get to
them.  I'm also confused by the presence of the case ')' in the second
switch; when will there ever be a ) on the stack?

The way you've structured some of the conditions is confusing.
Instead of writing

     case X:
       if(cond 1)
       {
         if(cond 2)
	 {
	   handle correct syntax;
	   break;
	 }
	 error("cond 2");
	 goto syntax_error;
       }
       error("cond 1");
       goto syntax_error;

it would be clearer if you wrote

     case X:
       if(!cond 1)
       {
         error("cond 1");
         goto syntax_error;
       }
       if(!cond 2)
       {
	 error("cond 2");
	 goto syntax_error;
       }
       handle correct syntax;
       break;

and possibly even clearer to write

#define SYNTAX_ERROR(message) \
  do { error(message); goto syntax_error; } while(0)

     case X:
       if(!cond 1)
         SYNTAX_ERROR("cond 1");
       if(!cond 2)
         SYNTAX_ERROR("cond 2");

       handle correct syntax;
       break;

I know the optimizer doesn't generate quite as good code for it this
way, but #if parsing is not worth tuning for speed, and the optimizer
will eventually be improved.

zw

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]