lex/yacc: mixed type expressions

janovetz@eehpx3.cen.uiuc.edu (Jacob W Janovetz)
Tue, 26 Sep 1995 23:02:26 GMT

          From comp.compilers

Related articles
lex/yacc: mixed type expressions janovetz@eehpx3.cen.uiuc.edu (1995-09-26)
| List of all articles for this month |
Newsgroups: comp.compilers
From: janovetz@eehpx3.cen.uiuc.edu (Jacob W Janovetz)
Keywords: yacc, assembler, question
Organization: University of Illinois at Urbana
Date: Tue, 26 Sep 1995 23:02:26 GMT

Hello,


      Still working on the assembler...


      I'd also like to know how to implement mixed type expressions (with
just ints/floats). For example:


      2 + 3.2 + 5.9 = 2 (int) + 9.1 (float) = 11.
    (2 + 3.2) + 5.9 = 5 (int) + 5.9 (float) = 10.


      etc.


      Is this how most int<-->float conversions work? I have the following
excerpt:


iexpr:
            iexpr '+' ifexpr { $$ = $1 + $3; printf("addi %d\n", $$); }
        | INTEGER
        ;
ifexpr:
            iexpr
        | fexpr { $$ = (int)$1; }
        ;
fexpr:
            fexpr '+' fiexpr { $$ = $1 + $3; printf("addf %f\n", $$); }
        | FLOAT
        ;
fiexpr:
            fexpr
        | iexpr { $$ = (double)$1; }
        ;


      If this is stupid, please let me know! I'm a lex/yacc beginner. Also,
I get 4 'shift/reduce' conflicts here, which leads me to believe it is
bad form.


        Jake


--


janovetz@coewl.cen.uiuc.edu
      University of Illinois
                                                      | http://www.cen.uiuc.edu/~janovetz/home.html
[You can do this, but I'd suggest having a single yacc expression type,
keeping the type of it as a piece of the semantic value, and doing the
conversions yourself. It's easier to do, and lets you give better error
messages than "syntax error" -John]




--


Post a followup to this message

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