Solved

Types variables in yacc interpreter?

Posted on 1997-06-19
3
531 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand and use structures 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.

758 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now