• C

what is the value in yacc?

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.
LVL 5
laeuchliAsked:
Who is Participating?
 
elfieConnect With a Mentor Commented:
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
 
AlexVirochovskyCommented:
Sorry, what it is yacc?
0
 
MatthewLCommented:
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
Become an IT Security Management Expert

In today’s fast-paced, digitally transformed world of business, the need to protect network data and ensure cloud privacy has never been greater. With a B.S. in Network Operations and Security, you can get the credentials it takes to become an IT security management expert.

 
laeuchliAuthor Commented:
that right. Please look it up if you can. If you feel I am  offering to few points I will offer more.

0
 
elfieCommented:
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
 
laeuchliAuthor Commented:
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
 
elfieCommented:
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
 
laeuchliAuthor Commented:
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
 
elfieCommented:
did you refer to the variable yylval.test ?
eg. printf ("%s\n", yylval.test);
0
 
laeuchliAuthor Commented:
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
 
elfieCommented:
$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
 
laeuchliAuthor Commented:
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
 
elfieCommented:
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
 
laeuchliAuthor Commented:
Thanks. Standby I will post a blank question with points for you.

0
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.

All Courses

From novice to tech pro — start learning today.