Solved

example in yacc

Posted on 1998-11-16
10
215 Views
Last Modified: 2010-04-15
Could some one write and example program in yacc that shows how to use the pseudovariables $1,$2,etc.. to get the value of a variable and puts it in a char variable?
Thanks
Will give more points later.
0
Comment
Question by:laeuchli
  • 4
  • 2
  • 2
  • +2
10 Comments
 
LVL 5

Author Comment

by:laeuchli
ID: 1254320
come on guys, a comment at least.
0
 

Expert Comment

by:mkahlert
ID: 1254321
Here's an example, perhaps this helps:

actualparams
      :S_LB actualparamlist S_RB
            { $$ = $2; }
      |
            { $$ = NULL; }
      ;

The $1 is the first term of the definiton, $2 the second, and so on.
e.g.

term: '(' term ')'
   -> $1 = '('
   -> $2 = term
   -> $3 = ')'
And: $$ is the result (the left side, "term").
0
 
LVL 3

Expert Comment

by:elfie
ID: 1254322


Sorry don't have time to make a small  example  but you cvan check

http://www.cs.um.edu.mt/~hzarb/CSM201/notes/lecture13/node24.html
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 3

Expert Comment

by:elfie
ID: 1254323
another exaple and 'manuals'

http://www-iiuf.unifr.ch/idp/applications/bison.html

check here the infix notation calculator: calc (23.0)

ps: remember bison is the GNU equivalent of yacc.
0
 
LVL 5

Author Comment

by:laeuchli
ID: 1254324
sorry, mkahlert,I want more complete sample code.Start from the start of a yacc program.

0
 
LVL 27

Expert Comment

by:BigRat
ID: 1254325
>> Will give more points later.

Should give the point now! (to elfie)
0
 

Expert Comment

by:avigon
ID: 1254326
Very easy, it only recognizes this expression: "1+2", but it works.

Code:

%%

result: '1' '+' '2'
          { $$ = $1 + $3; }

%%

it parses that expression and gives to the "result" ($$), the value of the addition of '1' ($1) and '2' ($3). In this case, $2 is '+'.

You must do now a mini-lex and a main, that can be defined in the yacc-file if you want... Anyway as mkahlert pointed, the idea is that the variables $1,... represent the values of the respective position-field. In the example $$ = value of "result", and if we use a rule like this:

  result : var '+' var

the variables $1 and $3 will have the value of each var, that is is you parse the string: 1+2, "result" will store the value 3 at the end of the parsing.





0
 
LVL 5

Author Comment

by:laeuchli
ID: 1254327
well how do you print the value of $$?

0
 

Accepted Solution

by:
avigon earned 10 total points
ID: 1254328
You just put:


%%

result: '1' '+' '2'
                { $$ = $1 + $3;
                   printf("%d",$$);  }

%%

For example.
0
 
LVL 5

Author Comment

by:laeuchli
ID: 1254329
Ok,but what if I wanted to do this with a char? and is the one and two the first two of the tokens for that section or for the whole program. Thanks.

0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
In desperate need of help 8 138
how to communicate between two pc's through vb.net code ? 4 153
List out all word 7 293
Arduino EDI - Programming language 3 95
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 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.

809 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