What is the difference between "keyword" and "reserved word"? - language-agnostic

What's the difference between a keyword and a reserved word?
For example, in the proposal for concepts in C++ one can read the following statement:
This proposal introduces five new keywords: concept, concept map, where, axiom, and late check. All of these keywords will also be
reserved words.

Keywords have a special meaning in a language, and are part of the syntax.
Reserved words are words that cannot be used as identifiers (variables, functions, etc.), because they are reserved by the language.
In practice most keywords are reserved words and vice versa. But because they're two different things it may happen that a keyword is not a reserved word (e.g. a keyword only has meaning in a special context, and can therefore be used as an identifier), or a reserved word is not a keyword (e.g. because it is reserved for future use).
Update: Some examples as given by others that illustrate the distinction:
In Java, goto is a reserved word but not a keyword (as a consequence, you cannot use it at all)
Fortran has no reserved words, all keywords (if, then, etc.) can be used as identifiers

Just to show that the distinction is very meaningful:
Not in all languages are all keywords reserved words. In Fortran it is possible to do this:
if if then then else else
In this case, the keywords are not reserved, but depending on context can be interpreted by the compiler as variables.

A good example of this distinction is "goto" in Java. It's not a language keyword (i.e. it's not valid Java), but it is a reserved word.
It seems that the java designers are telling us "We're not going to use 'goto', and neither are you".

Wiki says this "A keyword is a word that is special only in certain contexts but a reserved word is a special word that cannot be used as a user-defined name."
http://en.wikipedia.org/wiki/Reserved_word#Reserved_word_vs._keyword

I guess keyword is a word used as "keyword" (like if, for, switch, etc...) while a reserved word is something you cannot use as variable name because it might become a keyword in a future version of the language.

Keyword: It has some meaning and we can use in program.
Reserved word: We can't use in program. They may be used in future.
Example: goto

Really it will depend a lot on context. For example, the ISO C++ Standard says that things like "if", "while", "int" etc. are keywords, and doesn't actually use the term reserved word, except once, in a footnote, where something else was probably meant :-)
The standard doe specify reserved names - for example, all names that begin with an underscore and an uppercase letter are reserved names.

Keywords : Keywords has some special functionalities to the compiler. So the keywords can not be used as identifiers in coding.
Reserved words: Reserve words are the words which are reserved for future use. In java, const and goto are the reserved words which are not being used currently and may be brought back to java in the future.
If we check here Java Language Keywords (https://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html) , It says that java (latest I guess) has 50 keywords including goto and const. So goto and const are the keywords which are reserved.

keyword, - a word with special meaning in a particular context.
It's semantic definition.
reserved word is a word that cannot be used as an identifier -
such as, variable, and function name. It's syntactic definition.
E.g.In Java, all keywords are reserved words. Probably not the reverse.
goto is reserved word but not used and has no function.
In older languages like FORTRAN there were keywords but no reserved words.
However, keyword and reserved word are used interchangeably.

Reserved words and keywords are mostly the same and they have pre-defined meanings in GW-BASIC...these have pre-defined uses and cannot be used or re-defined for any other purpose in Basic. Keywords cannot be used as a variable name. Some of the keywords of Basic are...IF, THEN, WHILE etc..

Related

Why does gensim ignore underscores during preprocessing?

Going through the gensim source, I noticed the simple_preprocess utility function clears all punctuations except those with words starting with an underscore, _. Is there a reason for this?
def simple_preprocess(doc, deacc=False, min_len=2, max_len=15):
tokens = [
token for token in tokenize(doc, lower=True, deacc=deacc, errors='ignore')
if min_len <= len(token) <= max_len and not token.startswith('_')
]
return tokens
The underscore ('_') isn't typically meaningful punctuation, but is often considered a "word" character in programming and text-processing.
For example, common regular-expression syntax uses \w to indicate a "word character". Per https://www.regular-expressions.info/shorthand.html :
\w stands for "word character". It always matches the ASCII characters
[A-Za-z0-9_]. Notice the inclusion of the underscore and digits. In
most flavors that support Unicode, \w includes many characters from
other scripts. There is a lot of inconsistency about which characters
are actually included. Letters and digits from alphabetic scripts and
ideographs are generally included. Connector punctuation other than
the underscore and numeric symbols that aren't digits may or may not
be included. XML Schema and XPath even include all symbols in \w.
Again, Java, JavaScript, and PCRE match only ASCII characters with \w.
As such, it's often used in authoring, or in other text-preprocessing steps, to connect other groups of letters/numbers that should be kept together as a unit. Thus it's not often cleared with other true punctuation.
The code you've referenced also does something else, different than your question about clearing punctuation: it drops word-tokens beginning with _.
I'm not sure why it does that; at some point that code may have be designed with some specific text-format in mind where leading-underscore tokens were semantically-unimportant formatting directives.
The simple_preprocess() function in gensim is just a quick-and-dirty baseline helpful for internal tests and compact beginner tutorials. It shouldn't be considered a "best practice".
Real projects should give more consideration to the kind of word-tokenization that makes sense for their data and purposes – and either look to libraries with more options, or custom approaches (which still need not be more than a few lines of Python), to implement tokenization that best suits their needs.

How do I escape a reserved word in Actionscript?

In scala one can use backticks (`) as illustrated below:
match is a reserved word so the following will not compile:
class Foo(var match: String)
but this will:
class Foo(var `match`: String)
Similar to the above, what does one do to escape reserved words in AS3 in the example above without using dynamic properties?
It is normally not recommended to use reserved words as part of your variables, function names, function parameters or Class names. Why? Because as in any language reserved words are meant to only be used for specific uses.
You can also always add another small word in camel case to differentiate your variable from the reserved word.
I ran into a similar problem with posting variables to paypal. For example:
URLData.return = "mystring";
won't work because return is a reserve word. So simply use:
URLData.['return'] = "mystring";

What does a double colon followed by an equals sign (::=) mean in programming documentation?

What does ::= mean in programming documentation?
For example in the Lua documentation: or in Python documentation.
It symbolizes 'symbol derivation rule' in Backus–Naur Form
Meaning that in:
<symbol> ::= __expression__
nonterminal <symbol> consists of (is defined as, is constructed from, derives from) __expression__
It's used to describe language grammars.
Notice that both examples are in Extended Backus–Naur Form, but using a traditional BNF symbol-expression separator (::=).
This is Backus-Naur Form (BNF) notation describing the language. ::= in this context means is defined as.
For example, in the Python language documentation you refer to, an identifier is defined as a letter or an underscore, followed by a letter, a digit or an underscore. The notation then goes on to describe what a letter and a digit is defined as, and so on.
As others have already said, it's part of the BNF notation. Wikipedia has an explanation and some examples, which I won't repeat here.
The history and evolution of the ::= symbol itself is explained in The History of the ALGOL Effort (p29 onwards).
The given element syntax. For example:
identifier ::= (letter|"_") (letter | digit | "_")*
Means all identifiers must conform to the given syntax rule.

What is your system for avoiding keyword naming clashes?

Typically languages have keywords that you are unable to use directly with the exact same spelling and case for naming things (variables,functions,classes ...) in your program. Yet sometimes a keyword is the only natural choice for naming something. What is your system for avoiding/getting around this clash in your chosen technology?
I just avoid the name, usually. Either find a different name or change it slightly - e.g. clazz instead of class in C# or Java. In C# you can use the # prefix, but it's horrible:
int #int = 5; // Ick!
There is nothing intrinsically all-encompassing about a keyword, in that it should stop you from being able to name your variables. Since all names are just generalized instances of some type to one degree or another, you can always go up or down in the abstraction to find another useful name.
For example, if your writing a system that tracks students and you want an object to represent their study in a specific field, i.e. they've taken a "class" in something, if you can't use the term directly, or the plural "classes", or an alternative like "studies", you might find a more "instanced" variation: studentClass, currentClass, etc. or a higher perspective: "courses", "courseClass" or a specfic type attribute: dailyClass, nightClass, etc.
Lots of options, you should just prefer the simplest and most obvious one, that's all.
I always like to listen to the users talk, because the scope of their language helps define the scope of the problem, often if you listen long enough you'll find they have many multiple terms for the same underlying things (with only subtle differences). They usually have the answer ...
Paul.
My system is don't use keywords period!
If I have a function/variable/class and it only seems logical to name it with a keyword, I'll use a descriptive word in front of the keyword.
(adjectiveNoun) format. ie: personName instead of Name where "Name" is a keyword.
I just use a more descriptive name. For instance, 'id' becomes identifier, 'string' becomes 'descriptionString,' and so on.
In Python I usually use proper namespacing on my modules to avoid name clashes.
import re
re.compile()
instead of:
from re import *
compile()
Sometimes, when I can't avoid keyword name clashes I simply drop the last letter off the name of my variable.
for fil in files:
pass
As stated before either change class to clazz in Java/C#, or use some underscore as a prefix, for example
int _int = 0;
There should be no reason to use keywords as variable names. Either use a more detailed word or use a thesaraus. Capitalizing certain letters of the word to make it not exactly like the keyword is not going to help much to someone inheriting your code later.
Happy those with a language without ANY keywords...
But joke apart, I think in the seldom situations where "Yet sometimes a keyword is the only natural choice for naming something." you can get along by prefixing it with "my", "do", "_" or similar.
I honestly can't really think of many such instances where the keyword alone makes a good name ("int", "for" and "if" are definitely bad anyway). The only few in the C-language family which might make sense are "continue" (make it "doContinue"), "break" (how about "breakWhenEOFIsreached" or similar ?) and the already mentioned "class" (how about "classOfThingy" ?).
In other words: make the names more reasonable.
And always remember: code is WRITTEN only once, but usualy READ very often.
Typically I follow Hungarian Notation. So if, for whatever reason, I wanted to use 'End' as a variable of type integer I would declare it as 'iEnd'. A string would be 'strEnd', etc. This usually gives me some room as far as variables go.
If I'm working on a particular personal project that other people will only ever look at to see what I did, for example, when making an add-on to a game using the UnrealEngine, I might use my initials somewhere in the name. 'DS_iEnd' perhaps.
I write my own [vim] syntax highlighters for each language, and I give all keywords an obvious colour so that I notice them when I'm coding. Languages like PHP and Perl use $ for variables, making it a non-issue.
Developing in Ruby on Rails I sometime look up this list of reserved words.
In 15 years of programming, I've rarely had this problem.
One place I can immediately think of, is perhaps a css class, and in that case, I'd use a more descriptive name. So instead of 'class', I might use 'targetClass' or something similar.
In python the generally accepted method is to append an '_'
class -> class_
or -> or_
and -> and_
you can see this exemplified in the operator module.
I switched to a language which doesn't restrict identifier names at all.
First of all, most code conventions prevent such a thing from happening.
If not, I usually add a descriptive prose prefix or suffix:
the_class or theClass infix_or (prefix_or(class_param, in_class) , a_class) or_postfix
A practice, that is usually in keeping with every code style advice you can find ("long names don't kill", "Longer variable names don't take up more space in memory, I promise.")
Generally, if you think the keyword is the best description, a slightly worse one would be better.
Note that, by the very premise of your question you introduce ambiguity, which is bad for the reader, be it a compiler or human. Even if it is a custom to use class, clazz or klass and even if that custom is not so custom that it is a custom: it takes a word word, precisely descriptive as word may be, and distorts it, effectively shooting w0rd's precision in the "wrd". Somebody used to another w_Rd convention or language might have a few harsh wordz for your wolds.
Most of us have more to say about things than "Flower", "House" or "Car", so there's usually more to say about typeNames, decoratees, class_params, BaseClasses and typeReferences.
This is where my personal code obfuscation tolerance ends:
Never(!!!) rely on scoping or arcane syntax rules to prevent name clashes with "key words". (Don't know any compiler that would allow that, but, these days, you never know...).
Try that and someone will w**d you in the wörd so __rd, Word will look like TeX to you!
My system in Java is to capitalize the second letter of the word, so for example:
int dEfault;
boolean tRansient;
Class cLass;

In MATLAB, what ASCII characters are allowed to be in a function name?

I have a set of objects that I read information out of that contain information that ends up becoming a MATLAB m file. One piece of information ends up being a function name in MATLAB. I need to remove all of the not-allowed characters from that string before writing the M file out to the filesystem. Can someone tell me what characters make up the set of allowed characters in a function name for MATLAB?
Legal names follow the pattern [A-Za-z][A-Za-z0-9_]*, i.e. an alphabetic character followed by zero or more alphanumeric-or-underscore characters, up to NAMELENGTHMAX characters.
Since MATLAB variable and function naming rules are the same, you might find genvarname useful. It sanitizes arbitrary strings into legal MATLAB names.
The short answer...
Any alphanumeric characters or underscores, as long as the name starts with a letter.
The longer answer...
The MATLAB documentation has a section "Working with M-Files" that discusses naming with a little more detail. Specifically, it points out the functions NAMELENGTHMAX (the maximum number of characters in the name that the OS will pay attention to), ISVARNAME (to check if the variable/function name is valid), and ISKEYWORD (to display restricted keywords).
Edited:
this may be more informative:
http://scv.bu.edu/documentation/tutorials/MATLAB/functions.html