i wanna know how i can write in C a compiler for checking the multiplication and the division of course... ;)

0

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.

```
/********************* calc_val.cf ******************************
**
** Parse a calculator string (eg. 5.7/3.2 or 4.2*9.7) for
** the operator, then do the calculation and return the value.
** Include string.h and math.h
** Written in Borland C/C++ by vegaseat 12/11/91
**
*******************************************************************/
double calc_val(char *s1)
{
static char *sp, *ss, *sm, *sd, *s2;
sp = strchr(s1,'+'); /* parse for +, -, *, or / operator */
ss = strchr(s1,'-');
sm = strchr(s1,'*');
sd = strchr(s1,'/');
if (sp && ss && sm && sd) /* all NULL => no calculation needed */
return (atof(s1));
else {
if (sp) { s2 = sp + 1; return (atof(s1) + atof(s2)); }
if (ss) { s2 = ss + 1; return (atof(s1) - atof(s2)); }
if (sm) { s2 = sm + 1; return (atof(s1) * atof(s2)); }
if (sd) { s2 = sd + 1; return (atof(s1) / atof(s2)); }
}
}
```

0

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...:)

0

Try http://www.google.com and search for writing compilers.You will find loads of solid info on that.Also search for Lexical Analisis.

0

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

This topic has been dead for over six months. Start a new discussion instead.

Recommended Topics

My question is not so simple:

I have a WindowsForm with a diffrent controls like: textboxes, comboboxes, checkboxes, datetimepickers, listview and so on...

How can i create temporary data ...

Hi guys,

I have developed a windows application using vb.net 2012 and sqlserver express edition 2012. the application is able to access the database on a local hot computer. but ...