Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a syntactically legal expression that has 2 consecutive identifiers separated only by white space in C#?

Tags:

syntax

c#

That might not be the best way to phrase it, but I'm considering writing a tool that converts identifiers separated by spaces in my code to camel case. A quick example:

var zoo animals = GetZooAnimals(); // i can't help but type this
var zooAnimals = GetZooAnimals(); // i want it to rewrite it like this

I was wondering if writing a tool like this would run into any ambiguities assuming it ignores all keywords. The only reason I can think of is if there is a syntactically valid expression with 2 identifiers only separated by white space.

Looking through the grammar I could not immediately find a place that allows it, but perhaps someone else would know better.

On a side note, I realize this is not a practical solution to a real problem a lot of people have, but just something I do all the time and wanted to take a stab at fixing with tools instead of forcing myself to always write camel case.

like image 959
Nick Larsen Avatar asked Mar 16 '15 02:03

Nick Larsen


People also ask

What is C++ definition?

What is C++? C++ is a cross-platform language that can be used to create high-performance applications. C++ was developed by Bjarne Stroustrup, as an extension to the C language. C++ gives programmers a high level of control over system resources and memory.

Are data types in C and C++ same?

Built-in data types is supported in C. Built-in & user-defined data types is supported in C++. C is a function driven language because C is a procedural programming language. C++ is an object driven language because it is an object oriented programming.

What is the use of the indentation in C Plus Plus?

6. What is the use of the indentation in c++? Explanation: To distinguish between different parts of the program like comments, codes, etc.

What is the correct syntax of declaring and defining a reference?

The operator '&' is used to declare reference variable. The following is the syntax of reference variable. datatype variable_name; // variable declaration datatype& refer_var = variable_name; // reference variable. Here, datatype − The datatype of variable like int, char, float etc.


2 Answers

It is hard to tell whether a space-separated sequence of identifiers represents a single variable or not without doing full semantic analysis. For example

Myclass myVariable;

is a pair of space-separated identifiers which are perfectly valid. This would cause an ambiguity if you want to camel-case both type names and variable names.

like image 105
Sergey Kalinichenko Avatar answered Sep 23 '22 12:09

Sergey Kalinichenko


If one enters:

csharp> var i j = 3;
(1,7): error CS1525: Unexpected symbol `j', expecting `,', `;', or `='

in the csharp interactive shell, one gets an error generated by the parser (a (LA)LR parser does bookkeeping what to expect next). Such parser works left-to-right so it doesn't know which characters to come next. It simply knows that the next characters are one of the list shown above.

So that means that there is probably no way to - at least declare a variable - with spaces.

Furthermore based on this context-free grammar for C# there doesn't seem to be a case where one can place two identifiers next to each other. It is for instance possible that a primary expressions is an identifier, but there is no situation where a primary expression is placed next to an identifier (or with an optional part in between).

As @dasblinkenlight says, you can indeed see the rule "local-variable-declaration":

type variable-declarator

with type that can be evaluated to an identifier and variable-declarator starting with an identifier. You can however know that the type is the first identifier (or the var keyword). Some kind of rewrite rule is thus:

(\w+)(\s+\w+)+ -> \1 concat(\2)

where you need to combine (concat) the identifiers of the second group. In case of an assignment.

like image 35
Willem Van Onsem Avatar answered Sep 23 '22 12:09

Willem Van Onsem