new question about old C compiler

T

Today's Mulan

I guess this is new, at least to people who don't know about this, like
me, please be prepared to give me a satifying answer,
so please tell me how the compiler "knows" variable "i" as in a
declaration of "int i" is of type "int"?
 
?

=?ISO-8859-1?Q?St=E9phane_Zuckerman?=

I guess this is new, at least to people who don't know about this, like
me, please be prepared to give me a satifying answer,
so please tell me how the compiler "knows" variable "i" as in a
declaration of "int i" is of type "int"?

Er, because you told the compiler so ? You know, like in "int i;" ? Thus,
until the end of the current lexical scope, "i" is bound to be of type
"int".
 
B

Barry Schwarz

I guess this is new, at least to people who don't know about this, like
me, please be prepared to give me a satifying answer,
so please tell me how the compiler "knows" variable "i" as in a
declaration of "int i" is of type "int"?

There is nothing magical about i. The compiler doesn't know anything
about i until you tell it. In the case you cite, the compiler
recognizes the keyword int and applies that meaning the object name
that follows. If you had written "double i;", i would be a double and
not an int.

What is your real question?

Are you trying to learn how to write a compiler?

Are you trying to understand how your compiler parses the code?
(If so, the answer is it depends on the compiler.)


Remove del for email
 
P

prishil

Barry Schwarz ã®ãƒ¡ãƒƒã‚»ãƒ¼ã‚¸:
There is nothing magical about i. The compiler doesn't know anything
about i until you tell it. In the case you cite, the compiler
recognizes the keyword int and applies that meaning the object name
that follows. If you had written "double i;", i would be a double and
not an int.

What is your real question?

Are you trying to learn how to write a compiler?

Are you trying to understand how your compiler parses the code?
(If so, the answer is it depends on the compiler.)


Remove del for email

Decalring double and int is different to the original question's
meaning ?
 
R

Rod Pemberton

Today's Mulan said:
I guess this is new, at least to people who don't know about this, like
me, please be prepared to give me a satifying answer,
so please tell me how the compiler "knows" variable "i" as in a
declaration of "int i" is of type "int"?

The compiler saves the variable name, i.e., identifier, and the variable
type, i.e., type-specifier, as two fields of a structure (which may have
more fields), usually in a binary tree (but, occasionally on stacks). The
information in the binary tree is what is left after the C code is lexed
(tokenized) and parsed (syntax checked). Since the information in the
binary tree _usually_ lacks lexical elements such as terminators,
punctuation, etc..., it is called an Abstract Syntax Tree (AST). If the
compiler implementors chose to retain the full lexical information of the C
code in the tree, it'd be called a Concrete Syntax Tree (CST). I guess if
you used stacks it'd be an "Abstract Syntax Stack"...


Rod Pemberton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,054
Latest member
LucyCarper

Latest Threads

Top