compilers in C

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2004
Posts: 2
Reputation: manos is an unknown quantity at this point 
Solved Threads: 0
manos manos is offline Offline
Newbie Poster

compilers in C

 
0
  #1
Oct 11th, 2004
i wanna know how i can write in C a compiler for checking the multiplication and the division of course...
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 3,983
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 926
Moderator
vegaseat's Avatar
vegaseat vegaseat is online now Online
DaniWeb's Hypocrite

Re: compilers in C

 
0
  #2
Oct 11th, 2004
I hope this is what you had in mind. The enclosed function evaluates a string like 123/3.1 or 12345679*63 and returnes the calculated value. Enter this string in your main() and call the function.
  1. /********************* calc_val.cf ******************************
  2. **
  3. ** Parse a calculator string (eg. 5.7/3.2 or 4.2*9.7) for
  4. ** the operator, then do the calculation and return the value.
  5. ** Include string.h and math.h
  6. ** Written in Borland C/C++ by vegaseat 12/11/91
  7. **
  8. *******************************************************************/
  9.  
  10. double calc_val(char *s1)
  11. {
  12. static char *sp, *ss, *sm, *sd, *s2;
  13.  
  14. sp = strchr(s1,'+'); /* parse for +, -, *, or / operator */
  15. ss = strchr(s1,'-');
  16. sm = strchr(s1,'*');
  17. sd = strchr(s1,'/');
  18. if (sp && ss && sm && sd) /* all NULL => no calculation needed */
  19. return (atof(s1));
  20. else {
  21. if (sp) { s2 = sp + 1; return (atof(s1) + atof(s2)); }
  22. if (ss) { s2 = ss + 1; return (atof(s1) - atof(s2)); }
  23. if (sm) { s2 = sm + 1; return (atof(s1) * atof(s2)); }
  24. if (sd) { s2 = sd + 1; return (atof(s1) / atof(s2)); }
  25. }
  26. }
May 'the Google' be with you!
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 2
Reputation: manos is an unknown quantity at this point 
Solved Threads: 0
manos manos is offline Offline
Newbie Poster

compilers in C

 
0
  #3
Oct 13th, 2004
thnks a lot, but this is for programing in C of the operations i think
i wanted to see for the code to compile this program... i mean to see if there is an error in logic or in syntax, and to display the error...
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 256
Reputation: FireNet will become famous soon enough FireNet will become famous soon enough 
Solved Threads: 6
FireNet's Avatar
FireNet FireNet is offline Offline
Posting Whiz in Training

Re: compilers in C

 
0
  #4
Oct 13th, 2004
Try http://www.google.com and search for writing compilers.You will find loads of solid info on that.Also search for Lexical Analisis.
See what you can, remember what you need

Fourzon | Earn via Coding
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 9
Reputation: Waskar is an unknown quantity at this point 
Solved Threads: 0
Waskar Waskar is offline Offline
Newbie Poster

Re: compilers in C

 
0
  #5
Oct 15th, 2004
hope this file helps you!
Attached Files
File Type: doc Automa riconoscitore.doc (56.5 KB, 9 views)
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 9
Reputation: Waskar is an unknown quantity at this point 
Solved Threads: 0
Waskar Waskar is offline Offline
Newbie Poster

Re: compilers in C

 
0
  #6
Oct 15th, 2004
First of all: What a Compiler is?



Most canonical answers are like this: “It is a program that, starting from a high level programming language code, produces an equivalent executable machine code.�



That is slightly inexact!! Because that is what a compiler DOES not what it IS.



A compiler is a “Recognition Automaton� for a giving programming language (C or C++ for example) plus a conversion unit for a giving machine (we’ll say simply that a machine=OS+CPU).



Now, in this case, the question regards the first part of what a compiler is: Since it asked the way to “recognize� two “instances� of a high level language like multiplication an division (One must admit that those two instances have nothing to do with bits and bytes; and you?), that is “to produce an adequate recognition automaton�.



Producing a R.A. (form now that is for Recognition Automaton) is a matter of a special branch of knowledge: Linguistics; The intersection between Math, CS and Linguistics is the “Generative Grammars� witch studies roles and methods of R.A. (See the opera of the great American linguist Noam Chomsky).



After this reference to the theory that stands behind R.A., the answer to the question is a simple kind of R.A. called: R.A. with FINITE STATUS.



Take a simple instance: “John likes candies very much � ; the generative grammar tell us that if we establish that the syntax of this instance is correct (next to a language that we call L1) then we will be able to produce a Syntactic Diagram to describe it, and a R.A. with FINITE STATUS able to recognize that instance (next to the roles of L1); The R.A.F.S. is always represented by a graph.



Here are both L1 Syntactic Diagram and the relative R.A.F.S. graph.



( attachment 1)



Applying L1 syntax roles, as in the diagram, the following instances are correct:



John likes candies very much

John likes candies very very much

John likes candies very very very much

John likes candies very very very very much

……



While the following instances are not correct (X denote where syntax error occur: R.A.F.S. at work):



George likes candies very much (R.A.F.S: Status 0 > Status 1 = syntax error)

X



John eat candies very much (R.A.F.S: S0 > S2 > S1 = syntax error)

X



John likes girls very much (R.A.F.S: S0 > S2 > S3 > S1 = syntax error)

X



John likes candies too much (R.A.F.S: S0 > S2 > S3 > S4 > S1 = syntax error)

X



John likes candies very well (R.A.F.S: S0 > S2 > S3 > S4 > S5 > S1 = syntax error)

X



George is reading a funny book (R.A.F.S: S0 > S1 > S1 > S1 > S1 > S1 = syntax error)

X (X X X X X)



You can think about the finite status of the R.A. as Exciting Levels; In this case:

S0 Standby (Normal Status: what a boredom .. nothing to “COMPILE�)

S1 Syntax Error (Relax Status: They were just kidding)

S2 Exciting level 1 (Curios Status: Wow, perhaps I have a job to do)

S3 Exciting level 2 (Interesting Status: Yes.. Yes.. But I’ve not understand what they want)

S4 Exciting level 3 (Rock ‘n Roll Status: I hear the music in the air!!)

S5 Exciting level 4 (Superman Status: I have UNDERSTAND the instance: “I COMPILE IT� and I’ll give it to the conversion unit to perform actions then I’ll go to rest for a while in my Normal Status)



Now let us study an adequate R.A.F.S. to compile multiplications and divisions:

First of all we must to determinate the Language L2 Syntactic Diagram Roles:

1- Must have two operands.

2- Must have one of two possible operators ( “*� or “/�).

3- In case of operator “/�, the second operand must not be null.

The third role is the only real difference between L1 and L2: here comes in help Mathematics with the Theory of Sets and Formal Logic.



After selecting a Set of interest for the operands of L2 (let say Set R of real numbers), to satisfy the third role we must apply a logic PREDICATE on R (that is an inner relation on the Set): a logic predicate divide a Set into more subsets; the Predicate P1 over IR give us a set we’ll call RP.

RP = P1(R) = R – {Zero}.

Here are the three roles with the new logical symbols:



1- $ ! X1 ÃŽ R , $ ! X2 ÃŽ R



( $ ! X1 ÃŽ R must read: X1 exists and it is unique and belonging to Set R)



2- $ ! “*� ? “/�



(Exists and it is unique and one of the tow operators “*�, “/�)



3- "$ “/� : Þ X2 Î P1(R).



(Whatsoever operator “/� exists then operand X2 MUST belong to Set P1(R) )



Now we are able to draw the Syntactic Diagram and the relative R.A.F.S. (or Compiler if you prefer):


attachment 2




Finally, I’ve enjoy my time writing these few concepts; Hope to you to have same thing while coding the requested compiler (since you have, I think, all necessary elements to do a good job J).
Attached Thumbnails
attachment1.GIF   attachment2.GIF  
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC