Solved

what is the value in yacc?

Posted on 1998-10-30
14
544 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
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
 
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 100 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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.

705 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

21 Experts available now in Live!

Get 1:1 Help Now