Types variables in yacc interpreter?

Posted on 1997-06-19
Medium Priority
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 */

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.
Question by:joeo061997

Expert Comment

ID: 1251459

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


Author Comment

ID: 1251460
Edited text of question
LVL 51

Accepted Solution

ahoffmann earned 200 total points
ID: 1251461
    double fval;
    int    ival;

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


.. your rules for REAL and INTEGER ..

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
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.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

607 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