Solved

what is the value in yacc?

Posted on 1998-10-30
14
545 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
Give your grad a cloud of their own!

With up to 8TB of storage, give your favorite graduate their own personal cloud to centralize all their photos, videos and music in one safe place. They can save, sync and share all their stuff, and automatic photo backup helps free up space on their smartphone and tablet.

 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
libcurl and C++ - Post JSON Data 8 1,178
Best UNIX-compatible free C compiler for Windows or Mac 6 237
How to jump to matching brace in eclipse editor ? 1 263
Computer slow / BSOD 10 36
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

932 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

8 Experts available now in Live!

Get 1:1 Help Now