From: Shawn Barber <SbarberNOmfSPAM@snet.net>
Subject: Re: Ada95 Ayacc / Aflex specifications
Date: 1999/09/07
Date: 1999-09-07T00:00:00+00:00 [thread overview]
Message-ID: <28b1320f.6dc4fec9@usw-ex0102-014.remarq.com> (raw)
In-Reply-To: 7r0gob$m31$1@nnrp1.deja.com
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!
next prev parent reply other threads:[~1999-09-07 0:00 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
1999-09-06 0:00 Ada95 Ayacc / Aflex specifications mark_doherty
1999-09-07 0:00 ` Shawn Barber [this message]
1999-09-07 0:00 ` Ted Dennison
1999-09-08 0:00 ` cts
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox