Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Types variables in yacc interpreter?

Posted on 1997-06-19
3
Medium Priority
?
549 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 200 total points
ID: 1251461
%union
{
    double fval;
    int    ival;
}

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

%%

.. your rules for REAL and INTEGER ..
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Suggested Courses

971 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