?
Solved

what is the value in yacc?

Posted on 1998-10-30
14
Medium Priority
?
553 Views
Last Modified: 2010-04-15
Hi I need to put the the value of a yacc token into a char variable. How do I do it?
I need an answer quick.
0
Comment
Question by:laeuchli
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1253938
Sorry, what it is yacc?
0
 
LVL 1

Expert Comment

by:MatthewL
ID: 1253939
yacc - yet another compiler compiler

common utility in unix systems.  laeuchli must be using to write a C compiler.  I had a simular assignment in 4th year university.

unfortunately I can't remember the answer to laeuchli's question.  
0
 
LVL 5

Author Comment

by:laeuchli
ID: 1253940
that right. Please look it up if you can. If you feel I am  offering to few points I will offer more.

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:elfie
ID: 1253941
if inside your yylval union s stands for your strings, you can have something like

strcpy (chararray, $1->s);

where $1 refers to another rule.

When using lex, inside lex you would have something that looks like yylval.s as a string representation of your token for identifier.


0
 
LVL 5

Author Comment

by:laeuchli
ID: 1253942
I am not a yacc expert I am just starting. What is the yylval union? I have not set it up.Please explane a little more.

0
 
LVL 3

Expert Comment

by:elfie
ID: 1253943
if you are using lex and yacc.

Inside lex a variable(array) yytext is defined (check your generated .c source file after running lex on the .l file.
If yytext exist, it will contain the value you want.

in your yacc when you encounter a token (eg. IDENT), you can use the value in the variable yytext, it will contain what you have put in it in your lex rule

If this answer is not enough, please specify your OS, as i'm using different version of lex/yacc on each system
0
 
LVL 3

Accepted Solution

by:
elfie earned 300 total points
ID: 1253944
if you are using lex and yacc.

Inside lex a variable(array) yytext is defined (check your generated .c source file after running lex on the .l file.
If yytext exist, it will contain the value you want.

in your yacc when you encounter a token (eg. IDENT), you can use the value in the variable yytext, it will contain what you have put in it in your lex rule

If this answer is not enough, please specify your OS, as i'm using different version of lex/yacc on each system
0
 
LVL 5

Author Comment

by:laeuchli
ID: 1253945
I am useing LINUX. The way I understand it is that you setup a %union union and put the variable you want to use yylval with like this:
%union
{
char test[100];
}
However this does not work. Why?

0
 
LVL 3

Expert Comment

by:elfie
ID: 1253946
did you refer to the variable yylval.test ?
eg. printf ("%s\n", yylval.test);
0
 
LVL 5

Author Comment

by:laeuchli
ID: 1253947
elfie sorry two more question. one what does $1 mean? please explain. is there any other thing I can put the data in besides a union?


0
 
LVL 3

Expert Comment

by:elfie
ID: 1253948
$1 refers to the first part of your current rule in yaac, with $1 you can refer to the contents of that part.
eg if you have a rule

float: integers DOT integers { first_part = $1; second_part=$2}

and integers is a rule inside yacc, $1 refers to the part before the DOT, and $2 to the part after the dot.
I would suggest to search a book on LEX and YACC, and read something about how to program lex/yacc.

FYI: flex/bison are the GNU equivalents

As you maybe know when working with lex and yacc, both parts will be combined into one program. So if you define some global variables, you can always put some data inside them, but just remember that each you execute a rule, the same variables will used (and thus overwritten). On the other hand inside the union you can put as much variables as you want.

(Sorry for the sometimes late answers, i'm currently quite busy, and working on a number of different locations)

0
 
LVL 5

Author Comment

by:laeuchli
ID: 1253949
Are there any sites on the internet that have examples of this?
P.S. If you give me a hand this last time I will give you some more points. I think I short changed you.

0
 
LVL 3

Expert Comment

by:elfie
ID: 1253950
http://www.enc.hull.ac.uk/people/pjp/Teaching/08208-9697/08208IntroductoryLab.html

This gives a short introduction.
To find it www.altavista.com search engine, and specified
"+lex +yacc +example +yylval +union". this gave more 49 hits.

0
 
LVL 5

Author Comment

by:laeuchli
ID: 1253951
Thanks. Standby I will post a blank question with points for you.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion 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.

765 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