I'm newish to C++ (but not new to programming in general)
I am trying to call this getOption, and the error message is complaining that this call:
getOption(
"What do you want to do?",
std::vector<std::string>[
"Add a person",
"Delete a person",
"Print database information",
"Report average age",
"List all names",
"Exit"]);
doesn't match the following function definition:
int getOption(std::string prompt, std::vector<std::string> choices)
I tried searching SO, but I don't really understand what is going on enough to come up with keywords that will match already answered questions.
What concept of C++ am I missing here?
You're just screwing up the constructor syntax. Don't use brackets; use braces. (This will only work in C++11.)
use braces instead. It's the initializer list in constructor. It only works in c++11.
Related
I have been trying to solve this issue related to a current school assignment and would highly appreciate if anyone could explain to my why I am getting warnings from my compiler such as decafast.y:201.13-16: warning: rule useless in grammar [-Wother] | Type.
I have provided my code in the two following pastebin files:
decafast.lex: https://pastebin.com/2qzG2cwW
decafast.y: https://pastebin.com/Akg5ehW1
I have also been provided with a file 'decafast.cc' that contains classes and methods that enable me to create lists (I believe that is the purpose), found at:
https://pastebin.com/M7XRJunL
and the specification that I am supposed to follow (grammar found at bottom of page):
http://anoopsarkar.github.io/compilers-class/decafspec.html
My main concern is why I seem to be getting these warnings that are (what I assume to be) causing my code to fail. Nearly every (if not all) of my grammar is deemed to be useless, and despite my online searching (or lack thereof of understanding what has been said already), I am still unsuccessful.
I also had a secondary question if anyone is able to enlighten me. Regarding the .cc file above, I have been given a few classes that implement the decafAST class. In my parser generator file (decafast.y), I attempt to create lists by doing something along the lines of
decafStmtList *s = new decafStmtList();
I assumed that this would allow me to use the push_back() and push_front() methods, which is why I try things such as (in the case of ident_list, line 94) if I see one T_ID, then I create the list for T_ID (identifiers) and push the current T_ID into the list. If I see a situation of ident_list T_COMMA T_ID (which is what I assumed to be the case of a recurring list of comma-separated identifiers), then I would recognize that as an ident_list pattern, and thus push that T_ID into the list as well. Is this the correct way to use the lists that I have been provided?
I would like to stress that as this is an assignment question, I am pleading for any help that you can provide that will allow me to learn on my own terms. I am certain that the users on this website would easily be able to solve this assignment, therefore I would very highly appreciate any insight that you may be able to offer without giving me explicit answers. Thank you all for your time!
The grammar you were provided starts with:
Program = Externs package identifier "{" FieldDecls MethodDecls "}" .
That is, a Program consists of:
A possibly empty list of external declarations (library functions used)
The keyword "package"
An identifier
An open brace
A possibly empty list of field declarations
A possibly empty list of method declarations
A close brace.
Most of the rest of the grammar defines what field and method declarations look like, although a couple of productions define external declarations.
But your grammar is quite different: (I removed the actions because they aren't relevant to the grammar)
start: program
program: extern_list decafpackage
extern_list:
| ExternDefn
decafpackage: T_PACKAGE T_ID T_LCB T_RCB
Your decafpackage consists only of package ID { }, with nothing between the braces.
So most of the rest of the grammar productions, which detail field amd method declarations, can never be used, making them useless.
(Also, your extern_list does not define a list of ExternDecl. It defines an optional ExternDecl. I think you made that same mistake in other list productions.)
The syntax for a bison rule is:
result: components…;
As far as I see none of your rules have the semicolon.
This is a part of the code:
tmp<scalarField> nu(const label patchi) const
{
return nu_.boundaryField()[patchi];
}
I don't really understand the meaning of brackets after functions parentheses. Is this correct syntax and what does it actually mean?
Second question would be about this "tmp". Is that standart syntax of writing temptates or one can choose everything and write for example hallo.scalarField> or example.scalarField>.
Thanks in Advance.
Provided boundaryField() returns something that supports [] syntax, that is valid. For example, if foo is a function that returns a reference to an array, foo()[0] would get the first element of said array.
I created a syntax extension that allow the definition of a type as
type.yjson type_name {
/* type_declaration */
}
to be able to build a record value directly from a json file.
The syntax extension insert a module and the function necessary to do so.
Until here, no problem. The syntax extension do exactly what I wanted.
I start having some issue if I want to use "yjson" at some other place in my code (i.e:function parameter).
Here what I tried:
EXTEND Gram
str_item:
[
[ KEYWORD "type"; KEYWORD "."; "yjson"; tdl_raw = type_declaration ->
Here the error I get when I use "yjson" as a function parameter
[fun_binding] expected after [ipatt] (in [let_binding])
I don't really understand what happen here. It doesn't seems like the rule have been match, so why do I get a parse error ?
I do not perfectly understand the P4's mechanism around this, but [ [ "blahblah" -> ... makes blahblah as a new keyword of the language, so you can no longer use blahblah as a function argument.
To see this, try preprocess your pa_*.ml by camlp4of and see how "blahblah" is expanded to Gram.Skeyword "blahblah". It seems that this Skeyword _ is passed to Structure.using via Insert.insert of P4 and the string is registered as a new keyword.
To keep yjson usable as a normal variable, use id = LIDENT instead of "yjson" in your rule, then check id's content is "yjson" or not in your action.
If I can make a slightly off-topic remark, I think it's wrong to design a custom syntax for type-directed code generation, when there already exist two different syntaxes (one for type_conv and one for deriving), one of which (type-conv) is becoming a de facto standard.
type foo = {
...
} with json
If you pick a syntax for this, you should use this one unless you have very good reasons not to. In fact, type-conv itself is a helper utility to let you write your own type-directed code generators, so you may as well use type-conv directly for what you're trying to do.
(You probably know about Martin Jambon's Atdgen, which made a conscious choice not to use Camlp4; there is ongoing work by Alain Frisch to support annotations directly in the OCaml syntax, but that's not yet ready for consumption.)
Given the following in-progress C++ code:
if (true)
std
, as soon as I enter the first colon (:) of the scope resolution operator (::), XCode oddly inserts some square brackets, and my code looks like this:
if [(true)
std:]
This is really annoying, and it can't figure out why it's doing it. At the moment it's doing it in some files but not others.
It looks like it's trying to help you with Objective C syntax.
At a guess, the files where it's trying to do that have an extension indicating that what you're writing is Objective C. The files where it's not doing it are those that have an extension indicating that they contain C++ instead.
It's trying to be nice and complete the Obj-C syntax.
object method
Adding a colon next turns into
[object methodWithParameter:(parameter) ]
A little something that could be borrowed from IDEs. So the idea would be to highlight function arguments (and maybe scoped variable names) inside function bodies. This is the default behaviour for some C:
Well, if I were to place the cursor inside func I would like to see the arguments foo and bar highlighted to follow the algorithm logic better. Notice that the similarly named foo in func2 wouldn't get highlit. This luxury could be omitted though...
Using locally scoped variables, I would also like have locally initialized variables highlit:
Finally to redemonstrate the luxury:
Not so trivial to write this. I used the C to give a general idea. Really I could use this for Scheme/Clojure programming better:
This should recognize let, loop, for, doseq bindings for instance.
My vimscript-fu isn't that strong; I suspect we would need to
Parse (non-regexply?) the arguments from the function definition under the cursor. This would be language specific of course. My priority would be Clojure.
define a syntax region to cover the given function/scope only
give the required syntax matches
As a function this could be mapped to a key (if very resource intensive) or CursorMoved if not so slow.
Okay, now. Has anyone written/found something like this? Do the vimscript gurus have an idea on how to actually start writing such a script?
Sorry about slight offtopicness and bad formatting. Feel free to edit/format. Or vote to close.
This is much harder than it sounds, and borderline-impossible with the vimscript API as it stands, because you don't just need to parse the file; if you want it to work well, you need to parse the file incrementally. That's why regular syntax files are limited to what you can do with regexes - when you change a few characters, vim can figure out what's changed in the syntax highlighting, without redoing the whole file.
The vim syntax highlighter is limited to dealing with regexes, but if you're hellbent on doing this, you can roll your own parser in vimscript, and have it generate a buffer-local syntax that refers to tokens in the file by line and column, using the \%l and \%c atoms in a regex. This would have to be rerun after every change. Unfortunately there's no autocmd for "file changed", but there is the CursorHold autocmd, which runs when you've been idle for a configurable duration.
One possible solution can be found here. Not the best way because it highlights every occurrence in the whole file and you have to give the command every time (probably the second one can be avoided, don't know about the first). Give it a look though.