help-bison
[Top][All Lists]
Advanced

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

Re: concatenating string into $$


From: Hans Aberg
Subject: Re: concatenating string into $$
Date: Fri, 27 Feb 2004 19:53:15 +0100

At 14:50 -0800 2004/02/26, Tom Jackson wrote:
>I have a bison rule containing the following:
>ifcmd:
>        IFCMD ARGUMENT ENDCMD
>        {
>          printf("\nif %s {\n ", $2)
>        }
>        ;
>
>The problem is that A rule like this cannot validate an entire if
>statement. I'm thinking I need a rule like:
>
>ifcmd:
>     IFCMD ARGUMENT ENDCMD statements ENDIF
>...
>
>
>I'm new to using bison, and all the examples are for calculators, where
>it is easy to accumulate results.
>
>What I want to do is to somehow accumulate the "\nif %s {\n" into $$.
>
>Do I need to write a function to perform this operation, or is there
>something simple to help me out??

You can write it as you suggest, in one single statement:
  ifcmd:
      IFCMD ARGUMENT ENDCMD statements ENDIF {...}
  ;
Or you can use several statements, like:
  ifcmd:
      ifcmd_head ifcmd_body {...}
  ;
  ifcmd_head:
      IFCMD ARGUMENT ENDCMD {...}
  ;
  ifcmd_body:
      statements ENDIF {...}
  ;

With more rules, you get more states, which means a more spacious and
potentially slower parser, but most compilers spend little time in the
parser code, so this should not be a worry.

The choices you make will depend in part on how you want the grammar to be
structured, and in part how certain grammatic part reappear in the
language. For example, if your "statements" part will be the same also in
other types of statements, then you will not want to duplicate that code.

>Are there more complete examples somewhere (I've read _lex & yacc_,
>etc.).

When I learned it, I used to look into known grammars of languages like
C/C++. Their ANSI/ISO standards have grammars (which can be bought for $18
or something from ANSI).

  Hans Aberg






reply via email to

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