Solved

Types variables in yacc interpreter?

Posted on 1997-06-19
3
540 Views
Last Modified: 2008-03-10
I'm trying to code a once-through interpreter using yacc. One of the requirements is that typed variables be allowed. I've been noodling around through ORA's book on lex and yacc, and they mention using typed variables (through the %union specifier) but don't mention how to mix types in expressions and whatnot. I looked on the net for a similar problem solution, but couldn't find one. Can anyone explain type conversion in yacc (i.e., an expression can be either a float or an int) or point me to a solution?

For example, I need to declare two variables, and then assign the result of a computation to a third, as follows:

int x,z;
float y;

y=2; /* conversion from int to float - not a problem */
x=4;

z=x/y; /* requires specification of intexp '/' floatexp */

It's the last expression that concerns me. I don't how to tell yacc to handle that without specifying every combination of type in the expression grammar, and I'm not sure how to tell yacc than an "expression" can be either an int or a float or a string.
0
Comment
Question by:joeo061997
3 Comments
 
LVL 2

Expert Comment

by:Philippe
ID: 1251459

I'm not sure I understand your question. Could you give a simple example of something you would like to parse?


0
 

Author Comment

by:joeo061997
ID: 1251460
Edited text of question
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 100 total points
ID: 1251461
%union
{
    double fval;
    int    ival;
}

%token <fval> REAL
%token <ival> INTEGER

%%

.. your rules for REAL and INTEGER ..
0

Featured Post

How our DevOps Team Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question