سؤال

I'm on Ubuntu Saucy and I was hoping someone would tell me in want file the c struct dereference operator -> is in..when I

grep -R -> . 

i get

 bash: .: Is a directory

and when I omit the '.' i get

bash: syntax error near unexpected token `newline' 

id like to reproduce this functionality in another language but also google had nothing..any help is much appreciated.

هل كانت مفيدة؟

المحلول 2

bash reports that error message because, in grep -R -> ., the > looks like redirection of standard output, so bash attempts to open . as a file, to which output would be written. This fails because . stands for the current directory, and a directory cannot be opened in the same way as a file.

You can avoid bash interpreting the > this way by using quotes, such as grep -R "->" .. However, then grep interprets the - as indicating a command-line option, rather than as a pattern to search for. You can avoid this by marking the - with a backslash:

grep -R "\->" .

or using -- to tell grep where the options end:

grep -R -- "->" .

Unfortunately, this will only find files that contain the string “->”; it will not distinguish files that implement the C -> operator. And there will be many files containing that string which are unrelated to what you are searching for.

I expect the C -> operator will be implemented something like this:

  • Some code in some file involved in lexical analysis will recognize the string “->” and will respond by producing a token, which may have an arbitrary name (one chosen by the author of this particular code, not mandated by any particular standard or specification) such as DEREFERENCE.
  • Some code in some file involved in grammatical analysis will either process the DEREFERENCE token by emitting code (likely in an intermediate language for the compiler’s own use) that performs the dereference operation. Alternately, the code might convert expressions such as a->b to (*a).b and then emitting code for the latter.

Neither the process of emitting this code nor the code itself necessarily are single operations. It may involve checking the type of the left operand, calculating the offset of the right operand, relating the expression to things in its context (to support optimization), and generating the actual intermediate code. That intermediate code may be further processed by optimization or other parts of the compiler and will be used later to generate final machine code.

In summary, there is no single place where -> is implemented. It is part of a complex process and interacts with other parts of the compiler.

نصائح أخرى

Your grep is not written properly:

grep -R -> . 

What's it doing? Something like this:

grep -R - > . 

Now that may be a little more clear: it is going to look in "-" (which may be stdin, I'm not sure about for grep but many tools use - to mean stdin). And it is going to write the results to "." except that is a directory (the current directory, specifically). So it fails.

If you want to search for the -> operator, you can use this:

grep -R -- "->" . 

"--" is a fairly standard Unix thing that means "stop processing options after this," and putting the "->" in quotes should stop the shell from interpreting it as two tokens (stdin plus redirect).

Anyway, none of this really matters because you will never find the source code for the operator ->. It's defined in the language and the source code which implements it is entirely inside your compiler. So if you really want, go download the source for your compiler and grep there...even then it may not be found very quickly by grepping for "->".

An operator is not a library class or function, it has no "definition" (unless we are talking about overloaded operators).

The compiler parses the symbol and creates the adequate code, but it is not by just replacing the "->" with some method.

The - in grep is specifying a new command option. So, you need to escape it. There is a handy grep syntax grep -- "->" will search for ->.

However, I doubt you will find anything as operators are not methods.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top