From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,38d9bc3cc55bcedb X-Google-Attributes: gid103376,public From: Shawn Barber Subject: Re: Ada95 Ayacc / Aflex specifications Date: 1999/09/07 Message-ID: <28b1320f.6dc4fec9@usw-ex0102-014.remarq.com> X-Deja-AN: 522058242 References: <7r0gob$m31$1@nnrp1.deja.com> X-Originating-Host: 140.76.238.7 X-Complaints-To: wrenabuse@remarq.com X-Trace: WReNphoon3 936705400 10.0.2.14 (Tue, 07 Sep 1999 04:56:40 PDT) Organization: http://www.remarq.com: The World's Usenet/Discussions Start Here NNTP-Posting-Date: Tue, 07 Sep 1999 04:56:40 PDT Newsgroups: comp.lang.ada X-Wren-Trace: eBA1HRwFQghDYwYZB1IUCyIQDhABWl4DAVxOR0lKXgZRRANJUAc= Date: 1999-09-07T00:00:00+00:00 List-Id: Here is some math support I wrote for a compiler we wrote, it only supports Add, Subtract, Multiply and Divide but might provide you with a basis for your work. Ayacc spec file. ------------------------------------------------------------ ------ -- Simple Math Processing ---------------------------------------- ------------------------------------------------------------ ------ Numeric_Expression : Term { if $1.Value_Type = Integer_Type then $$.Int_Value := $1.Int_Value ; $$.Value_Type := Integer_Type ; elsif $1.Value_Type = Float_Type then $$.Float_Value := $1.Float_Value ; $$.Value_Type := Float_Type ; end if ; } | Numeric_Expression '+' Term { if $1.Value_Type = Integer_Type and $3.Value_Type = Integer_Type then $$.Int_Value := $1.Int_Value + $3.Int_Value ; $$.Value_Type := Integer_Type ; elsif $1.Value_Type = Float_Type and $3.Value_Type = Float_Type then $$.Float_Value := $1.Float_Value + $3.Float_Value ; $$.Value_Type := Float_Type ; else raise Type_Mismatch_Error ; end if ; } | Numeric_Expression '-' Term { if $1.Value_Type = Integer_Type and $3.Value_Type = Integer_Type then $$.Int_Value := $1.Int_Value - $3.Int_Value ; $$.Value_Type := Integer_Type ; elsif $1.Value_Type = Float_Type and $3.Value_Type = Float_Type then $$.Float_Value := $1.Float_Value - $3.Float_Value ; $$.Value_Type := Float_Type ; else raise Type_Mismatch_Error ; end if ; } ; Term : Factor { if $1.Value_Type = Integer_Type then $$.Int_Value := $1.Int_Value ; $$.Value_Type := Integer_Type ; elsif $1.Value_Type = Float_Type then $$.Float_Value := $1.Float_Value ; $$.Value_Type := Float_Type ; end if ; } | Term '*' Factor { if $1.Value_Type = Integer_Type and $3.Value_Type = Integer_Type then $$.Int_Value := $1.Int_Value * $3.Int_Value ; $$.Value_Type := Integer_Type ; elsif $1.Value_Type = Float_Type and $3.Value_Type = Float_Type then $$.Float_Value := $1.Float_Value * $3.Float_Value ; $$.Value_Type := Float_Type ; else raise Type_Mismatch_Error ; end if ; } | Term '/' Factor { if $1.Value_Type = Integer_Type and $3.Value_Type = Integer_Type then $$.Int_Value := $1.Int_Value / $3.Int_Value ; $$.Value_Type := Integer_Type ; elsif $1.Value_Type = Float_Type and $3.Value_Type = Float_Type then $$.Float_Value := $1.Float_Value / $3.Float_Value ; $$.Value_Type := Float_Type ; else raise Type_Mismatch_Error ; end if ; } ; Factor : INTEGER_LITERAL { $$.Int_Value := YYInt_Value ; $$.Value_Type := Integer_Type ; } | FLOAT_LITERAL { $$.Float_Value := YYFloat_Value ; $$.Value_Type := Float_Type ; } | IDENTIFIER { Get_Constant_Value_and_Type ( Identifier => YYIdentifier ) ; if YY_Const_Val.Value_Type = Integer_Type then $$.Int_Value := YY_Const_Val.Int_Value ; $$.Value_Type := Integer_Type ; elsif YY_Const_Val.Value_Type = Float_Type then $$.Float_Value := YY_Const_Val.Float_Value ; $$.Value_Type := Float_Type ; end if ; } | '-'IDENTIFIER { Get_Constant_Value_and_Type ( Identifier => YYIdentifier ) ; if YY_Const_Val.Value_Type = Integer_Type then $$.Int_Value := YY_Const_Val.Int_Value ; $$.Value_Type := Integer_Type ; elsif YY_Const_Val.Value_Type = Float_Type then $$.Float_Value := YY_Const_Val.Float_Value ; $$.Value_Type := Float_Type ; end if ; } | '(' Numeric_Expression ')' { if $2.Value_Type = Integer_Type then $$.Int_Value := $2.Int_Value ; $$.Value_Type := Integer_Type ; elsif $2.Value_Type = Float_Type then $$.Float_Value := $2.Float_Value ; $$.Value_Type := Float_Type ; end if ; } ; and the Aflex spec file. -- Math Operators "+" { return '+' ; } "-" { return '-' ; } "/" { return '/' ; } "*" { return '*' ; } "(" { return '(' ; } ")" { return ')' ; } Note that the reference to $$ is a record type with two fields, Float_Value and Integer_Value. In my usage of this I needed to separate Float and Integer which led to the semantic checking I used. You may not need or want this. Hope this helps. Shawn * Sent from RemarQ http://www.remarq.com The Internet's Discussion Network * The fastest and easiest way to search and participate in Usenet - Free!